-
Notifications
You must be signed in to change notification settings - Fork 2
/
mail_script.py
38 lines (31 loc) · 1.15 KB
/
mail_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import os
import smtplib
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import requests
from jinja2 import Template, Environment, FileSystemLoader
jinja_environment = Environment(autoescape=True,loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))
def email_log_in():
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(os.environ['EMAIL_ADDRESS'], os.environ['EMAIL_PASSWORD'])
return server
def mail_update(server, data, mail_to):
try:
msg = MIMEMultipart()
msg['Subject'] = "Today's filings!"
msg['From'] = os.environ['EMAIL_ADDRESS']
msg['BCC'] = mail_to
template = jinja_environment.get_template('working_filings.html')
text = template.render(data)
msg.attach(MIMEText(text, 'html'))
text = msg.as_string()
server.sendmail(os.environ['EMAIL_ADDRESS'], mail_to, text)
server.quit()
except:
mail_update('ERROR FOR {0}'.format(mail_to), os.environ['ADMIN_EMAIL'])
exit()