-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail-test.py
66 lines (51 loc) · 1.64 KB
/
mail-test.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import smtplib
from email.message import EmailMessage
import imghdr
import pdfkit
#THIS IS ANOTHER WAY TO DO IT
pdfkit.from_file('songs.html', 'results.pdf')
# this gives the port needed for the gmail -- this is from the python article
port = 465
# this is from the video ???
port = 587
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
msg = EmailMessage()
msg['Subject'] = 'Check out my results!'
msg['From'] = EMAIL_ADDRESS
msg['To'] = '[email protected]'
msg.set_content('How about dinner at 6pm this Saturday')
# lists for JPGs and PDFs
files = ['creators.jpg', 'city.jpg']
pdffiles = ['results.pdf']
# for loop for JPGs
for file in files:
# adds an attachment of an image to the email
with open(file, 'rb') as f:
file_data = f.read()
file_type = imghdr.what(f.name)
filename = f.name
# for loop for PDF
for pdffile in pdffiles:
# adds an attachment of an image to the email
with open(pdffile, 'rb') as x:
pdffile_data = x.read()
pdffilename = x.name
msg.add_attachment(file_data, maintype='image', subtype=file_type, filename=filename)
msg.add_attachment(pdffile_data, maintype='application', subtype="octet-stream", filename=pdffilename)
msg.add_alternative("""\
<! DOCTYPE html>
<html>
<body>
<h1 style="color: SlateGray;">This is an HTML Email!</h1>
</body>
</html>
""", subtype='html')
with smtplib.SMTP('smtp.gmail.com', port) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
# send the email and the message
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)