用Python来发送邮件

import smtplib
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart
mail_host = "smtp.163.com"
mail_user = "xxxxx"
mail_pass = "xxxxx"
def sendmail(to, From, Filename):
    '''
    sendmail函数,用来发送附件
    '''
    msg = MIMEMultipart()
    att = MIMEText(open(Filename, 'rb').read(), 'base64', 'utf-8')
    att["Content-Type"] = 'application/octet-stream'
    att["Content-Disposition"] = 'attachment; filename="%s"' % Filename
    msg.attach(att)
    msg['to'] = to
    msg['from'] = From
    msg['Subject'] = Filename
    server = smtplib.SMTP(mail_host)
    server.login(mail_user, mail_pass)
    server.sendmail(msg['from'], msg['to'], msg.as_string())
    server.close()


需要包含的库文件包括smtp以及email,全部为默认库。

这个sendmail函数可以直接用来将Filename对应文件作为附件发送出去。。。代码很简单,应该看完就能用了。。。

你可能感兴趣的:(用Python来发送邮件)