python發送郵件至多人
阿新 • • 發佈:2018-08-24
多個 授權 fin mtp lur code 收件人 import 郵箱
本文轉載自 https://www.cnblogs.com/lurenjia1994/p/9485997.html
python2發送郵件,demo腳本使用的是qq郵箱發送,需要申請授權碼(並非密碼)
#!/usr/bin/env python
# coding:utf-8
# @File : demo.py
import smtplib, sys from email.mime.text import MIMEText class Msmtp(): def __init__(self, target, subject, content): # 收件人、標題、內容 self.msg_from = ‘發件人QQ郵箱@qq.com‘ # 郵件發送者 self.password = ‘發件人QQ郵箱授權碼‘ self.sender = smtplib.SMTP_SSL("smtp.qq.com", 465) self.msg_to = target.split(",") print self.msg_to self.subject = subject self.content = content def _login(self): self.sender.login(self.msg_from, self.password)def _msg(self): self.msg = MIMEText(self.content) # 此處可選擇文本格式或html等格式, 顯示發送信息 self.msg[‘Subject‘] = self.subject self.msg[‘From‘] = self.msg_from self.msg[‘To‘] = ",".join(self.msg_to) def send_mail(self): try: self._login() self._msg()# sendmail 第二個參數,目的郵箱,參數類型 str 或者 list self.sender.sendmail(self.msg_from, self.msg_to, self.msg.as_string()) except Exception, e: print u‘郵件發送失敗,原因:{}‘.format( e) else: print u‘郵件發送至 {} 成功!‘.format(self.msg[‘To‘]) finally: self.sender.quit() if __name__ == ‘__main__‘: # 收件人, 標題, 內容 a = Msmtp(sys.argv[1], sys.argv[2], sys.argv[3]) a.send_mail()
調用方式:
python demo.py 收件人(多個的話英文逗號隔開) 郵件標題 郵件正文
python demo.py [email protected],[email protected],[email protected] 我是標題 我是正文
python發送郵件至多人