2017-06-13 18:19:16 +02:00
|
|
|
# coding: utf-8
|
|
|
|
"""
|
|
|
|
Email notifications.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2017-10-19 21:34:02 +02:00
|
|
|
from builtins import str
|
2017-07-06 15:05:21 +02:00
|
|
|
|
|
|
|
import logging
|
2017-06-13 18:19:16 +02:00
|
|
|
import smtplib
|
2021-02-12 12:16:03 +01:00
|
|
|
from money import Money
|
2017-06-13 18:19:16 +02:00
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
2017-07-06 15:05:21 +02:00
|
|
|
from email.utils import formatdate, make_msgid
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
2017-06-13 18:19:16 +02:00
|
|
|
|
2017-07-06 15:05:21 +02:00
|
|
|
|
2021-01-26 16:49:43 +01:00
|
|
|
def send_email(server, port, subject, _from, _to, txt, html, username=None, password=None):
|
2017-07-06 15:05:21 +02:00
|
|
|
"""
|
|
|
|
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:
|
2017-06-13 18:19:16 +02:00
|
|
|
LOGGER.warn("No recipients for the email notifications, aborting.")
|
|
|
|
return
|
|
|
|
|
|
|
|
server = smtplib.SMTP(server, port)
|
2019-01-25 18:41:16 +01:00
|
|
|
if username or password:
|
|
|
|
server.login(username or "", password or "")
|
2017-06-13 18:19:16 +02:00
|
|
|
|
2021-01-26 14:39:52 +01:00
|
|
|
msg = MIMEMultipart("alternative")
|
|
|
|
msg["Subject"] = subject
|
|
|
|
msg["From"] = _from
|
|
|
|
msg["To"] = ", ".join(_to)
|
|
|
|
msg["Date"] = formatdate()
|
|
|
|
msg["Message-ID"] = make_msgid()
|
2017-06-13 18:19:16 +02:00
|
|
|
|
2021-01-26 14:39:52 +01:00
|
|
|
msg.attach(MIMEText(txt, "plain", "utf-8"))
|
|
|
|
msg.attach(MIMEText(html, "html", "utf-8"))
|
2017-06-13 18:19:16 +02:00
|
|
|
|
2017-07-06 15:05:21 +02:00
|
|
|
server.sendmail(_from, _to, msg.as_string())
|
2017-06-13 18:19:16 +02:00
|
|
|
server.quit()
|
|
|
|
|
2017-07-06 15:05:21 +02:00
|
|
|
|
2017-06-13 18:19:16 +02:00
|
|
|
def send_notification(config, flats):
|
2017-07-06 15:05:21 +02:00
|
|
|
"""
|
|
|
|
Send an email notification about new available flats.
|
|
|
|
|
|
|
|
:param config: A config dict.
|
|
|
|
:param flats: List of flats to include in the notification.
|
|
|
|
"""
|
2017-06-13 18:19:16 +02:00
|
|
|
# Don't send an email if there are no new flats.
|
2017-07-06 15:05:21 +02:00
|
|
|
if not flats:
|
2017-06-13 18:19:16 +02:00
|
|
|
return
|
|
|
|
|
2021-01-26 14:42:53 +01:00
|
|
|
i18n = {
|
2021-02-08 16:17:26 +01:00
|
|
|
"en": {
|
|
|
|
"subject": f"{len(flats)} new flats found!",
|
|
|
|
"hello": "Hello dear user",
|
|
|
|
"following_new_flats": "The following new flats have been found:",
|
|
|
|
"area": "area",
|
|
|
|
"cost": "cost",
|
|
|
|
"signature": "Hope you'll find what you were looking for.",
|
|
|
|
},
|
|
|
|
"fr": {
|
|
|
|
"subject": f"{len(flats)} nouvelles annonces disponibles !",
|
|
|
|
"hello": "Bonjour cher utilisateur",
|
|
|
|
"following_new_flats": "Voici les nouvelles annonces :",
|
|
|
|
"area": "surface",
|
|
|
|
"cost": "coût",
|
|
|
|
"signature": "Bonne recherche",
|
|
|
|
},
|
2021-01-26 14:42:53 +01:00
|
|
|
}
|
2021-02-08 16:17:26 +01:00
|
|
|
trs = i18n.get(config["notification_lang"], "en")
|
2021-01-26 14:42:53 +01:00
|
|
|
|
2021-02-08 16:17:26 +01:00
|
|
|
txt = trs["hello"] + ",\n\n\n\n"
|
2021-01-26 14:42:53 +01:00
|
|
|
html = f"""
|
2017-06-13 18:19:16 +02:00
|
|
|
<html>
|
|
|
|
<head></head>
|
|
|
|
<body>
|
2021-02-08 16:17:26 +01:00
|
|
|
<p>{trs["hello"]}!</p>
|
|
|
|
<p>{trs["following_new_flats"]}
|
2017-06-13 18:19:16 +02:00
|
|
|
|
|
|
|
<ul>
|
|
|
|
"""
|
|
|
|
|
|
|
|
website_url = config["website_url"]
|
|
|
|
|
|
|
|
for flat in flats:
|
2017-10-19 21:34:02 +02:00
|
|
|
title = str(flat.title)
|
|
|
|
flat_id = str(flat.id)
|
2021-02-12 12:16:03 +01:00
|
|
|
area = str(int(flat.area))
|
|
|
|
cost = int(flat.cost)
|
2017-10-19 21:34:02 +02:00
|
|
|
currency = str(flat.currency)
|
2017-06-13 18:19:16 +02:00
|
|
|
|
2021-02-12 12:16:03 +01:00
|
|
|
txt += f"- {title}: {website_url}#/flat/{flat_id}"
|
|
|
|
html += f"""
|
2017-06-13 18:19:16 +02:00
|
|
|
<li>
|
2021-02-12 12:16:03 +01:00
|
|
|
<a href="{website_url}#/flat/{flat_id}">{title}</a>
|
|
|
|
"""
|
|
|
|
|
|
|
|
fields = []
|
|
|
|
if area:
|
|
|
|
fields.append(f"{trs['area']}: {area}m²")
|
|
|
|
if cost:
|
|
|
|
money = Money(cost, currency).format(config["notification_lang"])
|
|
|
|
fields.append(f"{trs['cost']}: {money}")
|
|
|
|
|
|
|
|
if len(fields):
|
|
|
|
txt += f'({", ".join(fields)})'
|
|
|
|
html += f'({", ".join(fields)})'
|
|
|
|
|
|
|
|
html += "</li>"
|
2017-06-13 18:19:16 +02:00
|
|
|
|
|
|
|
html += "</ul>"
|
|
|
|
|
2021-02-08 16:17:26 +01:00
|
|
|
signature = f"\n{trs['signature']}\n\nBye!\nFlatisfy"
|
2017-07-06 15:05:21 +02:00
|
|
|
txt += signature
|
2021-01-26 14:39:52 +01:00
|
|
|
html += signature.replace("\n", "<br>")
|
2017-06-13 18:19:16 +02:00
|
|
|
|
|
|
|
html += """</p>
|
|
|
|
</body>
|
|
|
|
</html>"""
|
|
|
|
|
2021-01-26 14:39:52 +01:00
|
|
|
send_email(
|
|
|
|
config["smtp_server"],
|
|
|
|
config["smtp_port"],
|
2021-02-08 16:17:26 +01:00
|
|
|
trs["subject"],
|
2021-01-26 14:39:52 +01:00
|
|
|
config["smtp_from"],
|
|
|
|
config["smtp_to"],
|
|
|
|
txt,
|
|
|
|
html,
|
|
|
|
config.get("smtp_username"),
|
|
|
|
config.get("smtp_password"),
|
|
|
|
)
|