예제 #1
0
        public static bool Mail(string to, string subject, string message, string additionalHeaders)
        {
            // to and subject cannot contain newlines, replace with spaces
            to      = (to != null) ? to.Replace("\r\n", " ").Replace('\n', ' ') : "";
            subject = (subject != null) ? subject.Replace("\r\n", " ").Replace('\n', ' ') : "";

            Debug.WriteLine("MAILER", "mail('{0}','{1}','{2}','{3}')", to, subject, message, additionalHeaders);

            // get current configuration, we need some fields for mailing
            LibraryConfiguration config = LibraryConfiguration.Local;

            // set SMTP server we are using
            RawSmtpClient client = new RawSmtpClient(config.Mailer.SmtpServer, config.Mailer.SmtpPort);

            // X-PHP-Originating-Script
            if (config.Mailer.AddXHeader)
            {
                additionalHeaders = "X-PHP-Originating-Script: 1:" + ScriptContext.CurrentContext.MainScriptFile.RelativePath.Path + "\n" + additionalHeaders;
            }

            try
            {
                client.Connect();
                client.SendMessage(
                    config.Mailer.DefaultFromHeader, to,
                    subject,
                    additionalHeaders,
                    message);
                return(true);
            }
            catch (Exception e)
            {
                string    error_message = e.Message;
                Exception inner         = e;
                while ((inner = inner.InnerException) != null)
                {
                    error_message += "; " + inner.Message;
                }

                PhpException.Throw(PhpError.Warning, LibResources.GetString("cannot_send_email", error_message)
#if DEBUG
                                   + "\n\n" + e.StackTrace
#endif
                                   );
                return(false);
            }
            finally
            {
                client.Disconnect();
            }
        }
예제 #2
0
        public static bool mail(Context ctx, string to, string subject, string message, string additional_headers = null, string additional_parameters = null)
        {
            // to and subject cannot contain newlines, replace with spaces
            to      = (to != null) ? to.Replace("\r\n", " ").Replace('\n', ' ') : "";
            subject = (subject != null) ? subject.Replace("\r\n", " ").Replace('\n', ' ') : "";

            Debug.WriteLine("MAILER", "mail('{0}','{1}','{2}','{3}')", to, subject, message, additional_headers);

            var config = ctx.Configuration.Core;

            // additional_parameters
            additional_parameters = config.ForceExtraMailParameters ?? additional_parameters;

            // set SMTP server we are using
            var client = new RawSmtpClient(config.SmtpServer, config.SmtpPort);

            // X-PHP-Originating-Script
            if (config.AddXHeader)
            {
                additional_headers = "X-PHP-Originating-Script: 1:" + ctx.MainScriptFile.Path + "\n" + additional_headers;
            }

            try
            {
                client.Connect();
                client.SendMessage(
                    config.DefaultFromHeader, to,
                    subject,
                    additional_headers,
                    message);
                return(true);
            }
            catch (Exception e)
            {
                string    error_message = e.Message;
                Exception inner         = e;
                while ((inner = inner.InnerException) != null)
                {
                    error_message += "; " + inner.Message;
                }

                PhpException.Throw(PhpError.Warning, Resources.LibResources.cannot_send_email, error_message);
                return(false);
            }
            finally
            {
                client.Disconnect();
            }
        }
예제 #3
0
        public static bool Mail(string to, string subject, string message, string additionalHeaders)
		{
			// to and subject cannot contain newlines, replace with spaces
			to = (to != null) ? to.Replace("\r\n", " ").Replace('\n', ' ') : "";
			subject = (subject != null) ? subject.Replace("\r\n", " ").Replace('\n', ' ') : "";

			Debug.WriteLine("MAILER", "mail('{0}','{1}','{2}','{3}')", to, subject, message, additionalHeaders);

			// get current configuration, we need some fields for mailing
			LibraryConfiguration config = LibraryConfiguration.Local;

			// set SMTP server we are using
            RawSmtpClient client = new RawSmtpClient(config.Mailer.SmtpServer, config.Mailer.SmtpPort);

            // X-PHP-Originating-Script
            if (config.Mailer.AddXHeader)
                additionalHeaders = "X-PHP-Originating-Script: 1:" + ScriptContext.CurrentContext.MainScriptFile.RelativePath.Path + "\n" + additionalHeaders;

            try
            {
                client.Connect();
                client.SendMessage(
                    config.Mailer.DefaultFromHeader, to,
                    subject,
                    additionalHeaders,
                    message);
                return true;
            }
            catch (Exception e)
            {
                string error_message = e.Message;
                Exception inner = e;
                while ((inner = inner.InnerException) != null)
                    error_message += "; " + inner.Message;

                PhpException.Throw(PhpError.Warning, LibResources.GetString("cannot_send_email", error_message)
#if DEBUG
                    + "\n\n" + e.StackTrace
#endif
                    );
                return false;
            }
            finally
            { 
                client.Disconnect(); 
            }
		}