使用sys.argv将电子邮件发送到多个电子邮件地址

时间:2018-10-31 16:26:54

标签: python python-3.x python-2.7 smtplib

import sys
import datetime

me = sys.argv[1]
you = sys.argv[2]  #[I want to pass here multiple recipient]
subject = datetime.now().strftime("%I %P")

# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Status Update ' + str(subject)
msgRoot['From'] = me
msgRoot['To'] = you
#msgRoot['To'] = ",".join(you) 
"""Earlier I was using something like above when I was hardcoding email address insided it"""

如果我需要使用toaddr传递多个收件人的sys.argv,请问有人可以建议我使用哪种最佳方法?

1 个答案:

答案 0 :(得分:0)

使用", "(逗号和空格)和命令行参数切片来加入所有地址

msgRoot['To'] = ", ".join(sys.argv[2:])

(改编自How to send email to multiple recipients using python smtplib?