Please enable JavaScript to view this site.

Navigation: Advanced topics > Events > Sample events > Email

Send an email with an attachment from the database

Scroll Prev Next More

 

To send an email with attachments stored in a database field, use the following code in events like BeforeAdd/AfterAdd/BeforeEdit/AfterEdit.

 

Make sure to replace "Field that stores attachments" with the actual field name. This must be a TEXT or VARCHAR(max) field that is set up as File/Image in ASPRunner.NET.

 

Note: Attachments will only work when you selected "Use custom mailer server settings" under "Email settings".

 

 

 

dynamic attachments = XVar.Array(), fileArray = XVar.Array(), msg = null, ret = XVar.Array(), subject = null;
email = new XVar("test@gmail.com");
msg = new XVar("File Attached");
subject = new XVar("Attached some files");
fileArray = XVar.Clone(CommonFunctions.my_json_decode((XVar)(values["Field that stores attachments"])));
attachments = XVar.Clone(XVar.Array());
msg = new XVar("");
foreach (KeyValuePair<XVar, dynamic> f in fileArray.GetEnumerator())
  {
  attachments.InitAndSetArrayItem(new XVar("path", f.Value["name"]), null);
  }
msg = MVCFunctions.Concat(msg, "Some field: ", values["Some field"], "\r");
msg = MVCFunctions.Concat(msg, "Some other field: ", values["Some other field"], "\r");
ret = XVar.Clone(MVCFunctions.runner_mail((XVar)(new XVar("to", email, "subject", subject, "body", msg, "attachments", attachments))));
if(XVar.Pack(!(XVar)(ret["mailed"])))
  {
  MVCFunctions.Echo(ret["message"]);
  }

 

See also:

Send simple email

runner_mail function

How to email selected records as separate PDF files

Email selected records

Send an email to all users

Send an email to selected users

Send an email with updated fields only