A working app with a few bugs sprinkled in. Configuration saved externally
This commit is contained in:
42
app/utils_mail/runner.py
Normal file
42
app/utils_mail/runner.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import smtplib
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from email.mime.application import MIMEApplication
|
||||
import os
|
||||
import logging
|
||||
import configuration
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
config = configuration.parsed["MAIL"]
|
||||
|
||||
def send(article_model):
|
||||
mail = MIMEMultipart()
|
||||
mail['Subject'] = "{} -- {}".format(article_model.source_name, article_model.title)
|
||||
mail['From'] = config["sender"]
|
||||
mail['To'] = config["recipient"]
|
||||
|
||||
msgs = article_model.mail_info # this is html
|
||||
msg = [m["reply_text"] for m in msgs]
|
||||
msg = "\n".join(msg)
|
||||
|
||||
content = MIMEText(msg, "html")
|
||||
mail.attach(content)
|
||||
|
||||
files = [m["file_path"] for m in msgs if m["file_path"]]
|
||||
for path in files:
|
||||
with open(path, 'rb') as file:
|
||||
part = MIMEApplication(file.read(), "pdf")
|
||||
# encoders.encode_base64(part)
|
||||
part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(path))
|
||||
mail.attach(part)
|
||||
|
||||
try:
|
||||
smtp = smtplib.SMTP(config["smtp_server"], config["port"])
|
||||
smtp.starttls()
|
||||
smtp.login(config["uname"], config["password"])
|
||||
smtp.sendmail(config["sender"], config["recipient"], mail.as_string())
|
||||
smtp.quit()
|
||||
logger.info("Mail successfully sent.")
|
||||
except Exception as e:
|
||||
logger.error("Could not send mail for article {}".format(article_model))
|
||||
logger.info(e)
|
Reference in New Issue
Block a user