Better email notifications
Rework a bit on top of @bnjbvr's MR to add basic email notifications support. * Cleaned the code a bit. * Add a `Date` and a `Message-ID` header to improve the spam score. Thanks a lot @bnjbvr's for the MR!
This commit is contained in:
parent
23bbee8271
commit
215fe14d01
@ -1,17 +1,33 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Email notifications.
|
Email notifications.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import absolute_import, print_function, unicode_literals
|
from __future__ import absolute_import, print_function, unicode_literals
|
||||||
|
|
||||||
|
import logging
|
||||||
import smtplib
|
import smtplib
|
||||||
|
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
|
from email.utils import formatdate, make_msgid
|
||||||
|
|
||||||
def send_email(server, port, subject, _from, to, txt, html):
|
LOGGER = logging.getLogger(__name__)
|
||||||
if len(to) == 0:
|
|
||||||
|
|
||||||
|
def send_email(server, port, subject, _from, _to, txt, html):
|
||||||
|
"""
|
||||||
|
Send an email
|
||||||
|
|
||||||
|
:param server: SMTP server to use.
|
||||||
|
:param port: SMTP port to use.
|
||||||
|
:param subject: Subject of the email to send.
|
||||||
|
:param _from: Email address of the sender.
|
||||||
|
:param _to: List of email addresses of the receivers.
|
||||||
|
:param txt: Text version of the message.
|
||||||
|
:param html: HTML version of the message.
|
||||||
|
"""
|
||||||
|
if not _to:
|
||||||
LOGGER.warn("No recipients for the email notifications, aborting.")
|
LOGGER.warn("No recipients for the email notifications, aborting.")
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -20,17 +36,26 @@ def send_email(server, port, subject, _from, to, txt, html):
|
|||||||
msg = MIMEMultipart('alternative')
|
msg = MIMEMultipart('alternative')
|
||||||
msg['Subject'] = subject
|
msg['Subject'] = subject
|
||||||
msg['From'] = _from
|
msg['From'] = _from
|
||||||
msg['To'] = ', '.join(to)
|
msg['To'] = ', '.join(_to)
|
||||||
|
msg['Date'] = formatdate()
|
||||||
|
msg['Message-ID'] = make_msgid()
|
||||||
|
|
||||||
msg.attach(MIMEText(txt, 'plain', 'utf-8'))
|
msg.attach(MIMEText(txt, 'plain', 'utf-8'))
|
||||||
msg.attach(MIMEText(html, 'html', 'utf-8'))
|
msg.attach(MIMEText(html, 'html', 'utf-8'))
|
||||||
|
|
||||||
server.sendmail(_from, to, msg.as_string())
|
server.sendmail(_from, _to, msg.as_string())
|
||||||
server.quit()
|
server.quit()
|
||||||
|
|
||||||
|
|
||||||
def send_notification(config, flats):
|
def send_notification(config, flats):
|
||||||
|
"""
|
||||||
|
Send an email notification about new available flats.
|
||||||
|
|
||||||
|
:param config: A config dict.
|
||||||
|
:param flats: List of flats to include in the notification.
|
||||||
|
"""
|
||||||
# Don't send an email if there are no new flats.
|
# Don't send an email if there are no new flats.
|
||||||
if len(flats) == 0:
|
if not flats:
|
||||||
return
|
return
|
||||||
|
|
||||||
txt = u'Hello dear user,\n\nThe following new flats have been found:\n\n'
|
txt = u'Hello dear user,\n\nThe following new flats have been found:\n\n'
|
||||||
@ -53,8 +78,11 @@ def send_notification(config, flats):
|
|||||||
cost = unicode(flat.cost)
|
cost = unicode(flat.cost)
|
||||||
currency = unicode(flat.currency)
|
currency = unicode(flat.currency)
|
||||||
|
|
||||||
txt += '- {}: {}#/flat/{} (area: {}, cost: {} {})\n' \
|
txt += (
|
||||||
.format(title, website_url, flat_id, area, cost, currency)
|
'- {}: {}#/flat/{} (area: {}, cost: {} {})\n'.format(
|
||||||
|
title, website_url, flat_id, area, cost, currency
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
html += """
|
html += """
|
||||||
<li>
|
<li>
|
||||||
@ -65,9 +93,11 @@ def send_notification(config, flats):
|
|||||||
|
|
||||||
html += "</ul>"
|
html += "</ul>"
|
||||||
|
|
||||||
SIGNATURE = u"\nHope you'll find what you were looking for.\n\nBye!\nFlatisfy"
|
signature = (
|
||||||
txt += SIGNATURE
|
u"\nHope you'll find what you were looking for.\n\nBye!\nFlatisfy"
|
||||||
html += SIGNATURE.replace('\n', '<br>')
|
)
|
||||||
|
txt += signature
|
||||||
|
html += signature.replace('\n', '<br>')
|
||||||
|
|
||||||
html += """</p>
|
html += """</p>
|
||||||
</body>
|
</body>
|
||||||
|
Loading…
Reference in New Issue
Block a user