-
Notifications
You must be signed in to change notification settings - Fork 38
/
InlineMailing.py
50 lines (40 loc) · 1.79 KB
/
InlineMailing.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
# Send an HTML email with an embedded image and a plain text message for
# email clients that don't want to display the HTML.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def SendMail(From,Password,To,Message,Subject,ImgPath):
# Define these once; use them twice!
strFrom = From
strTo = To
# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = Subject
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'
# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
# We reference the image in the IMG SRC attribute by the ID we give it below
# '<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1" width=20px height=30px><br>Nifty!'
msgText = MIMEText(Message, 'html')
msgAlternative.attach(msgText)
# This example assumes the image is in the current directory
fp = open(ImgPath, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
# Send the email (this example assumes SMTP authentication is required)
smtp = smtplib.SMTP('smtp.gmail.com',587)
smtp.starttls()
smtp.login(strFrom,Password)
smtp.sendmail(strFrom,strTo,msgRoot.as_string())
smtp.quit()
return "hey"