Skip to content
Snippets Groups Projects
Commit 74174dc7 authored by Jan Mach's avatar Jan Mach
Browse files

Support for sending emails in development via SMTP.

(Redmine issue: #7041)
parent 839bbbca
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,9 @@
"mail_return_path": "root",
"mail_admin": "root",
"mail_test_mode": true,
"mail_dev_mode": false,
"mail_dev_server": "localhost",
"mail_dev_port": 1025,
#---------------------------------------------------------------------------
# Common useful piper daemon configurations
......
......@@ -98,6 +98,24 @@
#
#"mail_test_mode": false,
# Send emails in development mode (flag). Will use localhost:1025 as mail server.
# default: false (set in core/common.json.conf)
# type: bool
#
#"mail_dev_mode": true,
# Hostname for development SMTP server.
# default: "localhost" (set in core/common.json.conf)
# type: string
#
#"mail_dev_server": "localhost",
# Port for development SMTP server.
# default: 1025 (set in core/common.json.conf)
# type: integer
#
#"mail_dev_port": 1025,
#---------------------------------------------------------------------------
# Common script configurations
#---------------------------------------------------------------------------
......
......@@ -132,6 +132,24 @@
#
#"mail_test_mode": false,
# Send emails in development mode (flag). Will use localhost:1025 as mail server.
# default: false (set in core/common.json.conf)
# type: bool
#
#"mail_dev_mode": true,
# Hostname for development SMTP server.
# default: "localhost" (set in core/common.json.conf)
# type: string
#
#"mail_dev_server": "localhost",
# Port for development SMTP server.
# default: 1025 (set in core/common.json.conf)
# type: integer
#
#"mail_dev_port": 1025,
#---------------------------------------------------------------------------
# Common script configurations
#---------------------------------------------------------------------------
......
......@@ -32,6 +32,7 @@ __author__ = "Jan Mach <jan.mach@cesnet.cz>"
__credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea.kropacova@cesnet.cz>"
import smtplib
import weakref
from subprocess import Popen, PIPE
......@@ -61,6 +62,9 @@ class MailerPlugin(pyzenkit.baseapp.ZenAppPlugin):
CONFIG_MAIL_RETURN_PATH = 'mail_return_path'
CONFIG_MAIL_ADMIN = 'mail_admin'
CONFIG_MAIL_TEST_MODE = 'mail_test_mode'
CONFIG_MAIL_DEV_MODE = 'mail_dev_mode'
CONFIG_MAIL_DEV_SERVER = 'mail_dev_server'
CONFIG_MAIL_DEV_PORT = 'mail_dev_port'
def __init__(self, settings = None):
"""
......@@ -93,8 +97,11 @@ class MailerPlugin(pyzenkit.baseapp.ZenAppPlugin):
arggroup_plugin.add_argument('--mail-reply-to', type = str, default = None, help = 'reply to email address (repeatable)', action='append')
arggroup_plugin.add_argument('--mail-return-path', type = str, default = None, help = 'return address for undeliverable emails')
arggroup_plugin.add_argument('--mail-admin', type = str, default = None, help = 'admin email address (repeatable)', action='append')
arggroup_plugin.add_argument('--mail-dev-server', type = str, default = None, help = 'development SMTP server hostname')
arggroup_plugin.add_argument('--mail-dev-port', type = int, default = None, help = 'development SMTP server port')
arggroup_plugin.add_argument('--mail-test-mode', help = 'send emails in test mode (flag)', action = 'store_true', default = None)
arggroup_plugin.add_argument('--mail-dev-mode', help = 'send emails in development mode (flag)', action = 'store_true', default = None)
return argparser
......@@ -111,7 +118,10 @@ class MailerPlugin(pyzenkit.baseapp.ZenAppPlugin):
self.CONFIG_MAIL_REPLY_TO: None,
self.CONFIG_MAIL_RETURN_PATH: None,
self.CONFIG_MAIL_ADMIN: 'root',
self.CONFIG_MAIL_TEST_MODE: False
self.CONFIG_MAIL_TEST_MODE: False,
self.CONFIG_MAIL_DEV_MODE: False,
self.CONFIG_MAIL_DEV_SERVER: None,
self.CONFIG_MAIL_DEV_PORT: None
})
return config
......@@ -155,6 +165,22 @@ class MailerPlugin(pyzenkit.baseapp.ZenAppPlugin):
with Popen(["/usr/sbin/sendmail", "-t", "-oi", "-f", envelope_from], stdin=PIPE) as proc:
proc.communicate(bytes(email.as_string(), 'UTF-8'))
@staticmethod
def mail_smtplib(email):
"""
Send given email via smtplib.
:param mentat.emails.base.BaseEmail email: Email object.
"""
envelope_from = email.get_header(
'return-path',
email.get_header('from')
)
s = self.get_application().c(self.CONFIG_MAIL_DEV_SERVER)
p = self.get_application().c(self.CONFIG_MAIL_DEV_PORT)
with smtplib.SMTP(s, p) as smtp:
smtp.send_message(email, from_addr = envelope_from)
#---------------------------------------------------------------------------
def email_send(self, email_class, email_headers, email_params, flag_redirect = False):
......@@ -214,6 +240,10 @@ class MailerPlugin(pyzenkit.baseapp.ZenAppPlugin):
self.get_application().logger.info("Sending email: '{}'".format(str(email_headers)))
msg = email_class(email_headers, **email_params)
self.mail_sendmail(msg)
if self.get_application().c(self.CONFIG_MAIL_DEV_MODE):
self.mail_smtplib(msg)
else
self.mail_sendmail(msg)
return msg
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment