Python发邮件的小脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python发邮件的小脚本相关的知识,希望对你有一定的参考价值。
1 # -*- coding: UTF-8 -*- 2 import smtplib 3 from email.mime.text import MIMEText 4 5 mailto_list = [‘[email protected]‘,‘[email protected]‘,‘[email protected]‘,‘[email protected]‘] 6 mail_host = "smtp.qq.com" # 设置服务器 7 mail_user = "1027179157" # 用户名 8 mail_pass = "xxxxxxxxx" # 口令 9 mail_postfix = "qq.com" # 发件箱的后缀 10 11 def send_mail(to_list, sub, content): 12 me = "hello" + "<" + mail_user + "@" + mail_postfix + ">" 13 msg = MIMEText(content, _subtype=‘plain‘, _charset=‘gb2312‘) 14 msg[‘Subject‘] = sub 15 msg[‘From‘] = me 16 msg[‘To‘] = ";".join(to_list) 17 try: 18 server = smtplib.SMTP() 19 server.connect(mail_host) 20 server.login(mail_user, mail_pass) 21 server.sendmail(me, to_list, msg.as_string()) 22 server.close() 23 return True 24 except Exception as e: 25 print(str(e)) 26 return False 27 28 if __name__ == ‘__main__‘: 29 if send_mail(mailto_list, "hello", "hello world!"): 30 print("发送成功") 31 else: 32 print("发送失败")
以上是关于Python发邮件的小脚本的主要内容,如果未能解决你的问题,请参考以下文章