Send e-mail in HTML with images of local .py

smtplib Import 
from email.mime.text Import MimeText
from email.header Import Header
from email.mime.image Import MIMEImage
from email.mime.multipart Import MimeMultipart

# third-party SMTP service
# Set the server
mail_host = "smtp.qq.com"

# username
mail_user = "[email protected]"

# obtain an authorization code
mail_pass = "mpaocydzpzfjidge"

# senders account
sENDER = '[email protected]'

# receive mail, you can set your QQ-mail mailbox or other
receivers = [ '[email protected]']

the Message = MimeMultipart ( "Related")

# senders
message [ 'From'] = Header ( " I am the sender", 'UTF-8')

# recipient
message [ 'To'] = Header ( "I addressee", 'utf-8')

Subject = 'beautiful picture Chiang Cher'
Message [ 'the Subject'] = Header (Subject, 'UTF-. 8')

# Bring pictures:
msg = MimeMultipart ( "Alternative")
message.attach (msg)

send_content = "" "
<h1> day not angry with me Chun Li Gang </ h1>
<h1> Kendo such as vancomycin night </ h1>
<the p-> wine on a small two </ the p->
<img src = "cid: Image">
<div> Please read the & nbsp; <a href="https://www.37zw.net/0/761/"> point I, point I </a> </ div>
"" "

# the first parameter is the message content
msg.attach (MimeText (send_content, 'HTML', 'UTF-8'))

# read the image in the current directory
f = open ( 'beautiful picture Chiang Cher .jpg', 'RB')
img_msg = MIMEImage (reached, f.read ())
f.close ()

# image position is defined in the HTML text
img_msg.add_header ( 'Content-ID', '<image > ') based on the id # Location
message.attach(img_msg)

try:
smtpObj = smtplib.SMTP()
# 25 为 SMTP 端口号
smtpObj.connect(mail_host, 25)
smtpObj.login (mail_user, mail_pass)
smtpObj.sendmail (SENDER, Receivers, message.as_string ())
Print ( "e-mail sent successfully")
the except smtplib.SMTPException:
Print ( "Error: Can not Send Mail")

Guess you like

Origin www.cnblogs.com/zhang-da/p/12231728.html