Skip to content

Instantly share code, notes, and snippets.

@enkelmedia
Created October 29, 2017 13:35
Show Gist options
  • Save enkelmedia/e3d9ff59d38bb36575d7af19d6ad61c1 to your computer and use it in GitHub Desktop.
Save enkelmedia/e3d9ff59d38bb36575d7af19d6ad61c1 to your computer and use it in GitHub Desktop.
public class AttachmentRendertask : Services.RenderTasks.RenderTask
{
string _attachmentPath = String.Empty;
public override void ProcessPreRender(RenderResult renderResult, RenderTaskParameters parameters)
{
// This method is executed when the scheduled jobs web request is still running. This means that the UmbracoContext, ApplicationContext etc can still be used.
// Let's save the path to the attachment media file here.
// Check if this newsletter is connected to a content node (if the ContenNodeId-property has a value the letter is created from that node in the content section).
if (parameters.Newsletter.ContentNodeId.HasValue && parameters.Newsletter.ContentNodeId.Value > 0)
{
// Get this node as a IPublishedContent-object
var helper = new UmbracoHelper(UmbracoContext.Current);
var node = helper.TypedContent(parameters.Newsletter.ContentNodeId);
// Check that there is an attachment-property defined (this could be any name of the property, attachement is just an example).
if (!node.HasProperty("attachment") || node.GetPropertyValue<int>("attachment") <= 0)
return;
var attachmentMediaId = node.GetPropertyValue<int>("attachment");
var property = helper.TypedMedia(attachmentMediaId);
_attachmentPath = HttpContext.Current.Server.MapPath(property.GetPropertyValue<string>("umbracoFile"));
}
}
public override void ProcessUniqueItem(RenderResult renderResult, RenderTaskUniqueItemParameters parameters)
{
if (!string.IsNullOrEmpty(_attachmentPath))
{
// add new attachment to the e-mail;
parameters.MailMessage.Attachments.Add(new Attachment(_attachmentPath));
}
}
}
@enkelmedia
Copy link
Author

Thanks for the feedback! I've updated the support article with some more details on this. Hope this make it more clear. All the best!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment