private void PrepareTestRecipient(Guid contactId, string emailRecipient, BulkEmailMacroComposer macroComposer, EmailMessage emailMessage, IMailingTemplate template) { var emailAddresses = emailRecipient == null?Enumerable.Empty <EmailAddress>() : Regex .Split(emailRecipient, EmailSplitRegexPattern).Where(x => x.IsNotNullOrWhiteSpace()) .Select(x => new EmailAddress(Guid.NewGuid(), x.Trim())); if (!contactId.Equals(Guid.Empty)) { emailMessage.InitRecipientVariable(); foreach (var emailAddress in emailAddresses) { var macrosValues = macroComposer.GetContactMacrosValues(contactId, template.MacrosCollection); var mergeVars = CreateRecipientMergeVars(contactId, emailAddress.email, macrosValues); emailMessage.merge_vars.Add(mergeVars); } } emailMessage.to = emailAddresses; var cesTemplate = template as CESMailingTemplate; var recipientMergeVars = emailMessage.merge_vars.FirstOrDefault()?.vars; if (cesTemplate == null || recipientMergeVars == null) { return; } OverrideFromEmailHeader(emailMessage, cesTemplate, recipientMergeVars); }
private EmailMessage InitEmailMessage(SendMessageData messageData, BulkEmailMacroComposer macroComposer, IMailingTemplate template) { var bulkEmail = messageData.BulkEmail; string senderEmail; string senderName; if (template is IMailingTemplateWithHeaders templateReplica) { senderEmail = templateReplica.SenderEmail; senderName = templateReplica.SenderName; } else { senderEmail = bulkEmail.SenderEmail; senderName = bulkEmail.SenderName; } var message = new EmailMessage { subject = template.TemplateSubject, from_email = senderEmail, from_name = senderName, track_clicks = true, track_opens = true, images = messageData.Images, inline_css = true }; var globalMacros = macroComposer.GetCommonMacrosValues(template.MacrosCollection); var mergeVars = globalMacros.Select(macros => new merge_var { name = macros.Key, content = BulkEmailMacroParser.EncodeUrlMacrosValue(macros.Key, macros.Value) }).ToList(); if (!mergeVars.Any()) { return(message); } message.InitGlobalVariable(); message.global_merge_vars.AddRange(mergeVars); return(message); }
private bool SendTestMessageTemplate(Guid bulkEmailId, Guid contactId, string emailRecipient, IMailingTemplate template, SendMessageData messageData, BulkEmailMacroComposer macroComposer) { var templateContent = template.Content; if (templateContent?.IsEmpty() != false) { MailingUtilities.Log.InfoFormat("[SendTestMessage]: Can't save template"); return(false); } var emailMessage = InitEmailMessage(messageData, macroComposer, template); emailMessage.images = template.InlineImages.ToCESImage(); emailMessage.html = templateContent; emailMessage.tags = new List <string> { TestMessageTag }; PrepareTestRecipient(contactId, emailRecipient, macroComposer, emailMessage, template); if (!Validator.ValidateTestMessage(bulkEmailId, contactId, template, emailMessage)) { return(false); } try { ServiceApi.SendMessage(emailMessage, bulkEmailId); MailingUtilities.Log.InfoFormat("[SendTestMessage]: Can't save template"); return(true); } catch (Exception e) { MailingUtilities.Log.ErrorFormat("[SendTestMessage]: Error while sending message", e); return(false); } }