Search code examples
pythonpython-3.xpython-2.7smtplib

Send email to multiple email address using sys.argv


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"""

Can some please suggest what a best way I can use if I need to pass toaddr of multiple recipient using sys.argv?


Solution

  • join all addresses using ", " (comma+space) and command line argument slicing

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

    (adapted from How to send email to multiple recipients using python smtplib?)