python SMTP other

时间:2023-03-09 19:01:33
python SMTP other

HTML 正文,带链接和图片

//test.py

import smtplib

from email.mime.image import MIMEImage

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

from email.header import Header

mail_host="smtp.qq.com"

mail_user="791398105@qq.com"

mail_pass="junyhvbhtjsrbcia" #your authorized code

sender='791398105@qq.com'

receivers=['791398105@qq.com']

message=MIMEMultipart()

message['From']=Header("my",'utf-8')

message['To']= Header("you",'utf-8')

subject='Test mail, please not open it'

message['Subject']=Header(subject,'utf-8')

msgAlter=MIMEMultipart('alter')

message.attach(msgAlter)

mail_msg='''

<p>Python mail test,give you money...</p>

<p><a href="http://www.zcl.com">my linker</a></p>

<p>photo demo:</p>

<p><img src="cid:image1"></p>

'''

msgAlter.attach(MIMEText(mail_msg, 'html', 'utf-8'))

fp=open('test.png','rb')

msgImage=MIMEImage(fp.read())

fp.close()

msgImage.add_header('Content-ID','<image1>')

message.attach(msgImage)

att1=MIMEText(open('f.txt', 'rb').read(), 'base64','utf-8')

att1["Content-Type"]='application/octet-stream'

att1["Content-Disposition"]='attachment;filename="f.txt"'

message.attach(att1)

try:

smtpObj = smtplib.SMTP_SSL(mail_host,465)

smtpObj.login(mail_user,mail_pass)

smtpObj.sendmail(sender, receivers, message.as_string())

smtpObj.quit()

  print "Succeed in sending mail"

except smtplib.SMTPException,e:

print e

//result

# python test.py
Succeed in sending mail

python SMTP other