2014年9月9日星期二

使用python发送邮件的一种写法

__author__ = 'eagle'
# -*- coding: utf-8 -*-
import smtplib

# The below code never changes, though obviously those variables need values.
GMAIL_USERNAME='longfengfirst@gmail.com'
GMAIL_PASSWORD='*********'
email_subject='example english'
body_of_email='''
<html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<body>
<p>
    我有一个想法,就是将每日英语分享出来,目前我决定采用python来抓取数据来源,
然后通过邮件将数据发送出去!
</p>
</body>
</html>
'''
recipient='42296****@qq.com'
session = smtplib.SMTP('smtp.gmail.com', 25)
session.ehlo()
session.starttls()
session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
headers = "\r\n".join(["from: " + GMAIL_USERNAME,
                       "subject: " + email_subject,
                       "to: " + recipient,
                       "mime-version: 1.0",
                       "content-type: text/html"])

# body_of_email can be plaintext or html!
content = headers + "\r\n\r\n" + body_of_email
session.sendmail(GMAIL_USERNAME, recipient, content)

没有评论:

发表评论