#encoding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
def Email_163(receiver,subject,text,model):
Sender = "xxx" # 发件人Email地址
AuthorizationCode = 'xxxx' # 授权码,163邮箱开启SMTP服务时授权第三方登陆邮箱的授权码
smtp_server = 'xx' # SMTP服务器地址
# HTML内容
# msg = MIMEText('<html><body><h1>Hello</h1>' +
# '<p>send by <a href="http://www.python.org">Python</a>...</p>' +
# '</body></html>', 'html', 'utf-8')
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = Header(subject, 'utf-8') # 邮件主题
msgRoot['From'] = formataddr(['xx', Sender]) # 发件人,显示在收件人界面上
msgRoot['To'] = formataddr(['', receiver]) # 收件人
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgAlternative.attach(MIMEText(text, model, 'utf-8'))
fp = open('test.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(smtp_server, 25) # 25 为 SMTP 端口号
smtpObj.login(Sender, AuthorizationCode) # 登陆邮箱
smtpObj.sendmail(Sender, receiver, msgRoot.as_string()) # 发送邮件
smtpObj.quit() # 推出
print u"发送成果"
except smtplib.SMTPException,e:
print u"发送失败"
print e
Email_163("xxx",'你好',''' <html>
<head>测试一下</head>
<body>
你们好啊<br>
<br><img src="cid:image1"></br>
</p>
</body>
</html> ''','html')