Ejemplo n.º 1
0
        /// <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());
        }
Ejemplo n.º 2
0
        /// <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."
                    }
                });
            }
        }