Skip to content

Commit 5a0dbad

Browse files
committed
pushing generic emailer code for Flake8 article
1 parent fbc8069 commit 5a0dbad

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

generic_emailer/generic_emailer.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!python3
2+
#emailer.py is a simple script for sending emails using smtplib
3+
#The idea is to assign a web-scraped file to the DATA_FILE constant.
4+
#The data in the file is then read in and sent as the body of the email.
5+
6+
import smtplib
7+
from email.mime.multipart import MIMEMultipart
8+
from email.mime.text import MIMEText
9+
10+
from email_list import EMAILS
11+
12+
DATA_FILE = 'scraped_data_file'
13+
from_addr = '[email protected]'
14+
to_addr = '[email protected]' #Or any generic email you want all recipients to see
15+
bcc = EMAILS
16+
17+
msg = MIMEMultipart()
18+
msg['From'] = from_addr
19+
msg['To'] = to_addr
20+
msg['Subject'] = 'Subject Line'
21+
22+
with open(DATA_FILE) as f:
23+
body = f.read()
24+
25+
msg.attach(MIMEText(body, 'plain'))
26+
27+
smtp_server = smtplib.SMTP('smtp.gmail.com', 587) #Specify Gmail Mail server
28+
29+
smtp_server.ehlo() #Send mandatory 'hello' message to SMTP server
30+
31+
smtp_server.starttls() #Start TLS Encryption as we're not using SSL.
32+
33+
#Login to gmail: Account | Password
34+
smtp_server.login(' [email protected] ', ' GMAIL APPLICATION ID ')
35+
36+
text = msg.as_string()
37+
38+
#Compile email list: From, To, Email body
39+
smtp_server.sendmail(from_addr, [to_addr] + bcc, text)
40+
41+
#Close connection to SMTP server
42+
smtp_server.quit()
43+
44+
#Test Message to verify all passes
45+
print('Email sent successfully')

0 commit comments

Comments
 (0)