I'm composing an email with MIMEMultipart module of Python 3.7 and try to send it over an "smtp.office365.com" server. I would like to complete the following piece of code adding an email signature composed by an image (.png) and some plain text.
I tried different ways but I'm still stuck on this problem. Can you help me to add the right piece of code for sign the email with a .png image and some plain text? Thank you very much!!
Here is the code I wrote so far:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email import encoders
sender='sender@hotmail.it'
reciever='reciever@gmail.com'
#--create a multi-part message object
message = MIMEMultipart()
#--filling up the header
message['From'] = sender
message['Subject'] = 'Hello!'
#body = some HTML text...
#---retrieve and attach the body from the draft email to the new message object
b = MIMEText(body,'html')
message.attach(b)
#---open the attachment file
filename = "attach.pdf"
attachment = open("C:/Users/attach.pdf",'rb')
#---attach the file to the message object
p = MIMEBase('application','octet-stream')
p.set_payload((attachment).read())
encoders.encode_base64(p)
p.add_header('Content-Disposition',"attachment; filename= %s" % filename)
message.attach(p)
#---convert the message object to string
text = message.as_string()
#--------------------------------------
#-------------SENDING------------------
#--------------------------------------
import smtplib
SMTPserver = 'smtp.office365.com'
USERNAME = sender
PASSWORD = password
#--start server connection
server = smtplib.SMTP(SMTPserver,'587')
server.starttls()
server.login(USERNAME, PASSWORD)
server.sendmail(sender,reciever,text)
server.quit()