private RockEmailMessage GetTemplateRockEmailMessage(RockMessage rockMessage, Dictionary <string, object> mergeFields, GlobalAttributesCache globalAttributes) { var templateRockEmailMessage = new RockEmailMessage(); var emailMessage = rockMessage as RockEmailMessage; if (emailMessage == null) { return(null); } templateRockEmailMessage.AppRoot = emailMessage.AppRoot; templateRockEmailMessage.CurrentPerson = emailMessage.CurrentPerson; templateRockEmailMessage.EnabledLavaCommands = emailMessage.EnabledLavaCommands; templateRockEmailMessage.CssInliningEnabled = emailMessage.CssInliningEnabled; templateRockEmailMessage.ReplyToEmail = emailMessage.ReplyToEmail; var fromAddress = GetFromAddress(emailMessage, mergeFields, globalAttributes); var fromName = GetFromName(emailMessage, mergeFields, globalAttributes); if (fromAddress.IsNullOrWhiteSpace()) { return(null); } templateRockEmailMessage.FromEmail = fromAddress; templateRockEmailMessage.FromName = fromName; // CC templateRockEmailMessage.CCEmails = emailMessage.CCEmails; // BCC templateRockEmailMessage.BCCEmails = emailMessage.BCCEmails; templateRockEmailMessage.Subject = emailMessage.Subject; templateRockEmailMessage.Message = emailMessage.Message; templateRockEmailMessage.PlainTextMessage = emailMessage.PlainTextMessage; // Attachments if (emailMessage.Attachments.Any()) { using (var rockContext = new RockContext()) { var binaryFileService = new BinaryFileService(rockContext); foreach (var binaryFileId in emailMessage.Attachments.Where(a => a != null).Select(a => a.Id)) { var attachment = binaryFileService.Get(binaryFileId); // We need to call content stream to make sure it is loaded while we have the rock context. var attachmentString = attachment.ContentStream; templateRockEmailMessage.Attachments.Add(attachment); } } } // Communication record for tracking opens & clicks templateRockEmailMessage.MessageMetaData = emailMessage.MessageMetaData; return(templateRockEmailMessage); }
/// <summary> /// Sends the asynchronous. /// </summary> /// <param name="rockMessage">The rock message.</param> /// <returns></returns> public virtual async Task <SendMessageResult> SendAsync(RockMessage rockMessage) { if (this.IsActive) { // Get the Medium's Entity Type Id int mediumEntityTypeId = EntityTypeCache.Get(this.GetType()).Id; // Add the Medium's settings as attributes for the Transport to use. var mediumAttributes = GetMediumAttributes(); // If there have not been any EnabledLavaCommands explicitly set, then use the global defaults. if (rockMessage.EnabledLavaCommands == null) { rockMessage.EnabledLavaCommands = GlobalAttributesCache.Get().GetValue("DefaultEnabledLavaCommands"); } // Use the transport to send communication var transport = Transport; if (transport != null && transport.IsActive) { var asyncTransport = transport as IAsyncTransport; if (asyncTransport == null) { var messageResult = new SendMessageResult { MessagesSent = transport.Send(rockMessage, mediumEntityTypeId, mediumAttributes, out var errorMessage) ? rockMessage.GetRecipients().Count : 0 }; return(await Task.FromResult(messageResult)); } else { return(await asyncTransport.SendAsync(rockMessage, mediumEntityTypeId, mediumAttributes).ConfigureAwait(false)); } } else { return(new SendMessageResult { Errors = new List <string> { "Invalid or Inactive Transport." } }); } } else { return(new SendMessageResult { Errors = new List <string> { "Inactive Medium." } }); } }
/// <summary> /// Sends the specified rock message. /// </summary> /// <param name="rockMessage">The rock message.</param> /// <param name="mediumEntityTypeId">The medium entity type identifier.</param> /// <param name="mediumAttributes">The medium attributes.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> public override bool Send(RockMessage rockMessage, int mediumEntityTypeId, Dictionary <string, string> mediumAttributes, out List <string> errorMessages) { errorMessages = new List <string>(); var emailMessage = rockMessage as RockEmailMessage; if (emailMessage == null) { return(false); } var mergeFields = GetAllMergeFields(rockMessage.CurrentPerson, rockMessage.AdditionalMergeFields); var globalAttributes = GlobalAttributesCache.Get(); var fromAddress = GetFromAddress(emailMessage, mergeFields, globalAttributes); if (fromAddress.IsNullOrWhiteSpace()) { errorMessages.Add("A From address was not provided."); return(false); } var templateMailMessage = GetTemplateRockEmailMessage(emailMessage, mergeFields, globalAttributes); var organizationEmail = globalAttributes.GetValue("OrganizationEmail"); foreach (var rockMessageRecipient in rockMessage.GetRecipients()) { try { var recipientEmailMessage = GetRecipientRockEmailMessage(templateMailMessage, rockMessageRecipient, mergeFields, organizationEmail); var result = SendEmail(recipientEmailMessage); // Create the communication record if (recipientEmailMessage.CreateCommunicationRecord) { var transaction = new SaveCommunicationTransaction(rockMessageRecipient, recipientEmailMessage.FromName, recipientEmailMessage.FromEmail, recipientEmailMessage.Subject, recipientEmailMessage.Message); transaction.RecipientGuid = recipientEmailMessage.MessageMetaData["communication_recipient_guid"].AsGuidOrNull(); RockQueue.TransactionQueue.Enqueue(transaction); } } catch (Exception ex) { errorMessages.Add(ex.Message); ExceptionLogService.LogException(ex); } } return(!errorMessages.Any()); }
/// <summary> /// Sends the specified rock message. /// </summary> /// <param name="rockMessage">The rock message.</param> /// <param name="errorMessages">The error messages.</param> public virtual void Send(RockMessage rockMessage, out List <string> errorMessages) { if (this.IsActive) { // Get the Medium's Entity Type Id int mediumEntityTypeId = EntityTypeCache.Get(this.GetType()).Id; // Add the Medium's settings as attributes for the Transport to use. var mediumAttributes = new Dictionary <string, string>(); foreach (var attr in this.Attributes.Select(a => a.Value)) { string value = this.GetAttributeValue(attr.Key); if (value.IsNotNullOrWhiteSpace()) { mediumAttributes.Add(attr.Key, GetAttributeValue(attr.Key)); } } // If there have not been any EnabledLavaCommands explicitly set, then use the global defaults. if (rockMessage.EnabledLavaCommands == null) { rockMessage.EnabledLavaCommands = GlobalAttributesCache.Get().GetValue("DefaultEnabledLavaCommands"); } // Use the transport to send communication var transport = Transport; if (transport != null && transport.IsActive) { transport.Send(rockMessage, mediumEntityTypeId, mediumAttributes, out errorMessages); } else { errorMessages = new List <string> { "Invalid or Inactive Transport." }; } } else { errorMessages = new List <string> { "Inactive Medium." }; } }
/// <summary> /// Sends the specified rock message. /// </summary> /// <param name="rockMessage">The rock message.</param> /// <param name="errorMessages">The error messages.</param> public virtual void Send(RockMessage rockMessage, out List <string> errorMessages) { if (this.IsActive) { // Get the Medium's Entity Type Id int mediumEntityTypeId = EntityTypeCache.Get(this.GetType()).Id; // Add the Medium's settings as attributes for the Transport to use. var mediumAttributes = GetMediumAttributes(); // If there have not been any EnabledLavaCommands explicitly set, then use the global defaults. if (rockMessage.EnabledLavaCommands == null) { rockMessage.EnabledLavaCommands = GlobalAttributesCache.Get().GetValue("DefaultEnabledLavaCommands"); } if (rockMessage.CurrentPerson == null) { rockMessage.CurrentPerson = HttpContext.Current?.Items["CurrentPerson"] as Person; } // Use the transport to send communication var transport = Transport; if (transport != null && transport.IsActive) { transport.Send(rockMessage, mediumEntityTypeId, mediumAttributes, out errorMessages); } else { errorMessages = new List <string> { "Invalid or Inactive Transport." }; } } else { errorMessages = new List <string> { "Inactive Medium." }; } }
/// <summary> /// Sends the specified rock message. /// </summary> /// <param name="rockMessage">The rock message.</param> /// <param name="errorMessages">The error messages.</param> public virtual void Send(RockMessage rockMessage, out List <string> errorMessages) { if (this.IsActive) { // Get the Medium's Entity Type Id int mediumEntityTypeId = EntityTypeCache.Read(this.GetType()).Id; // Add the Medium's settings as attributes for the Transport to use. var mediumAttributes = new Dictionary <string, string>(); foreach (var attr in this.Attributes.Select(a => a.Value)) { string value = this.GetAttributeValue(attr.Key); if (value.IsNotNullOrWhitespace()) { mediumAttributes.Add(attr.Key, GetAttributeValue(attr.Key)); } } // Use the transport to send communication var transport = Transport; if (transport != null && transport.IsActive) { transport.Send(rockMessage, mediumEntityTypeId, mediumAttributes, out errorMessages); } else { errorMessages = new List <string> { "Invalid or Inactive Transport." }; } } else { errorMessages = new List <string> { "Inactive Medium." }; } }
/// <summary> /// Sends the specified rock message. /// </summary> /// <param name="rockMessage">The rock message.</param> /// <param name="mediumEntityTypeId">The medium entity type identifier.</param> /// <param name="mediumAttributes">The medium attributes.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> public abstract bool Send(RockMessage rockMessage, int mediumEntityTypeId, Dictionary <string, string> mediumAttributes, out List <string> errorMessages);