Posted on 31/03/2020 16:52:21
Hi again, and thanks!
That certainly would be a way of doing it, however I ended implementing it in another way, properly not the easiest way but it works. Here is what I did:
The mail I had to edit the subject for was the “Others” Email, and I had to use the list of emails that was specified in the order flow.
I made a comment in the email template
topLeft.text.Add("<!--FieldCountry: " + GetString("Ecom:Order.Delivery.Country") + " -->");
And then I used the OnBeforeEmailSend subscriber to look for the comment, and edit the subject there using regex.
Here is my code for the OnBeforeEmail Send Subscriber:
[Subscribe(EmailNotifications.OnBeforeEmailSend)]
public class EcomCartSendingConfirmationMailObserver : NotificationSubscriber
{
public override void OnNotify(string notification, NotificationArgs args)
{
EmailSendNotificationArgs mailingArgs = args as EmailSendNotificationArgs;
try
{
var regexForCountry = new Regex(".*<!--FieldCountry: (.*) -->.*");
if (regexForCountry.IsMatch(mailingArgs.Message.Body))
{
var capturedCountry = regexForCountry.Match(mailingArgs.Message.Body).Groups[1];
if (!string.IsNullOrWhiteSpace(capturedCountry.ToString()))
{
mailingArgs.Message.Subject += $" - Country: {getCountryFromCode(capturedCountry.ToString())} ";
}
}
else
{
LogManager.System.GetLogger(LogCategory.Health, "Backend.OnBeforeEmailSend").Info($"Could not find Country");
}
}
catch(Exception e)
{
LogManager.System.GetLogger(LogCategory.Health, "Backend.OnBeforeEmailSend").Error($"Error when Finding Country in Email = {e.Message}");
}
Not the cleanest way, I know. Though I would share it anyways.