Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michele Benolli 18 posts 126 karma points
    Jul 23, 2021 @ 15:13
    Michele Benolli
    0

    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 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?

  • Matt Brailsford 3210 posts 16985 karma points MVP 8x c-trib
    Jul 23, 2021 @ 15:19
    Matt Brailsford
    0

    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.

  • Michele Benolli 18 posts 126 karma points
    Jul 26, 2021 @ 12:17
    Michele Benolli
    0

    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);
    }
    
  • Matt Brailsford 3210 posts 16985 karma points MVP 8x c-trib
    Jul 26, 2021 @ 12:23
    Matt Brailsford
    0

    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

  • Michele Benolli 18 posts 126 karma points
    Jul 26, 2021 @ 12:32
    Michele Benolli
    0

    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?

  • Matt Brailsford 3210 posts 16985 karma points MVP 8x c-trib
    Jul 26, 2021 @ 12:39
    Matt Brailsford
    0

    And how are you attaching your pipeline task?

  • Matt Brailsford 3210 posts 16985 karma points MVP 8x c-trib
    Jul 26, 2021 @ 12:43
    Matt Brailsford
    100

    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.

    composition.WithSendEmailPipeline()
        .InsertBefore<SendSmtpEmailTask, AddCustomAttachmentTask>();
    
  • Michele Benolli 18 posts 126 karma points
    Jul 26, 2021 @ 13:17
    Michele Benolli
    1

    Oh, you guessed it. With your code, the attachment is added before and everything is working. Thank you!

Please Sign in or register to post replies

Write your reply to:

Draft