I need to send an email containing attachments when the order is finalized. The customer has to download and print a Pdf ticket and use it to access a purchased event. I tried with the EmailTemplateService in the context of the NotificationEventHandlerBase<OrderFinalizedNotification>, but I'm not able to find a way to include one or more files as email attachments. Do you have any suggestions?
Hopefully this gives you enough info to get it working, but let me know if there is anything that isn't clear.
Matt
PS the EmailSendPipelineArgs should provide details of the email template being sent so you can use that to only add the attachment if it's the confirmation email.
Hi Matt, thanks for the help, I hadn't read that section in the documentation. I tried to use the Pipelines, and in debug I see that the code runs correctly when a confermation email is sent. However, the attachment - which is an existing file on my PC - is not sent using the following code. Is there anything else I'm missing?
public override PipelineResult<EmailContext>
Execute(EmailSendPipelineArgs args)
{
if (args.EmailContext.EmailTemplate.Alias == Constants.OrderConfirmationEmail)
{
var order = args.EmailContext.Model as OrderReadOnly;
if (order != null)
{
var attachment = new Attachment("C:\\test.txt");
args.EmailContext.MailMessage.Attachments.Add(attachment);
}
}
return Ok(args.EmailContext);
}
When you say you can see the code runs, does the attachment show in the mail messages attachments collection? My initial guess would be a) does the text file exist and b) does the sending process have permission to access it.
I'll be more precise. The path is correct, the file is detected and it is correctly added to the collection. If I look at the EmailContext object in the last line return Ok(args.EmailContext); I see that the Attachments property contains my document. However, I don't see the attachment in the email received. Is it possible that the MailMessage.Attachments property is replaced somewhere else - later - in the code?
Ok, my guess is that you've copied the docs and I've just realized, the example in the docs isn't going to work as the sending of the email is part of the pipeline, so appending your task on the end is essentially going to run your task after the email is sent.
Where do you get the "SendSmtpEmail" from in the V3.0.0 & Commerce versions of the docs? Mine's lit up permanently red and Resharper can't find anything to link it from. Same with "composition".
Order confirmation email with attachments
I need to send an email containing attachments when the order is finalized. The customer has to download and print a Pdf ticket and use it to access a purchased event. I tried with the
EmailTemplateService
in the context of theNotificationEventHandlerBase<OrderFinalizedNotification>
, but I'm not able to find a way to include one or more files as email attachments. Do you have any suggestions?Hi Michele,
You'll need to hook into the email sending pipeline to add the attachment to the email message. We have a basic example of exactly this in the docs for pipelines https://vendr.net/docs/core/1.8.0/key-concepts/pipelines/#example-pipeline-task
Hopefully this gives you enough info to get it working, but let me know if there is anything that isn't clear.
Matt
PS the
EmailSendPipelineArgs
should provide details of the email template being sent so you can use that to only add the attachment if it's the confirmation email.Hi Matt, thanks for the help, I hadn't read that section in the documentation. I tried to use the Pipelines, and in debug I see that the code runs correctly when a confermation email is sent. However, the attachment - which is an existing file on my PC - is not sent using the following code. Is there anything else I'm missing?
When you say you can see the code runs, does the attachment show in the mail messages attachments collection? My initial guess would be a) does the text file exist and b) does the sending process have permission to access it.
Matt
I'll be more precise. The path is correct, the file is detected and it is correctly added to the collection. If I look at the EmailContext object in the last line
return Ok(args.EmailContext);
I see that the Attachments property contains my document. However, I don't see the attachment in the email received. Is it possible that theMailMessage.Attachments
property is replaced somewhere else - later - in the code?And how are you attaching your pipeline task?
Ok, my guess is that you've copied the docs and I've just realized, the example in the docs isn't going to work as the sending of the email is part of the pipeline, so appending your task on the end is essentially going to run your task after the email is sent.
You'll need to use something like this instead.
Oh, you guessed it. With your code, the attachment is added before and everything is working. Thank you!
Where do you get the "SendSmtpEmail" from in the V3.0.0 & Commerce versions of the docs? Mine's lit up permanently red and Resharper can't find anything to link it from. Same with "composition".
It’s just called SendEmailTask in v3 / Umbraco Commerce
Umbraco.Commerce.Core.Pipelines.Email.Tasks.SendEmailTask
Perfect, thanks.
Vendr.Core.Pipelines.Email.Tasks.SendEmailTask
is working on a reply...