Search code examples
pythondjangosmtplib

How can I send email to multi addresses by smtplib?


I use bellow code I can send email to one account, but how can I send to multi account?

import smtplib
from email.mime.text import MIMEText

_user = "67676767@qq.com"  #  
_pwd = "orjxywulscbxsdwbaii"  #  
_to = linux213@126.com"  #  

msg = MIMEText("content")  #  
msg["Subject"] = "邮件主题测试"  #  
msg["From"] = _user
msg["To"] = _to

try:
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    s.login(_user, _pwd)
    s.sendmail(_user, _to, msg.as_string())
    s.quit()
    print ("Success!")
except smtplib.SMTPException as e:
    print ("Falied,%s" % e)

Solution

  • Try this:

    import smtplib
    from email.mime.text import MIMEText
    
    s = smtplib.SMTP('smtp.qq.com')
    s.set_debuglevel(1)
    msg = MIMEText("""body""")
    sender = 'me@example.com'
    recipients = ['k.ankur@abc.com', 'a.smith@abc.co.in']
    msg['Subject'] = "subject line"
    msg['From'] = sender
    msg['To'] = ", ".join(recipients)
    s.sendmail(sender, recipients, msg.as_string())