simplify mailer

pull/6/head
Yax 3 years ago
parent 7f2ff74ebe
commit 185641e6d0

@ -19,19 +19,9 @@ port = 8100
proto = https proto = https
file = comments.xml file = comments.xml
[imap]
polling = 120
host = mail.gandi.net
ssl = false
port = 993
login = blog@mydomain.com
password = MYPASSWORD
[smtp] [smtp]
host = mail.gandi.net host = smtp.mail.com
starttls = true port = 465
ssl = false
port = 587
login = blog@mydomain.com login = blog@mydomain.com
password = MYPASSWORD password = MYPASSWORD

@ -74,24 +74,16 @@ def stacosys_server(config_pathname):
mailer = Mailer( mailer = Mailer(
conf.get(ConfigParameter.SMTP_HOST), conf.get(ConfigParameter.SMTP_HOST),
conf.get_int(ConfigParameter.SMTP_PORT), conf.get_int(ConfigParameter.SMTP_PORT),
conf.get_bool(ConfigParameter.SMTP_STARTTLS),
conf.get_bool(ConfigParameter.SMTP_SSL),
conf.get(ConfigParameter.SMTP_LOGIN), conf.get(ConfigParameter.SMTP_LOGIN),
conf.get(ConfigParameter.SMTP_PASSWORD), conf.get(ConfigParameter.SMTP_PASSWORD),
conf.get(ConfigParameter.SITE_ADMIN_EMAIL) conf.get(ConfigParameter.SITE_ADMIN_EMAIL)
) )
# configure mailer logger
mail_handler = mailer.get_error_handler()
logger.addHandler(mail_handler)
app.logger.addHandler(mail_handler)
# configure scheduler # configure scheduler
conf.put(ConfigParameter.SITE_TOKEN, hashlib.sha1(conf.get(ConfigParameter.SITE_NAME).encode('utf-8')).hexdigest()) conf.put(ConfigParameter.SITE_TOKEN, hashlib.sha1(conf.get(ConfigParameter.SITE_NAME).encode('utf-8')).hexdigest())
scheduler.configure( scheduler.configure(
conf.get_int(ConfigParameter.COMMENT_POLLING), conf.get_int(ConfigParameter.COMMENT_POLLING),
conf.get(ConfigParameter.SITE_NAME), conf.get(ConfigParameter.SITE_NAME),
conf.get(ConfigParameter.SITE_ADMIN_EMAIL),
mailer, mailer,
) )

@ -17,8 +17,6 @@ class ConfigParameter(Enum):
RSS_PROTO = "rss.proto" RSS_PROTO = "rss.proto"
RSS_FILE = "rss.file" RSS_FILE = "rss.file"
SMTP_STARTTLS = "smtp.starttls"
SMTP_SSL = "smtp.ssl"
SMTP_HOST = "smtp.host" SMTP_HOST = "smtp.host"
SMTP_PORT = "smtp.port" SMTP_PORT = "smtp.port"
SMTP_LOGIN = "smtp.login" SMTP_LOGIN = "smtp.login"

@ -8,7 +8,7 @@ from stacosys.db import dao
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def submit_new_comment(site_name, site_admin_email, mailer): def submit_new_comment(site_name, mailer):
for comment in dao.find_not_notified_comments(): for comment in dao.find_not_notified_comments():
comment_list = ( comment_list = (
"author: %s" % comment.author_name, "author: %s" % comment.author_name,
@ -23,7 +23,7 @@ def submit_new_comment(site_name, site_admin_email, mailer):
# send email to notify admin # send email to notify admin
subject = "STACOSYS %s" % site_name subject = "STACOSYS %s" % site_name
if mailer.send(site_admin_email, subject, email_body): if mailer.send(subject, email_body):
logger.debug("new comment processed ") logger.debug("new comment processed ")
# save notification datetime # save notification datetime

@ -3,10 +3,9 @@
import logging import logging
import smtplib import smtplib
import email.utils import ssl
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.message import EmailMessage
from logging.handlers import SMTPHandler
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -16,96 +15,28 @@ class Mailer:
self, self,
smtp_host, smtp_host,
smtp_port, smtp_port,
smtp_starttls,
smtp_ssl,
smtp_login, smtp_login,
smtp_password, smtp_password,
site_admin_email, site_admin_email,
): ):
self._smtp_host = smtp_host self._smtp_host = smtp_host
self._smtp_port = smtp_port self._smtp_port = smtp_port
self._smtp_starttls = smtp_starttls
self._smtp_ssl = smtp_ssl
self._smtp_login = smtp_login self._smtp_login = smtp_login
self._smtp_password = smtp_password self._smtp_password = smtp_password
self._site_admin_email = site_admin_email self._site_admin_email = site_admin_email
def send(self, to_email, subject, message): def send(self, subject, message):
sender = self._smtp_login
receivers = [self._site_admin_email]
# Create the container (outer) email message.
msg = MIMEText(message) msg = MIMEText(message)
msg["Subject"] = subject msg["Subject"] = subject
msg["To"] = to_email msg["To"] = self._site_admin_email
msg["From"] = self._smtp_login msg["From"] = sender
success = True context = ssl.create_default_context()
try: with smtplib.SMTP_SSL(self._smtp_host, self._smtp_port, context=context) as server:
if self._smtp_ssl: server.login(self._smtp_login, self._smtp_password)
s = smtplib.SMTP_SSL(self._smtp_host, self._smtp_port) server.send_message(msg, sender, receivers)
else: return True
s = smtplib.SMTP(self._smtp_host, self._smtp_port) return False
if self._smtp_starttls:
s.starttls()
if self._smtp_login:
s.login(self._smtp_login, self._smtp_password)
s.send_message(msg)
s.quit()
except Exception:
logger.exception("send mail exception")
success = False
return success
def get_error_handler(self):
if self._smtp_ssl:
mail_handler = SSLSMTPHandler(
mailhost=(
self._smtp_host,
self._smtp_port,
),
credentials=(
self._smtp_login,
self._smtp_password,
),
fromaddr=self._smtp_login,
toaddrs=self._site_admin_email,
subject="Stacosys error",
)
else:
mail_handler = SMTPHandler(
mailhost=(
self._smtp_host,
self._smtp_port,
),
credentials=(
self._smtp_login,
self._smtp_password,
),
fromaddr=self._smtp_login,
toaddrs=self._site_admin_email,
subject="Stacosys error",
)
mail_handler.setLevel(logging.ERROR)
return mail_handler
class SSLSMTPHandler(SMTPHandler):
def emit(self, record):
"""
Emit a record.
Format the record and send it to the specified addressees.
"""
try:
smtp = smtplib.SMTP_SSL(self.mailhost, self.mailport)
msg = EmailMessage()
msg["From"] = self.fromaddr
msg["To"] = ",".join(self.toaddrs)
msg["Subject"] = self.getSubject(record)
msg["Date"] = email.utils.localtime()
msg.set_content(self.format(record))
if self.username:
smtp.login(self.username, self.password)
smtp.send_message(msg)
smtp.quit()
except Exception:
self.handleError(record)

@ -15,14 +15,13 @@ class JobConfig(object):
self, self,
new_comment_polling_seconds, new_comment_polling_seconds,
site_name, site_name,
site_admin_email,
mailer, mailer,
): ):
self.JOBS = [ self.JOBS = [
{ {
"id": "submit_new_comment", "id": "submit_new_comment",
"func": "stacosys.core.cron:submit_new_comment", "func": "stacosys.core.cron:submit_new_comment",
"args": [site_name, site_admin_email, mailer], "args": [site_name, mailer],
"trigger": "interval", "trigger": "interval",
"seconds": new_comment_polling_seconds, "seconds": new_comment_polling_seconds,
}, },
@ -32,14 +31,12 @@ class JobConfig(object):
def configure( def configure(
comment_polling, comment_polling,
site_name, site_name,
site_admin_email,
mailer, mailer,
): ):
app.config.from_object( app.config.from_object(
JobConfig( JobConfig(
comment_polling, comment_polling,
site_name, site_name,
site_admin_email,
mailer, mailer,
) )
) )

@ -16,7 +16,6 @@ class ConfigTestCase(unittest.TestCase):
self.conf = Config() self.conf = Config()
self.conf.put(ConfigParameter.DB_SQLITE_FILE, EXPECTED_DB_SQLITE_FILE) self.conf.put(ConfigParameter.DB_SQLITE_FILE, EXPECTED_DB_SQLITE_FILE)
self.conf.put(ConfigParameter.HTTP_PORT, EXPECTED_HTTP_PORT) self.conf.put(ConfigParameter.HTTP_PORT, EXPECTED_HTTP_PORT)
self.conf.put(ConfigParameter.SMTP_STARTTLS, "yes")
def test_exists(self): def test_exists(self):
self.assertTrue(self.conf.exists(ConfigParameter.DB_SQLITE_FILE)) self.assertTrue(self.conf.exists(ConfigParameter.DB_SQLITE_FILE))
@ -27,7 +26,6 @@ class ConfigTestCase(unittest.TestCase):
self.assertIsNone(self.conf.get(ConfigParameter.HTTP_HOST)) self.assertIsNone(self.conf.get(ConfigParameter.HTTP_HOST))
self.assertEqual(self.conf.get(ConfigParameter.HTTP_PORT), EXPECTED_HTTP_PORT) self.assertEqual(self.conf.get(ConfigParameter.HTTP_PORT), EXPECTED_HTTP_PORT)
self.assertEqual(self.conf.get_int(ConfigParameter.HTTP_PORT), 8080) self.assertEqual(self.conf.get_int(ConfigParameter.HTTP_PORT), 8080)
self.assertTrue(self.conf.get_bool(ConfigParameter.SMTP_STARTTLS))
try: try:
self.conf.get_bool(ConfigParameter.DB_SQLITE_FILE) self.conf.get_bool(ConfigParameter.DB_SQLITE_FILE)
self.assertTrue(False) self.assertTrue(False)

Loading…
Cancel
Save