1. 程式人生 > >python傳送郵件給多人

python傳送郵件給多人

親測,163郵箱不能傳送,我用的qq的

import smtplib
from email.mime.text import MIMEText

sender = '[email protected]'
passwd = '****'
receiver = ["[email protected]","[email protected]"] # 多個收件人放在一個list裡面

content = 'hello world'
subject = 'this is a test'

msg = MIMEText(content, 'plain', 'utf-8'
) # 中文需引數‘utf-8’,單位元組字元不需要 msg['Subject'] = subject msg['From'] = sender msg['To'] = ','.join(receiver) # 這裡必須要把多個郵箱按照逗號拼接為字串 server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 親測qq郵箱才有用,163沒用 server.login(sender,passwd) try: server.sendmail(sender, receiver, msg.as_string()) print('email sent'
) except smtplib.SMTPException as e: print(e)