2

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@domain.com; email2@domain.com;'
sender = 'me@gmail.com'
subject = 'the subject'
body = 'the body'
password = "password"
username = "me@gmail.com"

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()
Trindaz
  • 16,049
  • 21
  • 77
  • 108

5 Answers5

8

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.

Kyle Jones
  • 5,387
  • 1
  • 20
  • 30
  • Using commas did not solve the problem. Do you have a URL for a specification on standard email header composition? – Trindaz Jan 24 '12 at 23:07
  • Thanks Kyle. I hadn't picked up on that because I assumed that because viewing the email 'details' in the GMail account of the first recipient showed the second recipient as having been CC'd that it was a problem at GMail's end. – Trindaz Jan 24 '12 at 23:33
2

Recipients in the standard header must be separated by commas, not semicolons. I blame Microsoft Outlook for leading people to believe otherwise.

Mark Ransom
  • 286,393
  • 40
  • 379
  • 604
  • Using commas did not solve the problem. Do you have a URL for a specification on standard email header composition? – Trindaz Jan 24 '12 at 23:07
  • [RFC 2822](http://www.ietf.org/rfc/rfc2822.txt) section 3.6.3, which refers to an address-list, which is defined as `address-list = (address *("," address)) / obs-addr-list`. obs-addr-list uses commas, too. – Kyle Jones Jan 24 '12 at 23:12
  • @KyleJones, thanks for the link. Section 3.4 will be useful also since it defines `address-list`. – Mark Ransom Jan 24 '12 at 23:19
  • @Trindaz, please see the link added by Kyle Jones. – Mark Ransom Jan 24 '12 at 23:20
0

Change this in your code:

recipient = ['email@domain.com','email2@domain.com']

headers = ",".join(headers)

session.sendmail(sender, recipient.split(","), headers + "\r\n\r\n" + body)
Roman Luštrik
  • 67,056
  • 24
  • 151
  • 191
0

Alternatively

recipient = ', '.join(recipient.split('; '))

if your recipients are a string of semicolon separated addresses.

TheoretiCAL
  • 17,309
  • 8
  • 36
  • 62
0

You can, just put the emails in an array, and loop through the array for each email as in: (my python is rusty... so forgive my syntax)

foreach recipient in recipients
    headers = ["From: " + sender, "Subject: " + subject, "To: " + recipient, "MIME-Version: 1.0",  "Content-Type: text/html"]
    headers = "\r\n".join(headers)
    session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
Eric Hodonsky
  • 4,928
  • 4
  • 24
  • 36