Developer forum

Forum » Ecommerce - Standard features » Attach dynamic file to order/quote confirmation

Attach dynamic file to order/quote confirmation

Marie Louise Veigert
Reply

Hi,

We have a customer who would like the customer to get their quote attached as an csv in the email recieved after offfer have been made.
I can only see an option to attach a static file to the email. 

Its a DW version 9.18.2

Have anyone done this before ? :) 

BR
Marie Louise


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

You’re right that the standard setup only supports attaching a static file.

If the customer needs a dynamic CSV generated per quote/order, this has to be done in code with a notification subscriber listening to SendingConfirmationMail:

Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.SendingConfirmationMail

That notification gives you access to the order/quote and the MailMessage, so you can generate a CSV on the fly and attach it before the email is sent.

This notification is also fired when you check out to quote.

Docs:
https://doc.dynamicweb.com/apix/api/Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.SendingConfirmationMailArgs.html

Example to attach a CSV:

using System.IO;
using System.Net.Mail;
using System.Text;
using Dynamicweb.Core;

namespace Dynamicweb.Ecommerce.Examples.Notifications
{
    [Dynamicweb.Extensibility.Notifications.Subscribe(
        Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.SendingConfirmationMail)]
    public class EcomCartSendingConfirmationMailObserver 
        : Dynamicweb.Extensibility.Notifications.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.Notifications.NotificationArgs args)
        {
            var mailArgs = args as Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.SendingConfirmationMailArgs;
            if (mailArgs?.Order == null || mailArgs.MailMessage == null)
                return;

            var order = mailArgs.Order;

            // Create CSV content
            var csv = new StringBuilder();
            csv.AppendLine("OrderId,CustomerName,CustomerEmail,ProductNumber,ProductName,Quantity,Price");

            foreach (var line in order.OrderLines)
            {
                csv.AppendLine(string.Join(",",
                    Escape(order.Id),
                    Escape(order.CustomerName),
                    Escape(order.CustomerEMail),
                    Escape(line.ProductNumber),
                    Escape(line.ProductName),
                    Escape(line.Quantity.ToString()),
                    Escape(line.Price.Price.ToString(System.Globalization.CultureInfo.InvariantCulture))
                ));
            }

            // Save to a temporary file
            var folder = SystemInformation.MapPath("/Files/System/OrderExport/");
            Directory.CreateDirectory(folder);

            var filePath = Path.Combine(folder, $"Order{order.Id}.csv");
            File.WriteAllText(filePath, csv.ToString(), Encoding.UTF8);

            // Attach CSV to outgoing email
            mailArgs.MailMessage.Attachments.Add(new Attachment(filePath));
        }

        private static string Escape(string value)
        {
            if (string.IsNullOrEmpty(value))
                return "\"\"";

            return "\"" + value.Replace("\"", "\"\"") + "\"";
        }
    }
}
Votes for this answer: 1
 
Marie Louise Veigert
Reply

Thanks - I will try something like that!

 

You must be logged in to post in the forum