Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Not Sending Email To Multiple Addresses

I can't see where i'm going wrong with this, I hope someone can spot the problem. I'd like to send an email to multiple addresses; however, it only sends it to the first email address in the list and not both. Here's the code:

import smtplib
from smtplib import SMTP

recipients = ['[email protected]', '[email protected]']

def send_email (message, status):
    fromaddr = '[email protected]'
    toaddrs = ", ".join(recipients)
    server = SMTP('smtp.gmail.com:587')
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login('example_username', 'example_pw')
    server.sendmail(fromaddr, toaddrs, 'Subject: %s\r\n%s' % (status, message))
    server.quit()

 send_email("message","subject")

Has anyone came across this error before?

Thank you for your time.

like image 900
Sam Perry Avatar asked Dec 11 '13 02:12

Sam Perry


Video Answer


2 Answers

Try to use this code, without your join:

import smtplib
from smtplib import SMTP

recipients = ['[email protected]', '[email protected]']

def send_email (message, status):
    fromaddr = '[email protected]'
    server = SMTP('smtp.gmail.com:587')
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login('example_username', 'example_pw')
    server.sendmail(fromaddr, recipients, 'Subject: %s\r\n%s' % (status, message))
    server.quit()

 send_email("message","subject")

Hope it helps!

like image 177
Sergio Ayestarán Avatar answered Sep 20 '22 15:09

Sergio Ayestarán


   import smtplib

   from email.mime.text import MIMEText

   s = smtplib.SMTP('xxx.xx')

   msg = MIMEText("""body""")
   sender = 'xx.xx.com'

   recipients = ['[email protected]', '[email protected]']

   msg['Subject'] = "test"
   msg['From'] = sender
   msg['To'] = ", ".join(recipients)
   s.sendmail(sender, recipients, msg.as_string())
like image 34
Tharanga Abeyseela Avatar answered Sep 18 '22 15:09

Tharanga Abeyseela