Skip to content

Instantly share code, notes, and snippets.

@gainskills
Created February 13, 2018 15:21
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header
from email.utils import formataddr
sender = "mySendEmail1"
receiver = "myRecEmail1"
pwd = 'mypwd'
ret = True
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
<p><img src="cid:image1"></p>
"""
msg = MIMEMultipart()
msg['From'] = formataddr(['kz', sender])
msg['To'] = formataddr(['Dear Customer', receiver])
msg['Subject'] = Header('Test Email', 'utf-8')
msg.attach(MIMEText(mail_msg, 'html', 'utf-8'))
fp = open('test.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msg.attach(msgImage)
att1 = MIMEText(open('1.cpp', 'rb').read(), 'base64', 'utf-8')
att1['Context-Type'] = 'application/octet-stream'
att1.add_header('Content-Disposition', 'attachment', filename="1.cpp")
msg.attach(att1)
server = smtplib.SMTP('smtp-mail.outlook.com', 587)
server.starttls()
try:
server.login(sender, pwd)
server.sendmail(sender, [receiver ,], msg.as_string())
server.quit()
except Exception:
print(Exception)
ret = False
if ret:
print('Email has been sent successfully')
else:
print('It failed to send the email')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment