예제 #1
0
        /// <summary>
        /// Checks to make sure the sender's email address domain is one from the
        /// SystemGuid.DefinedType.COMMUNICATION_SAFE_SENDER_DOMAINS.  If it is not
        /// it will replace the From address with the one defined by the OrganizationEmail
        /// global attribute.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="globalAttributes">The global attributes.</param>
        private void CheckSafeSender(MailMessage message, GlobalAttributesCache globalAttributes)
        {
            if (message != null && message.From != null)
            {
                string from     = message.From.Address;
                string fromName = message.From.DisplayName;

                // Check to make sure sending domain is a safe sender
                var safeDomains = DefinedTypeCache.Read(SystemGuid.DefinedType.COMMUNICATION_SAFE_SENDER_DOMAINS.AsGuid()).DefinedValues.Select(v => v.Value).ToList();
                var emailParts  = from.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
                if (emailParts.Length != 2 || !safeDomains.Contains(emailParts[1], StringComparer.OrdinalIgnoreCase))
                {
                    string orgEmail = globalAttributes.GetValue("OrganizationEmail");
                    if (!string.IsNullOrWhiteSpace(orgEmail) && !orgEmail.Equals(from, StringComparison.OrdinalIgnoreCase))
                    {
                        message.From = new MailAddress(orgEmail);

                        bool addReplyTo = true;
                        foreach (var replyTo in message.ReplyToList)
                        {
                            if (replyTo.Address.Equals(from, StringComparison.OrdinalIgnoreCase))
                            {
                                addReplyTo = false;
                                break;
                            }
                        }

                        if (addReplyTo)
                        {
                            message.ReplyToList.Add(new MailAddress(from, fromName));
                        }
                    }
                }
            }
        }
예제 #2
0
        private string GetFromName(RockEmailMessage emailMessage, Dictionary <string, object> mergeFields, GlobalAttributesCache globalAttributes)
        {
            string fromName = emailMessage.FromName.ResolveMergeFields(mergeFields, emailMessage.CurrentPerson, emailMessage.EnabledLavaCommands);

            fromName = fromName.IsNullOrWhiteSpace() ? globalAttributes.GetValue("OrganizationName") : fromName;
            return(fromName);
        }
예제 #3
0
        private string GetFromAddress(RockEmailMessage emailMessage, Dictionary <string, object> mergeFields, GlobalAttributesCache globalAttributes)
        {
            // Resolve any possible merge fields in the from address
            string fromAddress = emailMessage.FromEmail.ResolveMergeFields(mergeFields, emailMessage.CurrentPerson, emailMessage.EnabledLavaCommands);

            fromAddress = fromAddress.IsNullOrWhiteSpace() ? globalAttributes.GetValue("OrganizationEmail") : fromAddress;
            return(fromAddress);
        }
예제 #4
0
        private RockEmailMessage GetTemplateRockEmailMessage(Model.Communication communication, Dictionary <string, object> mergeFields, GlobalAttributesCache globalAttributes)
        {
            var resultEmailMessage = new RockEmailMessage();

            var publicAppRoot      = globalAttributes.GetValue("PublicApplicationRoot").EnsureTrailingForwardslash();
            var cssInliningEnabled = communication.CommunicationTemplate?.CssInliningEnabled ?? false;

            resultEmailMessage.AppRoot             = publicAppRoot;
            resultEmailMessage.CssInliningEnabled  = cssInliningEnabled;
            resultEmailMessage.CurrentPerson       = communication.CreatedByPersonAlias?.Person;
            resultEmailMessage.EnabledLavaCommands = communication.EnabledLavaCommands;
            resultEmailMessage.FromEmail           = communication.FromEmail;
            resultEmailMessage.FromName            = communication.FromName;

            var fromAddress = GetFromAddress(resultEmailMessage, mergeFields, globalAttributes);
            var fromName    = GetFromName(resultEmailMessage, mergeFields, globalAttributes);

            resultEmailMessage.FromEmail = fromAddress;
            resultEmailMessage.FromName  = fromName;

            // Reply To
            var replyToEmail = "";

            if (communication.ReplyToEmail.IsNotNullOrWhiteSpace())
            {
                // Resolve any possible merge fields in the replyTo address
                replyToEmail = communication.ReplyToEmail.ResolveMergeFields(mergeFields, resultEmailMessage.CurrentPerson);
            }
            resultEmailMessage.ReplyToEmail = replyToEmail;

            // Attachments
            resultEmailMessage.Attachments = communication.GetAttachments(CommunicationType.Email).Select(a => a.BinaryFile).ToList();
            // Load up the content stream while the context is still active.
            for (int i = 0; i < resultEmailMessage.Attachments.Count; i++)
            {
                var _ = resultEmailMessage.Attachments[i].ContentStream;
            }

            return(resultEmailMessage);
        }
예제 #5
0
        /// <summary>
        /// Loads the credentials.
        /// </summary>
        /// <param name="context">The context.</param>
        private void LoadCredentials(RockContext context)
        {
            ClearProperties();
            GlobalAttributesCache cache = GlobalAttributesCache.Get();

            ServerUrl              = cache.GetValue(SERVER_URL_KEY, context);
            ReportPath             = cache.GetValue(SERVER_ROOT_PATH_KEY, context);
            ContentManagerUser     = cache.GetValue(CONTENT_MANAGER_USER_KEY, context);
            ContentManagerPassword = Encryption.DecryptString(cache.GetValue(CONTENT_MANAGER_PWD_KEY, context));
            BrowserUser            = cache.GetValue(BROWSER_USER_KEY, context);
            BrowserPassword        = Encryption.DecryptString(cache.GetValue(BROWSER_PWD_KEY, context));

            if (!String.IsNullOrWhiteSpace(ServerUrl) && !String.IsNullOrWhiteSpace(ReportPath) && !String.IsNullOrWhiteSpace(BrowserUser) && !String.IsNullOrWhiteSpace(BrowserPassword))
            {
                CredentialsStored = true;
            }
        }
예제 #6
0
        /// <summary>
        /// Checks to make sure the sender's email address domain is one from the
        /// SystemGuid.DefinedType.COMMUNICATION_SAFE_SENDER_DOMAINS.  If it is not
        /// it will replace the From address with the one defined by the OrganizationEmail
        /// global attribute.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="globalAttributes">The global attributes.</param>
        private void CheckSafeSender(MailMessage message, GlobalAttributesCache globalAttributes)
        {
            if (message != null && message.From != null)
            {
                string from     = message.From.Address;
                string fromName = message.From.DisplayName;

                // Get the safe sender domains
                var safeDomainValues = DefinedTypeCache.Get(SystemGuid.DefinedType.COMMUNICATION_SAFE_SENDER_DOMAINS.AsGuid()).DefinedValues;
                var safeDomains      = safeDomainValues.Select(v => v.Value).ToList();

                // Check to make sure the From email domain is a safe sender
                var fromParts = from.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
                if (fromParts.Length != 2 || !safeDomains.Contains(fromParts[1], StringComparer.OrdinalIgnoreCase))
                {
                    // The sending email address is not a safe sender domain, but check to see if all the recipients have a domain
                    // that does not require a safe sender domain
                    bool unsafeToDomain = false;
                    foreach (var to in message.To)
                    {
                        bool safe    = false;
                        var  toParts = to.Address.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
                        if (toParts.Length == 2 && safeDomains.Contains(toParts[1], StringComparer.OrdinalIgnoreCase))
                        {
                            var domain = safeDomainValues.FirstOrDefault(dv => dv.Value.Equals(toParts[1], StringComparison.OrdinalIgnoreCase));
                            safe = domain != null && domain.GetAttributeValue("SafeToSendTo").AsBoolean();
                        }

                        if (!safe)
                        {
                            unsafeToDomain = true;
                            break;
                        }
                    }

                    if (unsafeToDomain)
                    {
                        string orgEmail = globalAttributes.GetValue("OrganizationEmail");
                        if (!string.IsNullOrWhiteSpace(orgEmail) && !orgEmail.Equals(from, StringComparison.OrdinalIgnoreCase))
                        {
                            message.From = new MailAddress(orgEmail, fromName);

                            bool addReplyTo = true;
                            foreach (var replyTo in message.ReplyToList)
                            {
                                if (replyTo.Address.Equals(from, StringComparison.OrdinalIgnoreCase))
                                {
                                    addReplyTo = false;
                                    break;
                                }
                            }

                            if (addReplyTo)
                            {
                                message.ReplyToList.Add(new MailAddress(from, fromName));
                            }
                        }
                    }
                }
            }
        }