Search code examples
pythonemailgmailgmail-imap

Why can't I send emails to multiple recipients with this script?


Why can't I send emails to multiple recipients with this script?

I get no errors nor bouncebacks, and the first recipient does receive the email. None of the others do.

The script:

#!/usr/bin/python
import smtplib

SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587

recipient = '[email protected]; [email protected];'
sender = '[email protected]'
subject = 'the subject'
body = 'the body'
password = "password"
username = "[email protected]"

body = "" + body + ""

headers = ["From: " + sender,
           "Subject: " + subject,
           "To: " + recipient,
           "MIME-Version: 1.0",
           "Content-Type: text/html"]
headers = "\r\n".join(headers)

session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)

session.ehlo()
session.starttls()
session.ehlo
session.login(username, password)

session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()

Solution

  • Semicolons are not the correct separator for addresses in recipient headers. You must use commas.

    EDIT: I see now that you are using the library incorrectly. You're supplying a string which will always be interpreted as a single address. You must supply a list of addresses to send to multiple recipients.