simplify mailer
This commit is contained in:
parent
7f2ff74ebe
commit
185641e6d0
7 changed files with 24 additions and 118 deletions
14
config.ini
14
config.ini
|
@ -19,19 +19,9 @@ port = 8100
|
|||
proto = https
|
||||
file = comments.xml
|
||||
|
||||
[imap]
|
||||
polling = 120
|
||||
host = mail.gandi.net
|
||||
ssl = false
|
||||
port = 993
|
||||
login = blog@mydomain.com
|
||||
password = MYPASSWORD
|
||||
|
||||
[smtp]
|
||||
host = mail.gandi.net
|
||||
starttls = true
|
||||
ssl = false
|
||||
port = 587
|
||||
host = smtp.mail.com
|
||||
port = 465
|
||||
login = blog@mydomain.com
|
||||
password = MYPASSWORD
|
||||
|
||||
|
|
8
run.py
8
run.py
|
@ -74,24 +74,16 @@ def stacosys_server(config_pathname):
|
|||
mailer = Mailer(
|
||||
conf.get(ConfigParameter.SMTP_HOST),
|
||||
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_PASSWORD),
|
||||
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
|
||||
conf.put(ConfigParameter.SITE_TOKEN, hashlib.sha1(conf.get(ConfigParameter.SITE_NAME).encode('utf-8')).hexdigest())
|
||||
scheduler.configure(
|
||||
conf.get_int(ConfigParameter.COMMENT_POLLING),
|
||||
conf.get(ConfigParameter.SITE_NAME),
|
||||
conf.get(ConfigParameter.SITE_ADMIN_EMAIL),
|
||||
mailer,
|
||||
)
|
||||
|
||||
|
|
|
@ -17,8 +17,6 @@ class ConfigParameter(Enum):
|
|||
RSS_PROTO = "rss.proto"
|
||||
RSS_FILE = "rss.file"
|
||||
|
||||
SMTP_STARTTLS = "smtp.starttls"
|
||||
SMTP_SSL = "smtp.ssl"
|
||||
SMTP_HOST = "smtp.host"
|
||||
SMTP_PORT = "smtp.port"
|
||||
SMTP_LOGIN = "smtp.login"
|
||||
|
|
|
@ -8,7 +8,7 @@ from stacosys.db import dao
|
|||
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():
|
||||
comment_list = (
|
||||
"author: %s" % comment.author_name,
|
||||
|
@ -23,7 +23,7 @@ def submit_new_comment(site_name, site_admin_email, mailer):
|
|||
|
||||
# send email to notify admin
|
||||
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 ")
|
||||
|
||||
# save notification datetime
|
||||
|
|
|
@ -3,109 +3,40 @@
|
|||
|
||||
import logging
|
||||
import smtplib
|
||||
import email.utils
|
||||
import ssl
|
||||
|
||||
from email.mime.text import MIMEText
|
||||
from email.message import EmailMessage
|
||||
from logging.handlers import SMTPHandler
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Mailer:
|
||||
def __init__(
|
||||
self,
|
||||
smtp_host,
|
||||
smtp_port,
|
||||
smtp_starttls,
|
||||
smtp_ssl,
|
||||
smtp_login,
|
||||
smtp_password,
|
||||
site_admin_email,
|
||||
self,
|
||||
smtp_host,
|
||||
smtp_port,
|
||||
smtp_login,
|
||||
smtp_password,
|
||||
site_admin_email,
|
||||
):
|
||||
self._smtp_host = smtp_host
|
||||
self._smtp_port = smtp_port
|
||||
self._smtp_starttls = smtp_starttls
|
||||
self._smtp_ssl = smtp_ssl
|
||||
self._smtp_login = smtp_login
|
||||
self._smtp_password = smtp_password
|
||||
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["Subject"] = subject
|
||||
msg["To"] = to_email
|
||||
msg["From"] = self._smtp_login
|
||||
msg["To"] = self._site_admin_email
|
||||
msg["From"] = sender
|
||||
|
||||
success = True
|
||||
try:
|
||||
if self._smtp_ssl:
|
||||
s = smtplib.SMTP_SSL(self._smtp_host, self._smtp_port)
|
||||
else:
|
||||
s = smtplib.SMTP(self._smtp_host, self._smtp_port)
|
||||
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)
|
||||
context = ssl.create_default_context()
|
||||
with smtplib.SMTP_SSL(self._smtp_host, self._smtp_port, context=context) as server:
|
||||
server.login(self._smtp_login, self._smtp_password)
|
||||
server.send_message(msg, sender, receivers)
|
||||
return True
|
||||
return False
|
||||
|
|
|
@ -15,14 +15,13 @@ class JobConfig(object):
|
|||
self,
|
||||
new_comment_polling_seconds,
|
||||
site_name,
|
||||
site_admin_email,
|
||||
mailer,
|
||||
):
|
||||
self.JOBS = [
|
||||
{
|
||||
"id": "submit_new_comment",
|
||||
"func": "stacosys.core.cron:submit_new_comment",
|
||||
"args": [site_name, site_admin_email, mailer],
|
||||
"args": [site_name, mailer],
|
||||
"trigger": "interval",
|
||||
"seconds": new_comment_polling_seconds,
|
||||
},
|
||||
|
@ -32,14 +31,12 @@ class JobConfig(object):
|
|||
def configure(
|
||||
comment_polling,
|
||||
site_name,
|
||||
site_admin_email,
|
||||
mailer,
|
||||
):
|
||||
app.config.from_object(
|
||||
JobConfig(
|
||||
comment_polling,
|
||||
site_name,
|
||||
site_admin_email,
|
||||
mailer,
|
||||
)
|
||||
)
|
||||
|
|
|
@ -16,7 +16,6 @@ class ConfigTestCase(unittest.TestCase):
|
|||
self.conf = Config()
|
||||
self.conf.put(ConfigParameter.DB_SQLITE_FILE, EXPECTED_DB_SQLITE_FILE)
|
||||
self.conf.put(ConfigParameter.HTTP_PORT, EXPECTED_HTTP_PORT)
|
||||
self.conf.put(ConfigParameter.SMTP_STARTTLS, "yes")
|
||||
|
||||
def test_exists(self):
|
||||
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.assertEqual(self.conf.get(ConfigParameter.HTTP_PORT), EXPECTED_HTTP_PORT)
|
||||
self.assertEqual(self.conf.get_int(ConfigParameter.HTTP_PORT), 8080)
|
||||
self.assertTrue(self.conf.get_bool(ConfigParameter.SMTP_STARTTLS))
|
||||
try:
|
||||
self.conf.get_bool(ConfigParameter.DB_SQLITE_FILE)
|
||||
self.assertTrue(False)
|
||||
|
|
Loading…
Add table
Reference in a new issue