本文最后更新于:2020-08-23 00:01
利用 Python 的内置 SMTP 模块发送邮件,可以配合其他功能(如天气预报等)一起使用
这里,只讲解一下发送邮件的方法,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
from email.mime.text import MIMEText from email.header import Header from smtplib import SMTPException from smtplib import SMTP_SSL
host_server = 'smtp.qq.com' sender = '120*****43' password = 'asd**********spf'
def SendEmail(receiver, mail_title, mail_content): try: smtp = SMTP_SSL(host_server) smtp.set_debuglevel(0) smtp.ehlo(host_server) smtp.login(sender, password)
msg = MIMEText(mail_content, "plain", 'utf-8') msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = sender + '@qq.com' msg["To"] = receiver smtp.sendmail(sender + '@qq.com', receiver, msg.as_string()) smtp.quit() print('邮件 %s 发送成功' % mail_title) except SMTPException as e: print('Error: ', e)
|
要发送邮件只要调用
1
| SendEmail('67*****22@qq.com', '标题', '内容')
|