Beispiel #1
0
        //This method gets the email from the form and checks if sending a template email is enabled to be sent to them, and sends it
        internal void SendResponseToFormSender()
        {
            //Check if send response is enabled...
            bool isSendResponseEnabled = TypesHelper.DoConvert <bool>(GetParamValue("response.enabled", "false"));

            if (!isSendResponseEnabled)
            {
                return;
            }

            //...if there's a valid template for it...
            string templatePath = GetParamValue("response.template");

            if (string.IsNullOrEmpty(templatePath))
            {
                return;
            }
            templatePath = GetAbsolutePath(templatePath);
            if (!File.Exists(templatePath))
            {
                return;
            }

            //...and if there's a field named "email" in the form
            string userEmail = _ctx.Request.Form["email"];

            if (string.IsNullOrEmpty(userEmail))
            {
                return;
            }

            //Read template from disk
            string templateContents = IOHelper.ReadTextFromFile(templatePath);

            //Substitute placeholders for fields, if any
            templateContents = ReplacePlaceholders(templateContents);

            //Get the subject
            string subject = GetParamValue("response.subject", "Thanks for getting in touch");

            subject = ReplacePlaceholders(subject);

            //Send the final email
            Mailer mlr = new Mailer(this);

            mlr.SendMail(
                userEmail,
                subject,
                templateContents,
                true);
        }
Beispiel #2
0
        //Appends data line to the CSV file (if there's one)
        internal void AppendToCSVFile()
        {
            bool   csvEnabled = TypesHelper.DoConvert <bool>(GetParamValue("CSV.enabled", "false"));
            string csvPath    = GetParamValue("CSV.path", "");

            if (!csvEnabled || string.IsNullOrEmpty(csvPath))
            {
                return;
            }

            csvPath = GetAbsolutePath(csvPath);

            using (StreamWriter swCSV = new StreamWriter(csvPath, true))    //Open file to append data
            {
                using (CsvFileWriter csvFW = new CsvFileWriter(swCSV))
                {
                    csvFW.WriteRow(GetDataAsList(_data));
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Sends an email to the specified recipientes (comma separated string)
        /// with the specified body using the configuration in web.config.
        /// May raise exceptions if there are missing parameters in the configuration file.
        /// It doesn't check if there's a valid sender email address!! If it's not valid it will raise an exception.
        /// If the recipient email addresses are not valid emails it will ignore them
        /// </summary>
        /// <param name="recipients"></param>
        /// <param name="subj"></param>
        /// <param name="body"></param>
        /// <param name="isHTMLContent"></param>
        public void SendMail(string recipients, string subj, string body, bool isHTMLContent = false)
        {
            if (String.IsNullOrEmpty(body))
            {
                return;
            }

            //Email params
            string fromAddress = hlpr.GetParamValue("fromAddress"),
                   fromName    = hlpr.GetParamValue("fromName"),
                   toAddress   = recipients,
                   subject     = subj,
                   serverUser  = hlpr.GetParamValue("server.user"),
                   serverPwd   = hlpr.GetParamValue("server.password"),
                   serverHost  = hlpr.GetParamValue("server.host");
            int  serverPort    = TypesHelper.DoConvert <int>(hlpr.GetParamValue("server.port", "587"));
            bool serverSSL     = TypesHelper.DoConvert <bool>(hlpr.GetParamValue("server.SSL", "true"));

            //At least we'll need from, to and host to try to send the email
            if (fromAddress == "" || toAddress == "" || serverHost == "")
            {
                throw new ArgumentException("One of the needed configuration parameters is missing", "fromAddress, toAddress or server.host");
            }

            //Configure email contents
            MailMessage msg = new MailMessage
            {
                From = new MailAddress(fromAddress, fromName, System.Text.Encoding.UTF8)
            };

            string[] toAddresses = toAddress.Split(',');
            for (int i = 0; i < toAddresses.Length; i++)
            {
                //Catches invalid email addresses
                try
                {
                    msg.To.Add(toAddresses[i]);
                }
                catch { }
            }
            if (msg.To.Count == 0)  //If there are no valid email addresses in the To field, abort email sending
            {
                return;
            }

            msg.Subject         = subject;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body            = body;
            msg.BodyEncoding    = System.Text.Encoding.UTF8;
            msg.IsBodyHtml      = isHTMLContent;

            //Configure email server
            SmtpClient client = new SmtpClient();

            if (!string.IsNullOrEmpty(serverUser))  //If there's a user name for the server (could have an empty password)
            {
                client.Credentials = new NetworkCredential(serverUser, serverPwd);
            }

            client.Port      = serverPort;
            client.Host      = serverHost;
            client.EnableSsl = serverSSL;

            //Try to send the email
            client.Send(msg);
        }