python发送邮件

# coding=utf-8
from smtplib import SMTP_SSL
from email.header import Header
from email.mime.text import MIMEText
mail_info = {
            "from": "[email protected]",
            "to": "[email protected]",
            "hostname": "smtp.qq.com",
            "username": "[email protected]",
            "password": "",
            "mail_subject": "",
            "mail_text": "起床了,猪",
            "mail_encoding": "utf-8"
            }
smtp = SMTP_SSL(mail_info["hostname"])
smtp.set_debuglevel(1)
smtp.ehlo(mail_info["hostname"])
smtp.login(mail_info["username"], mail_info["password"])
msg = MIMEText(mail_info["mail_text"], "plain", mail_info["mail_encoding"])
msg["Subject"] = Header(mail_info["mail_subject"], mail_info["mail_encoding"])
msg["from"] = mail_info["from"]
msg["to"] = mail_info["to"]        
smtp.sendmail(mail_info["from"], mail_info["to"], msg.as_string())
smtp.quit()



以上的password需要开启邮箱的POP3/SMTP服务

具体方法登录邮箱到账户设置里面。

你可能感兴趣的:(python发送邮件)