-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_gmail_html.py
52 lines (43 loc) · 1.39 KB
/
email_gmail_html.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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
#Prepare email for sending attachment
msg = MIMEMultipart()
#
# HTML message for the email body
html = """
<html>
<head></head>
<body>
<h1>Hello</h1>
<img src='cid:image1'>
</p>
</body>
</html>
"""
# Add the HTML text using MIMEText
html = MIMEText(html, 'html')
msg.attach(html)
f = open('android.png', "rb")
# Create the MIMEImage object
img = MIMEImage( f.read() )
# Note how "<image1>" below for Content-ID match the HTML "src" above
img.add_header('Content-ID', '<image1>')
# Always close after done reading
f.close()
# Set it to be viewed "inline"
img.add_header('Content-Disposition', 'inline', filename='android.png')
msg.attach(img)
sender_email_address ="[email protected]"
sender_email_password = "uurltlnomitneezj"
receiver_email_address = "[email protected]"
#email_title_content = "Subject: Sending Email Using Python\nThis is a test mail.f"
print("Trying to connect o Gmail SMTP server")
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.starttls()
print("Connected. Logging in...")
smtpObj.login(sender_email_address, sender_email_password)
smtpObj.sendmail(sender_email_address, receiver_email_address, msg.as_string())
print("Email sent successfully...")
smtpObj.quit()