1. 程式人生 > >Python3.5同時給多人傳送純文字郵件

Python3.5同時給多人傳送純文字郵件

關鍵點1:收件人郵箱msg_to=['[email protected]','[email protected]','[email protected]'],以列表的方式給出。

關鍵點2:msg['To'] =','.join(msg_to)。

關鍵點3:s.sendmail(msg_from, msg['To'].split(','), msg.as_string())至於join()和split()大家可以看文件明白含義用法,處理好這三個關鍵點就可以成功利用python傳送郵件給多人了。
例項:

法一:

import smtplib
from email.mime.text import MIMEText
sender='
[email protected]
' #傳送郵件的郵箱 pwd ='indqrfdfsdsvbxxx'# 傳送方郵箱的授權碼 碼:Zhang1995 [email protected],[email protected], receivers=['[email protected],[email protected]']#接收方的郵箱 message =MIMEText('我要群發給幾個人','plain','utf-8')#第二個plain為設定文字格式 #設定傳送已經接受郵件的郵件頭部資訊, 主題名 message['From']=sender message['To']=','.join(receivers) #郵件主題 subject='我是' message['Subject']=Header(subject,'utf-8') try: #使用非本地伺服器,需要建立ssl連線 # smtpObj = smtplib.SMTP_SSL('smtp.163.com',465) smtpObj = smtplib.SMTP_SSL('smtp.qq.com',465) # smtpObj= smtplib.SMTP('localhost') smtpObj.login(sender,pwd) smtpObj.sendmail(sender,message['To'].split(','),message.as_string()) print('郵件傳送成功') except smtplib.SMTPException as e: print('ERROR:無法傳送郵件.Case:%s'%e)

測試成功,倆人都可以收到郵件。