Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Embed picture in email using smtplib

import smtplib
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage

strFrom = 'zzzzzz@gmail.com'
strTo = 'xxxxx@gmail.com'

# Create the root message 

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot['Cc'] =cc
msgRoot.preamble = 'Multi-part message in MIME format.'

msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText('Alternative plain text message.')
msgAlternative.attach(msgText)

msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>KPI-DATA!', 'html')
msgAlternative.attach(msgText)

#Attach Image 
fp = open('test.png', 'rb') #Read image 
msgImage = MIMEImage(fp.read())
fp.close()

# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp.gmail.com') #SMTp Server Details
smtp.login('exampleuser', 'examplepass') #Username and Password of Account
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
Comment

PREVIOUS NEXT
Code Example
Python :: grid search python 
Python :: matplotlib transparency 
Python :: pylint: disable=unused-argument 
Python :: how to print items in a list in a single line python 
Python :: how to take password using pyautogui 
Python :: replace "-" for nan in dataframe 
Python :: numpy count the number of 1s in array 
Python :: undefie int value python 
Python :: divide by zero errors when using annotate 
Python :: python specify typeError output 
Python :: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead. 
Python :: python initialize list length n 
Python :: python create environment variable 
Python :: numpy identity matrix 
Python :: button icon pyqt5 
Python :: django logout 
Python :: koncemzem 
Python :: how to display speechmarks in python string 
Python :: How do I start a DataFrame index from 1? 
Python :: how to split a string from the beginning to a specific character in python 
Python :: how to fill an array with consecutive numbers python 
Python :: selenium send keys python 
Python :: button position python 
Python :: python os is directory 
Python :: hand tracking module 
Python :: python input tuple from user 
Python :: python for property in object 
Python :: pandas series to list 
Python :: absolute value of int python 
Python :: how to add column headers in pandas 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =