[Python] 发送Email (可发HTML,附件)

长平狐 发布于 2013/06/03 15:03
阅读 2K+
收藏 2

在Python里我们可以使用smtplib模块发送email,smtp(Simple Mail Transfer Protocal),是简单邮件传输协议,通过它可以和smtp server进行通讯。

smtplib的一个简单例子

import smtplib
"""The first step is to create an SMTP object, each object is used for connection with one server."""
server = smtplib.SMTP('smtp.163.com','994')
 
#Next, log in to the server
server.login("username","password")
 
#Send the mail
msg = "\nHello!" # The \n separates the message from the headers
server.sendmail("from@163.com", "to@gmail.com", msg)

首先你要获得邮件服务器的smtp地址和端口号,一般都能在网上找到,然后输入你邮箱的用户名和密码,编辑邮件标题和正文(它们之间用\n隔开),最后指定目标邮件地址发送邮件。

使用Email Package

Python里有两个模块:MIMEMultipart和MIMEText,我们可以利用它们构造和解析邮件信息。
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

#First, we compose some of the basic message headers:
fromaddr = "from@163.com"
toaddr = "to@gmail.com"
ccaddr = "cc@gmail.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Cc'] = ccaddr
msg['Subject'] = "Python email"

#Next, we attach the body of the email to the MIME message:
body = "Python test mail"
msg.attach(MIMEText(body, 'plain'))

'''For sending the mail, we have to convert the object to a string, and then
use the same prodecure as above to send using the SMTP server..'''

server = smtplib.SMTP('smtp.163.com','994')
server.login("username","password")

text = msg.as_string()server.sendmail(fromaddr, toaddr, text)






上面这个例子发送了一个纯文本信息(Plain),如果有发送html邮件,可以参考如下例子:
body = "<a href='http://blog.csdn.net/u010415792'>Zhu_Julian's Blog</a>"
msg.attach(MIMEText(body, 'html'))

如果要发送附件,只要把附件attach到msg实例即可:
#Attach an attachment
att = MIMEText(open(r'c:\123.txt', 'rb').read(), 'base64', 'gb2312')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="123.txt'
msg.attach(att)

Send Email模板

def sendemail(from_addr, to_addr_list, cc_addr_list,
              subject, username, password, msg_content, 
              msg_type='plain',  attachment=None):
    #First, we compose some of the basic message headers:
    msg = MIMEMultipart()
    msg['From'] = from_addr
    msg['To'] = to_addr_list
    msg['Cc'] = cc_addr_list
    msg['Subject'] = subject   
    
    msg_type = msg_type.lower()
    if msg_type == 'plain':
        msg.attach(MIMEText(msg_content, 'plain'))
    elif mst_type == 'html':
        msg.attach(MIMEText(msg_content, 'html'))
    else:
        return false
    
    #Attach an attachment
    if attachment is not None:
        att = MIMEText(open(attachment, 'rb').read(), 'base64', 'gb2312')
        att["Content-Type"] = 'application/octet-stream'
        att.add_header("Content-Disposition", "attachment", filename = os.path.basename(attachment))
        msg.attach(att)    


    server = smtplib.SMTP('atom.corp.ebay.com')
    text = msg.as_string()
    server.sendmail(from_addr, to_addr_list, text)    

下面是调用上面模板的客户端:
sendemail(from_addr = 'xianzhu@ebay.com', 
          to_addr_list = 'xianzhu@ebay.com, xianzhu@ebay.com', 
          cc_addr_list = 'xianzhu@ebay.com',
          subject = 'subject', 
          username = 'username',
          password = 'password'
          msg_content = 'content', 
          msg_type='plain',  
          attachment=r'C:\123.txt')


原文链接:http://blog.csdn.net/u010415792/article/details/8924686
加载中
返回顶部
顶部