How to send email to multiple recipients using python smtplib?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
After much searching I couldn't find out how to use smtplib.sendmail to send to multiple recipients. The problem was every time the mail would be sent the mail headers would appear to contain multiple addresses, but in fact only the first recipient would receive the email.
The problem seems to be that the email.Message module expects something different than the smtplib.sendmail() function.
In short, to send to multiple recipients you should set the header to be a string of comma delimited email addresses. The sendmail() parameter to_addrs however should be a list of email addresses.
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib
msg = MIMEMultipart()
msg["Subject"] = "Example"
msg["From"] = "me@example.com"
msg["To"] = "malcom@example.com,reynolds@example.com,firefly@example.com"
msg["Cc"] = "serenity@example.com,inara@example.com"
body = MIMEText("example email body")
msg.attach(body)
smtp = smtplib.SMTP("mailhost.example.com", 25)
smtp.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
smtp.quit()
python email smtp message smtplib
add a comment |
After much searching I couldn't find out how to use smtplib.sendmail to send to multiple recipients. The problem was every time the mail would be sent the mail headers would appear to contain multiple addresses, but in fact only the first recipient would receive the email.
The problem seems to be that the email.Message module expects something different than the smtplib.sendmail() function.
In short, to send to multiple recipients you should set the header to be a string of comma delimited email addresses. The sendmail() parameter to_addrs however should be a list of email addresses.
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib
msg = MIMEMultipart()
msg["Subject"] = "Example"
msg["From"] = "me@example.com"
msg["To"] = "malcom@example.com,reynolds@example.com,firefly@example.com"
msg["Cc"] = "serenity@example.com,inara@example.com"
body = MIMEText("example email body")
msg.attach(body)
smtp = smtplib.SMTP("mailhost.example.com", 25)
smtp.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
smtp.quit()
python email smtp message smtplib
1
It appears OP answered his own question:sendmailneeds a list.
– Cees Timmerman
May 20 '15 at 8:35
possible duplicate of Is there any way to add multiple receivers in Python SMTPlib?
– Cees Timmerman
May 20 '15 at 8:42
Using Python3 I had to loop through recipients;for addr in recipients: msg['To'] = addrand then it worked. Multiple assignments actually appends a new 'To' header for each one. This is a very bizarre interface, I can't even explain how I thought to try it. I was even considering usingsubprocessto call the unixsendmailpackage to save my sanity before I figured this out.
– mehtunguh
Nov 7 '18 at 21:47
add a comment |
After much searching I couldn't find out how to use smtplib.sendmail to send to multiple recipients. The problem was every time the mail would be sent the mail headers would appear to contain multiple addresses, but in fact only the first recipient would receive the email.
The problem seems to be that the email.Message module expects something different than the smtplib.sendmail() function.
In short, to send to multiple recipients you should set the header to be a string of comma delimited email addresses. The sendmail() parameter to_addrs however should be a list of email addresses.
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib
msg = MIMEMultipart()
msg["Subject"] = "Example"
msg["From"] = "me@example.com"
msg["To"] = "malcom@example.com,reynolds@example.com,firefly@example.com"
msg["Cc"] = "serenity@example.com,inara@example.com"
body = MIMEText("example email body")
msg.attach(body)
smtp = smtplib.SMTP("mailhost.example.com", 25)
smtp.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
smtp.quit()
python email smtp message smtplib
After much searching I couldn't find out how to use smtplib.sendmail to send to multiple recipients. The problem was every time the mail would be sent the mail headers would appear to contain multiple addresses, but in fact only the first recipient would receive the email.
The problem seems to be that the email.Message module expects something different than the smtplib.sendmail() function.
In short, to send to multiple recipients you should set the header to be a string of comma delimited email addresses. The sendmail() parameter to_addrs however should be a list of email addresses.
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib
msg = MIMEMultipart()
msg["Subject"] = "Example"
msg["From"] = "me@example.com"
msg["To"] = "malcom@example.com,reynolds@example.com,firefly@example.com"
msg["Cc"] = "serenity@example.com,inara@example.com"
body = MIMEText("example email body")
msg.attach(body)
smtp = smtplib.SMTP("mailhost.example.com", 25)
smtp.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
smtp.quit()
python email smtp message smtplib
python email smtp message smtplib
edited Apr 29 '15 at 3:26
Martey
1,59811123
1,59811123
asked Jan 13 '12 at 19:28
user1148320user1148320
776263
776263
1
It appears OP answered his own question:sendmailneeds a list.
– Cees Timmerman
May 20 '15 at 8:35
possible duplicate of Is there any way to add multiple receivers in Python SMTPlib?
– Cees Timmerman
May 20 '15 at 8:42
Using Python3 I had to loop through recipients;for addr in recipients: msg['To'] = addrand then it worked. Multiple assignments actually appends a new 'To' header for each one. This is a very bizarre interface, I can't even explain how I thought to try it. I was even considering usingsubprocessto call the unixsendmailpackage to save my sanity before I figured this out.
– mehtunguh
Nov 7 '18 at 21:47
add a comment |
1
It appears OP answered his own question:sendmailneeds a list.
– Cees Timmerman
May 20 '15 at 8:35
possible duplicate of Is there any way to add multiple receivers in Python SMTPlib?
– Cees Timmerman
May 20 '15 at 8:42
Using Python3 I had to loop through recipients;for addr in recipients: msg['To'] = addrand then it worked. Multiple assignments actually appends a new 'To' header for each one. This is a very bizarre interface, I can't even explain how I thought to try it. I was even considering usingsubprocessto call the unixsendmailpackage to save my sanity before I figured this out.
– mehtunguh
Nov 7 '18 at 21:47
1
1
It appears OP answered his own question:
sendmail needs a list.– Cees Timmerman
May 20 '15 at 8:35
It appears OP answered his own question:
sendmail needs a list.– Cees Timmerman
May 20 '15 at 8:35
possible duplicate of Is there any way to add multiple receivers in Python SMTPlib?
– Cees Timmerman
May 20 '15 at 8:42
possible duplicate of Is there any way to add multiple receivers in Python SMTPlib?
– Cees Timmerman
May 20 '15 at 8:42
Using Python3 I had to loop through recipients;
for addr in recipients: msg['To'] = addr and then it worked. Multiple assignments actually appends a new 'To' header for each one. This is a very bizarre interface, I can't even explain how I thought to try it. I was even considering using subprocess to call the unix sendmail package to save my sanity before I figured this out.– mehtunguh
Nov 7 '18 at 21:47
Using Python3 I had to loop through recipients;
for addr in recipients: msg['To'] = addr and then it worked. Multiple assignments actually appends a new 'To' header for each one. This is a very bizarre interface, I can't even explain how I thought to try it. I was even considering using subprocess to call the unix sendmail package to save my sanity before I figured this out.– mehtunguh
Nov 7 '18 at 21:47
add a comment |
13 Answers
13
active
oldest
votes
This really works, I spent a lot of time trying multiple variants.
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = ['john.doe@example.com', 'john.smith@example.co.uk']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())
3
the documentation does have the example:tolist =["one@one.org","two@two.org","three@three.org","four@four.org"]
– chug2k
Sep 23 '13 at 22:27
1
thank you @sorin for this script. I was having a problem to send an email from a python script and with this piece of code, i can now send the email.
– fear_matrix
Jul 14 '15 at 10:36
2
This will not send to multiple recipients if you are using Python 3 you needsend_messageinstead ofsendmailas per Antoine's comment below and the Python docs docs.python.org/3/library/email-examples.html
– cardamom
Jun 8 '17 at 16:35
You have to use for each traverse that recipients for sendmail, otherwise only first element will receive the mail.
– Johnny
Aug 10 '17 at 13:29
2
correction to the url mentioned above: docs.python.org/3/library/email.examples.html
– David
Oct 18 '17 at 14:41
|
show 1 more comment
The msg['To'] needs to be a string:
msg['To'] = "a@b.com, b@b.com, c@b.com"
While the recipients in sendmail(sender, recipients, message) needs to be a list:
sendmail("a@a.com", ["a@b.com", "b@b.com", "c@b.com"], "Howdy")
This is one strange design decision forsmtplib.
– Adam Matan
Jul 26 '15 at 7:43
2
recipientsdoes not have to be a list - if a string is given, it is treated as a list with one element. Themsg['To']string can simply be omitted.
– Suzana
Dec 29 '15 at 15:47
I don't really understand, how 'a@a.com, b@b.com' is parsed so only the first address gets the email. But, thanks! This is the answer, had to put list in there.
– antonavy
Nov 25 '16 at 13:19
worked for me, and it is consistent with documentation in docs.python.org/2/library/email-examples.html
– Rodrigo Laguna
Mar 20 '17 at 18:11
add a comment |
You need to understand the difference between the visible address of an email, and the delivery.
msg["To"] is essentially what is printed on the letter. It doesn't actually have any effect. Except that your email client, just like the regular post officer, will assume that this is who you want to send the email to.
The actual delivery however can work quite different. So you can drop the email (or a copy) into the post box of someone completely different.
There are various reasons for this. For example forwarding. The To: header field doesn't change on forwarding, however the email is dropped into a different mailbox.
The smtp.sendmail command now takes care of the actual delivery. email.Message is the contents of the letter only, not the delivery.
In low-level SMTP, you need to give the receipients one-by-one, which is why a list of adresses (not including names!) is the sensible API.
For the header, it can also contain for example the name, e.g. To: First Last <email@addr.tld>, Other User <other@mail.tld>. Your code example therefore is not recommended, as it will fail delivering this mail, since just by splitting it on , you still not not have the valid adresses!
2
RFC 2822 imposes a maximum width of 988 characters for a given header and a recommended width of 78 characters. You will need to ensure you "fold" the header if you have too many addresses.
– Steve Hunt
Jun 11 '14 at 21:53
This should be the accepted answer, as it actually explains the why and the how.
– Serrano
Sep 1 '16 at 12:34
Great answer. What about CC and BCC email fields? I assume we also have to include CC and BCC email in smtp.send. And only CC list (and not BCC list) in the msg fields?
– Tagar
May 25 '17 at 2:59
Yes, that is how it works. Mail servers will likely drop the BCC field (to prevent this from being visible, and I don't think they all do), but they won't parse it.
– Anony-Mousse
May 25 '17 at 7:11
add a comment |
It works for me.
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = 'john.doe@example.com,john.smith@example.co.uk'
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = recipients
s.sendmail(sender, recipients.split(','), msg.as_string())
what version of python are you using? I get the same problem as the original poster and I am using python 2.7.9
– panofish
Oct 25 '17 at 16:41
Why not simplyrecipients = ['john.doe@example.com','john.smith@example.co.uk']instead of making it a string, and then split it to make a list?
– WoJ
Apr 16 at 17:48
add a comment |
I tried the below and it worked like a charm :)
rec_list = ['first@example.com', 'second@example.com']
rec = ', '.join(rec_list)
msg['To'] = rec
send_out = smtplib.SMTP('localhost')
send_out.sendmail(me, rec_list, msg.as_string())
FYR whole simple code below:import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText sender = 'myEmailAddress@example.com' rec_list = ['first@example.com', 'second@example.com'] rec = ', '.join(rec_list) msg = MIMEMultipart('alternative') msg['Subject'] = 'The required subject' msg['From'] = sender msg['To'] = rec html = ('whatever html code') htm_part = MIMEText(html, 'html') msg.attach(htm_part) send_out = smtplib.SMTP('localhost') send_out.sendmail(sender, rec_list, msg.as_string()) send_out.quit()
– TopSecret_007
Nov 24 '17 at 4:50
add a comment |
I came up with this importable module function. It uses the gmail email server in this example. Its split into header and message so you can clearly see whats going on:
import smtplib
def send_alert(subject=""):
to = ['email@one.com', 'email2@another_email.com', 'a3rd@email.com']
gmail_user = 'me@gmail.com'
gmail_pwd = 'my_pass'
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + ", ".join(to) + 'n' + 'From: ' + gmail_user + 'n' + 'Subject: ' + subject + 'n'
msg = header + 'n' + subject + 'nn'
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
add a comment |
I figured this out a few months back and blogged about it. The summary is:
If you want to use smtplib to send email to multiple recipients, use email.Message.add_header('To', eachRecipientAsString) to add them, and then when you invoke the sendmail method, use email.Message.get_all('To') send the message to all of them. Ditto for Cc and Bcc recipients.
Python 3.7 throws an exception with message: Exception has occurred: ValueError There may be at most 1 To headers in a message
– Wojciech Jakubas
Oct 19 '18 at 13:47
add a comment |
So actually the problem is that SMTP.sendmail and email.MIMEText need two different things.
email.MIMEText sets up the "To:" header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)
SMTP.sendmail, on the other hand, sets up the "envelope" of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.
So, what you need to do is COMBINE the two replies you received. Set msg['To'] to a single string, but pass the raw list to sendmail:
emails = ['a.com','b.com', 'c.com']
msg['To'] = ', '.join( emails )
....
s.sendmail( msg['From'], emails, msg.as_string())
add a comment |
Well, the method in this asnwer method did not work for me. I don't know, maybe this is a Python3 (I am using the 3.4 version) or gmail related issue, but after some tries, the solution that worked for me, was the line
s.send_message(msg)
instead of
s.sendmail(sender, recipients, msg.as_string())
add a comment |
I use python 3.6 and the following code works for me
email_send = 'xxxxx@xxx.xxx,xxxx@xxx.xxx'
server.sendmail(email_user,email_send.split(','),text)
add a comment |
Below worked for me.
It sends email to multiple with attachment - "To", "Cc" & "Bcc" successfully.
toaddr = ['mailid_1','mailid_2']
cc = ['mailid_3','mailid_4']
bcc = ['mailid_5','mailid_6']
subject = 'Email from Python Code'
fromaddr = 'sender_mailid'
message = "n !! Hello... !!"
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
msg['Subject'] = subject
s.sendmail(fromaddr, (toaddr+cc+bcc) , message)
add a comment |
you can try this when you write the recpient emails on a text file
from email.mime.text import MIMEText
from email.header import Header
import smtplib
f = open('emails.txt', 'r').readlines()
for n in f:
emails = n.rstrip()
server = smtplib.SMTP('smtp.uk.xensource.com')
server.ehlo()
server.starttls()
body = "Test Email"
subject = "Test"
from = "me@example.com"
to = emails
msg = MIMEText(body,'plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = Header(from, 'utf-8')
msg['To'] = Header(to, 'utf-8')
text = msg.as_string()
try:
server.send(from, emails, text)
print('Message Sent Succesfully')
except:
print('There Was An Error While Sending The Message')
add a comment |
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sender(recipients):
body = 'Your email content here'
msg = MIMEMultipart()
msg['Subject'] = 'Email Subject'
msg['From'] = 'your.email@gmail.com'
msg['To'] = (', ').join(recipients.split(','))
msg.attach(MIMEText(body,'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('your.email@gmail.com', 'yourpassword')
server.send_message(msg)
server.quit()
if __name__ == '__main__':
sender('email_1@domain.com,email_2@domain.com')
It only worked for me with send_message function and using the join function in the list whith recipients, python 3.6.
add a comment |
protected by miken32 Jan 30 at 22:28
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
This really works, I spent a lot of time trying multiple variants.
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = ['john.doe@example.com', 'john.smith@example.co.uk']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())
3
the documentation does have the example:tolist =["one@one.org","two@two.org","three@three.org","four@four.org"]
– chug2k
Sep 23 '13 at 22:27
1
thank you @sorin for this script. I was having a problem to send an email from a python script and with this piece of code, i can now send the email.
– fear_matrix
Jul 14 '15 at 10:36
2
This will not send to multiple recipients if you are using Python 3 you needsend_messageinstead ofsendmailas per Antoine's comment below and the Python docs docs.python.org/3/library/email-examples.html
– cardamom
Jun 8 '17 at 16:35
You have to use for each traverse that recipients for sendmail, otherwise only first element will receive the mail.
– Johnny
Aug 10 '17 at 13:29
2
correction to the url mentioned above: docs.python.org/3/library/email.examples.html
– David
Oct 18 '17 at 14:41
|
show 1 more comment
This really works, I spent a lot of time trying multiple variants.
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = ['john.doe@example.com', 'john.smith@example.co.uk']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())
3
the documentation does have the example:tolist =["one@one.org","two@two.org","three@three.org","four@four.org"]
– chug2k
Sep 23 '13 at 22:27
1
thank you @sorin for this script. I was having a problem to send an email from a python script and with this piece of code, i can now send the email.
– fear_matrix
Jul 14 '15 at 10:36
2
This will not send to multiple recipients if you are using Python 3 you needsend_messageinstead ofsendmailas per Antoine's comment below and the Python docs docs.python.org/3/library/email-examples.html
– cardamom
Jun 8 '17 at 16:35
You have to use for each traverse that recipients for sendmail, otherwise only first element will receive the mail.
– Johnny
Aug 10 '17 at 13:29
2
correction to the url mentioned above: docs.python.org/3/library/email.examples.html
– David
Oct 18 '17 at 14:41
|
show 1 more comment
This really works, I spent a lot of time trying multiple variants.
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = ['john.doe@example.com', 'john.smith@example.co.uk']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())
This really works, I spent a lot of time trying multiple variants.
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = ['john.doe@example.com', 'john.smith@example.co.uk']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())
answered Sep 14 '12 at 10:44
sorinsorin
77.4k117377590
77.4k117377590
3
the documentation does have the example:tolist =["one@one.org","two@two.org","three@three.org","four@four.org"]
– chug2k
Sep 23 '13 at 22:27
1
thank you @sorin for this script. I was having a problem to send an email from a python script and with this piece of code, i can now send the email.
– fear_matrix
Jul 14 '15 at 10:36
2
This will not send to multiple recipients if you are using Python 3 you needsend_messageinstead ofsendmailas per Antoine's comment below and the Python docs docs.python.org/3/library/email-examples.html
– cardamom
Jun 8 '17 at 16:35
You have to use for each traverse that recipients for sendmail, otherwise only first element will receive the mail.
– Johnny
Aug 10 '17 at 13:29
2
correction to the url mentioned above: docs.python.org/3/library/email.examples.html
– David
Oct 18 '17 at 14:41
|
show 1 more comment
3
the documentation does have the example:tolist =["one@one.org","two@two.org","three@three.org","four@four.org"]
– chug2k
Sep 23 '13 at 22:27
1
thank you @sorin for this script. I was having a problem to send an email from a python script and with this piece of code, i can now send the email.
– fear_matrix
Jul 14 '15 at 10:36
2
This will not send to multiple recipients if you are using Python 3 you needsend_messageinstead ofsendmailas per Antoine's comment below and the Python docs docs.python.org/3/library/email-examples.html
– cardamom
Jun 8 '17 at 16:35
You have to use for each traverse that recipients for sendmail, otherwise only first element will receive the mail.
– Johnny
Aug 10 '17 at 13:29
2
correction to the url mentioned above: docs.python.org/3/library/email.examples.html
– David
Oct 18 '17 at 14:41
3
3
the documentation does have the example:
tolist =["one@one.org","two@two.org","three@three.org","four@four.org"]– chug2k
Sep 23 '13 at 22:27
the documentation does have the example:
tolist =["one@one.org","two@two.org","three@three.org","four@four.org"]– chug2k
Sep 23 '13 at 22:27
1
1
thank you @sorin for this script. I was having a problem to send an email from a python script and with this piece of code, i can now send the email.
– fear_matrix
Jul 14 '15 at 10:36
thank you @sorin for this script. I was having a problem to send an email from a python script and with this piece of code, i can now send the email.
– fear_matrix
Jul 14 '15 at 10:36
2
2
This will not send to multiple recipients if you are using Python 3 you need
send_message instead of sendmail as per Antoine's comment below and the Python docs docs.python.org/3/library/email-examples.html– cardamom
Jun 8 '17 at 16:35
This will not send to multiple recipients if you are using Python 3 you need
send_message instead of sendmail as per Antoine's comment below and the Python docs docs.python.org/3/library/email-examples.html– cardamom
Jun 8 '17 at 16:35
You have to use for each traverse that recipients for sendmail, otherwise only first element will receive the mail.
– Johnny
Aug 10 '17 at 13:29
You have to use for each traverse that recipients for sendmail, otherwise only first element will receive the mail.
– Johnny
Aug 10 '17 at 13:29
2
2
correction to the url mentioned above: docs.python.org/3/library/email.examples.html
– David
Oct 18 '17 at 14:41
correction to the url mentioned above: docs.python.org/3/library/email.examples.html
– David
Oct 18 '17 at 14:41
|
show 1 more comment
The msg['To'] needs to be a string:
msg['To'] = "a@b.com, b@b.com, c@b.com"
While the recipients in sendmail(sender, recipients, message) needs to be a list:
sendmail("a@a.com", ["a@b.com", "b@b.com", "c@b.com"], "Howdy")
This is one strange design decision forsmtplib.
– Adam Matan
Jul 26 '15 at 7:43
2
recipientsdoes not have to be a list - if a string is given, it is treated as a list with one element. Themsg['To']string can simply be omitted.
– Suzana
Dec 29 '15 at 15:47
I don't really understand, how 'a@a.com, b@b.com' is parsed so only the first address gets the email. But, thanks! This is the answer, had to put list in there.
– antonavy
Nov 25 '16 at 13:19
worked for me, and it is consistent with documentation in docs.python.org/2/library/email-examples.html
– Rodrigo Laguna
Mar 20 '17 at 18:11
add a comment |
The msg['To'] needs to be a string:
msg['To'] = "a@b.com, b@b.com, c@b.com"
While the recipients in sendmail(sender, recipients, message) needs to be a list:
sendmail("a@a.com", ["a@b.com", "b@b.com", "c@b.com"], "Howdy")
This is one strange design decision forsmtplib.
– Adam Matan
Jul 26 '15 at 7:43
2
recipientsdoes not have to be a list - if a string is given, it is treated as a list with one element. Themsg['To']string can simply be omitted.
– Suzana
Dec 29 '15 at 15:47
I don't really understand, how 'a@a.com, b@b.com' is parsed so only the first address gets the email. But, thanks! This is the answer, had to put list in there.
– antonavy
Nov 25 '16 at 13:19
worked for me, and it is consistent with documentation in docs.python.org/2/library/email-examples.html
– Rodrigo Laguna
Mar 20 '17 at 18:11
add a comment |
The msg['To'] needs to be a string:
msg['To'] = "a@b.com, b@b.com, c@b.com"
While the recipients in sendmail(sender, recipients, message) needs to be a list:
sendmail("a@a.com", ["a@b.com", "b@b.com", "c@b.com"], "Howdy")
The msg['To'] needs to be a string:
msg['To'] = "a@b.com, b@b.com, c@b.com"
While the recipients in sendmail(sender, recipients, message) needs to be a list:
sendmail("a@a.com", ["a@b.com", "b@b.com", "c@b.com"], "Howdy")
edited Feb 15 '15 at 3:37
Adrian Petrescu
9,94044874
9,94044874
answered Jan 28 '15 at 22:43
dvdhnsdvdhns
1,6591811
1,6591811
This is one strange design decision forsmtplib.
– Adam Matan
Jul 26 '15 at 7:43
2
recipientsdoes not have to be a list - if a string is given, it is treated as a list with one element. Themsg['To']string can simply be omitted.
– Suzana
Dec 29 '15 at 15:47
I don't really understand, how 'a@a.com, b@b.com' is parsed so only the first address gets the email. But, thanks! This is the answer, had to put list in there.
– antonavy
Nov 25 '16 at 13:19
worked for me, and it is consistent with documentation in docs.python.org/2/library/email-examples.html
– Rodrigo Laguna
Mar 20 '17 at 18:11
add a comment |
This is one strange design decision forsmtplib.
– Adam Matan
Jul 26 '15 at 7:43
2
recipientsdoes not have to be a list - if a string is given, it is treated as a list with one element. Themsg['To']string can simply be omitted.
– Suzana
Dec 29 '15 at 15:47
I don't really understand, how 'a@a.com, b@b.com' is parsed so only the first address gets the email. But, thanks! This is the answer, had to put list in there.
– antonavy
Nov 25 '16 at 13:19
worked for me, and it is consistent with documentation in docs.python.org/2/library/email-examples.html
– Rodrigo Laguna
Mar 20 '17 at 18:11
This is one strange design decision for
smtplib.– Adam Matan
Jul 26 '15 at 7:43
This is one strange design decision for
smtplib.– Adam Matan
Jul 26 '15 at 7:43
2
2
recipients does not have to be a list - if a string is given, it is treated as a list with one element. Themsg['To'] string can simply be omitted.– Suzana
Dec 29 '15 at 15:47
recipients does not have to be a list - if a string is given, it is treated as a list with one element. Themsg['To'] string can simply be omitted.– Suzana
Dec 29 '15 at 15:47
I don't really understand, how 'a@a.com, b@b.com' is parsed so only the first address gets the email. But, thanks! This is the answer, had to put list in there.
– antonavy
Nov 25 '16 at 13:19
I don't really understand, how 'a@a.com, b@b.com' is parsed so only the first address gets the email. But, thanks! This is the answer, had to put list in there.
– antonavy
Nov 25 '16 at 13:19
worked for me, and it is consistent with documentation in docs.python.org/2/library/email-examples.html
– Rodrigo Laguna
Mar 20 '17 at 18:11
worked for me, and it is consistent with documentation in docs.python.org/2/library/email-examples.html
– Rodrigo Laguna
Mar 20 '17 at 18:11
add a comment |
You need to understand the difference between the visible address of an email, and the delivery.
msg["To"] is essentially what is printed on the letter. It doesn't actually have any effect. Except that your email client, just like the regular post officer, will assume that this is who you want to send the email to.
The actual delivery however can work quite different. So you can drop the email (or a copy) into the post box of someone completely different.
There are various reasons for this. For example forwarding. The To: header field doesn't change on forwarding, however the email is dropped into a different mailbox.
The smtp.sendmail command now takes care of the actual delivery. email.Message is the contents of the letter only, not the delivery.
In low-level SMTP, you need to give the receipients one-by-one, which is why a list of adresses (not including names!) is the sensible API.
For the header, it can also contain for example the name, e.g. To: First Last <email@addr.tld>, Other User <other@mail.tld>. Your code example therefore is not recommended, as it will fail delivering this mail, since just by splitting it on , you still not not have the valid adresses!
2
RFC 2822 imposes a maximum width of 988 characters for a given header and a recommended width of 78 characters. You will need to ensure you "fold" the header if you have too many addresses.
– Steve Hunt
Jun 11 '14 at 21:53
This should be the accepted answer, as it actually explains the why and the how.
– Serrano
Sep 1 '16 at 12:34
Great answer. What about CC and BCC email fields? I assume we also have to include CC and BCC email in smtp.send. And only CC list (and not BCC list) in the msg fields?
– Tagar
May 25 '17 at 2:59
Yes, that is how it works. Mail servers will likely drop the BCC field (to prevent this from being visible, and I don't think they all do), but they won't parse it.
– Anony-Mousse
May 25 '17 at 7:11
add a comment |
You need to understand the difference between the visible address of an email, and the delivery.
msg["To"] is essentially what is printed on the letter. It doesn't actually have any effect. Except that your email client, just like the regular post officer, will assume that this is who you want to send the email to.
The actual delivery however can work quite different. So you can drop the email (or a copy) into the post box of someone completely different.
There are various reasons for this. For example forwarding. The To: header field doesn't change on forwarding, however the email is dropped into a different mailbox.
The smtp.sendmail command now takes care of the actual delivery. email.Message is the contents of the letter only, not the delivery.
In low-level SMTP, you need to give the receipients one-by-one, which is why a list of adresses (not including names!) is the sensible API.
For the header, it can also contain for example the name, e.g. To: First Last <email@addr.tld>, Other User <other@mail.tld>. Your code example therefore is not recommended, as it will fail delivering this mail, since just by splitting it on , you still not not have the valid adresses!
2
RFC 2822 imposes a maximum width of 988 characters for a given header and a recommended width of 78 characters. You will need to ensure you "fold" the header if you have too many addresses.
– Steve Hunt
Jun 11 '14 at 21:53
This should be the accepted answer, as it actually explains the why and the how.
– Serrano
Sep 1 '16 at 12:34
Great answer. What about CC and BCC email fields? I assume we also have to include CC and BCC email in smtp.send. And only CC list (and not BCC list) in the msg fields?
– Tagar
May 25 '17 at 2:59
Yes, that is how it works. Mail servers will likely drop the BCC field (to prevent this from being visible, and I don't think they all do), but they won't parse it.
– Anony-Mousse
May 25 '17 at 7:11
add a comment |
You need to understand the difference between the visible address of an email, and the delivery.
msg["To"] is essentially what is printed on the letter. It doesn't actually have any effect. Except that your email client, just like the regular post officer, will assume that this is who you want to send the email to.
The actual delivery however can work quite different. So you can drop the email (or a copy) into the post box of someone completely different.
There are various reasons for this. For example forwarding. The To: header field doesn't change on forwarding, however the email is dropped into a different mailbox.
The smtp.sendmail command now takes care of the actual delivery. email.Message is the contents of the letter only, not the delivery.
In low-level SMTP, you need to give the receipients one-by-one, which is why a list of adresses (not including names!) is the sensible API.
For the header, it can also contain for example the name, e.g. To: First Last <email@addr.tld>, Other User <other@mail.tld>. Your code example therefore is not recommended, as it will fail delivering this mail, since just by splitting it on , you still not not have the valid adresses!
You need to understand the difference between the visible address of an email, and the delivery.
msg["To"] is essentially what is printed on the letter. It doesn't actually have any effect. Except that your email client, just like the regular post officer, will assume that this is who you want to send the email to.
The actual delivery however can work quite different. So you can drop the email (or a copy) into the post box of someone completely different.
There are various reasons for this. For example forwarding. The To: header field doesn't change on forwarding, however the email is dropped into a different mailbox.
The smtp.sendmail command now takes care of the actual delivery. email.Message is the contents of the letter only, not the delivery.
In low-level SMTP, you need to give the receipients one-by-one, which is why a list of adresses (not including names!) is the sensible API.
For the header, it can also contain for example the name, e.g. To: First Last <email@addr.tld>, Other User <other@mail.tld>. Your code example therefore is not recommended, as it will fail delivering this mail, since just by splitting it on , you still not not have the valid adresses!
answered Jan 14 '12 at 11:06
Anony-MousseAnony-Mousse
59.5k798163
59.5k798163
2
RFC 2822 imposes a maximum width of 988 characters for a given header and a recommended width of 78 characters. You will need to ensure you "fold" the header if you have too many addresses.
– Steve Hunt
Jun 11 '14 at 21:53
This should be the accepted answer, as it actually explains the why and the how.
– Serrano
Sep 1 '16 at 12:34
Great answer. What about CC and BCC email fields? I assume we also have to include CC and BCC email in smtp.send. And only CC list (and not BCC list) in the msg fields?
– Tagar
May 25 '17 at 2:59
Yes, that is how it works. Mail servers will likely drop the BCC field (to prevent this from being visible, and I don't think they all do), but they won't parse it.
– Anony-Mousse
May 25 '17 at 7:11
add a comment |
2
RFC 2822 imposes a maximum width of 988 characters for a given header and a recommended width of 78 characters. You will need to ensure you "fold" the header if you have too many addresses.
– Steve Hunt
Jun 11 '14 at 21:53
This should be the accepted answer, as it actually explains the why and the how.
– Serrano
Sep 1 '16 at 12:34
Great answer. What about CC and BCC email fields? I assume we also have to include CC and BCC email in smtp.send. And only CC list (and not BCC list) in the msg fields?
– Tagar
May 25 '17 at 2:59
Yes, that is how it works. Mail servers will likely drop the BCC field (to prevent this from being visible, and I don't think they all do), but they won't parse it.
– Anony-Mousse
May 25 '17 at 7:11
2
2
RFC 2822 imposes a maximum width of 988 characters for a given header and a recommended width of 78 characters. You will need to ensure you "fold" the header if you have too many addresses.
– Steve Hunt
Jun 11 '14 at 21:53
RFC 2822 imposes a maximum width of 988 characters for a given header and a recommended width of 78 characters. You will need to ensure you "fold" the header if you have too many addresses.
– Steve Hunt
Jun 11 '14 at 21:53
This should be the accepted answer, as it actually explains the why and the how.
– Serrano
Sep 1 '16 at 12:34
This should be the accepted answer, as it actually explains the why and the how.
– Serrano
Sep 1 '16 at 12:34
Great answer. What about CC and BCC email fields? I assume we also have to include CC and BCC email in smtp.send. And only CC list (and not BCC list) in the msg fields?
– Tagar
May 25 '17 at 2:59
Great answer. What about CC and BCC email fields? I assume we also have to include CC and BCC email in smtp.send. And only CC list (and not BCC list) in the msg fields?
– Tagar
May 25 '17 at 2:59
Yes, that is how it works. Mail servers will likely drop the BCC field (to prevent this from being visible, and I don't think they all do), but they won't parse it.
– Anony-Mousse
May 25 '17 at 7:11
Yes, that is how it works. Mail servers will likely drop the BCC field (to prevent this from being visible, and I don't think they all do), but they won't parse it.
– Anony-Mousse
May 25 '17 at 7:11
add a comment |
It works for me.
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = 'john.doe@example.com,john.smith@example.co.uk'
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = recipients
s.sendmail(sender, recipients.split(','), msg.as_string())
what version of python are you using? I get the same problem as the original poster and I am using python 2.7.9
– panofish
Oct 25 '17 at 16:41
Why not simplyrecipients = ['john.doe@example.com','john.smith@example.co.uk']instead of making it a string, and then split it to make a list?
– WoJ
Apr 16 at 17:48
add a comment |
It works for me.
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = 'john.doe@example.com,john.smith@example.co.uk'
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = recipients
s.sendmail(sender, recipients.split(','), msg.as_string())
what version of python are you using? I get the same problem as the original poster and I am using python 2.7.9
– panofish
Oct 25 '17 at 16:41
Why not simplyrecipients = ['john.doe@example.com','john.smith@example.co.uk']instead of making it a string, and then split it to make a list?
– WoJ
Apr 16 at 17:48
add a comment |
It works for me.
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = 'john.doe@example.com,john.smith@example.co.uk'
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = recipients
s.sendmail(sender, recipients.split(','), msg.as_string())
It works for me.
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = 'john.doe@example.com,john.smith@example.co.uk'
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = recipients
s.sendmail(sender, recipients.split(','), msg.as_string())
answered Jul 7 '16 at 7:37
coolguycoolguy
14113
14113
what version of python are you using? I get the same problem as the original poster and I am using python 2.7.9
– panofish
Oct 25 '17 at 16:41
Why not simplyrecipients = ['john.doe@example.com','john.smith@example.co.uk']instead of making it a string, and then split it to make a list?
– WoJ
Apr 16 at 17:48
add a comment |
what version of python are you using? I get the same problem as the original poster and I am using python 2.7.9
– panofish
Oct 25 '17 at 16:41
Why not simplyrecipients = ['john.doe@example.com','john.smith@example.co.uk']instead of making it a string, and then split it to make a list?
– WoJ
Apr 16 at 17:48
what version of python are you using? I get the same problem as the original poster and I am using python 2.7.9
– panofish
Oct 25 '17 at 16:41
what version of python are you using? I get the same problem as the original poster and I am using python 2.7.9
– panofish
Oct 25 '17 at 16:41
Why not simply
recipients = ['john.doe@example.com','john.smith@example.co.uk'] instead of making it a string, and then split it to make a list?– WoJ
Apr 16 at 17:48
Why not simply
recipients = ['john.doe@example.com','john.smith@example.co.uk'] instead of making it a string, and then split it to make a list?– WoJ
Apr 16 at 17:48
add a comment |
I tried the below and it worked like a charm :)
rec_list = ['first@example.com', 'second@example.com']
rec = ', '.join(rec_list)
msg['To'] = rec
send_out = smtplib.SMTP('localhost')
send_out.sendmail(me, rec_list, msg.as_string())
FYR whole simple code below:import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText sender = 'myEmailAddress@example.com' rec_list = ['first@example.com', 'second@example.com'] rec = ', '.join(rec_list) msg = MIMEMultipart('alternative') msg['Subject'] = 'The required subject' msg['From'] = sender msg['To'] = rec html = ('whatever html code') htm_part = MIMEText(html, 'html') msg.attach(htm_part) send_out = smtplib.SMTP('localhost') send_out.sendmail(sender, rec_list, msg.as_string()) send_out.quit()
– TopSecret_007
Nov 24 '17 at 4:50
add a comment |
I tried the below and it worked like a charm :)
rec_list = ['first@example.com', 'second@example.com']
rec = ', '.join(rec_list)
msg['To'] = rec
send_out = smtplib.SMTP('localhost')
send_out.sendmail(me, rec_list, msg.as_string())
FYR whole simple code below:import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText sender = 'myEmailAddress@example.com' rec_list = ['first@example.com', 'second@example.com'] rec = ', '.join(rec_list) msg = MIMEMultipart('alternative') msg['Subject'] = 'The required subject' msg['From'] = sender msg['To'] = rec html = ('whatever html code') htm_part = MIMEText(html, 'html') msg.attach(htm_part) send_out = smtplib.SMTP('localhost') send_out.sendmail(sender, rec_list, msg.as_string()) send_out.quit()
– TopSecret_007
Nov 24 '17 at 4:50
add a comment |
I tried the below and it worked like a charm :)
rec_list = ['first@example.com', 'second@example.com']
rec = ', '.join(rec_list)
msg['To'] = rec
send_out = smtplib.SMTP('localhost')
send_out.sendmail(me, rec_list, msg.as_string())
I tried the below and it worked like a charm :)
rec_list = ['first@example.com', 'second@example.com']
rec = ', '.join(rec_list)
msg['To'] = rec
send_out = smtplib.SMTP('localhost')
send_out.sendmail(me, rec_list, msg.as_string())
edited Nov 24 '17 at 4:35
answered Nov 24 '17 at 3:57
TopSecret_007TopSecret_007
8113
8113
FYR whole simple code below:import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText sender = 'myEmailAddress@example.com' rec_list = ['first@example.com', 'second@example.com'] rec = ', '.join(rec_list) msg = MIMEMultipart('alternative') msg['Subject'] = 'The required subject' msg['From'] = sender msg['To'] = rec html = ('whatever html code') htm_part = MIMEText(html, 'html') msg.attach(htm_part) send_out = smtplib.SMTP('localhost') send_out.sendmail(sender, rec_list, msg.as_string()) send_out.quit()
– TopSecret_007
Nov 24 '17 at 4:50
add a comment |
FYR whole simple code below:import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText sender = 'myEmailAddress@example.com' rec_list = ['first@example.com', 'second@example.com'] rec = ', '.join(rec_list) msg = MIMEMultipart('alternative') msg['Subject'] = 'The required subject' msg['From'] = sender msg['To'] = rec html = ('whatever html code') htm_part = MIMEText(html, 'html') msg.attach(htm_part) send_out = smtplib.SMTP('localhost') send_out.sendmail(sender, rec_list, msg.as_string()) send_out.quit()
– TopSecret_007
Nov 24 '17 at 4:50
FYR whole simple code below:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText sender = 'myEmailAddress@example.com' rec_list = ['first@example.com', 'second@example.com'] rec = ', '.join(rec_list) msg = MIMEMultipart('alternative') msg['Subject'] = 'The required subject' msg['From'] = sender msg['To'] = rec html = ('whatever html code') htm_part = MIMEText(html, 'html') msg.attach(htm_part) send_out = smtplib.SMTP('localhost') send_out.sendmail(sender, rec_list, msg.as_string()) send_out.quit()– TopSecret_007
Nov 24 '17 at 4:50
FYR whole simple code below:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText sender = 'myEmailAddress@example.com' rec_list = ['first@example.com', 'second@example.com'] rec = ', '.join(rec_list) msg = MIMEMultipart('alternative') msg['Subject'] = 'The required subject' msg['From'] = sender msg['To'] = rec html = ('whatever html code') htm_part = MIMEText(html, 'html') msg.attach(htm_part) send_out = smtplib.SMTP('localhost') send_out.sendmail(sender, rec_list, msg.as_string()) send_out.quit()– TopSecret_007
Nov 24 '17 at 4:50
add a comment |
I came up with this importable module function. It uses the gmail email server in this example. Its split into header and message so you can clearly see whats going on:
import smtplib
def send_alert(subject=""):
to = ['email@one.com', 'email2@another_email.com', 'a3rd@email.com']
gmail_user = 'me@gmail.com'
gmail_pwd = 'my_pass'
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + ", ".join(to) + 'n' + 'From: ' + gmail_user + 'n' + 'Subject: ' + subject + 'n'
msg = header + 'n' + subject + 'nn'
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
add a comment |
I came up with this importable module function. It uses the gmail email server in this example. Its split into header and message so you can clearly see whats going on:
import smtplib
def send_alert(subject=""):
to = ['email@one.com', 'email2@another_email.com', 'a3rd@email.com']
gmail_user = 'me@gmail.com'
gmail_pwd = 'my_pass'
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + ", ".join(to) + 'n' + 'From: ' + gmail_user + 'n' + 'Subject: ' + subject + 'n'
msg = header + 'n' + subject + 'nn'
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
add a comment |
I came up with this importable module function. It uses the gmail email server in this example. Its split into header and message so you can clearly see whats going on:
import smtplib
def send_alert(subject=""):
to = ['email@one.com', 'email2@another_email.com', 'a3rd@email.com']
gmail_user = 'me@gmail.com'
gmail_pwd = 'my_pass'
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + ", ".join(to) + 'n' + 'From: ' + gmail_user + 'n' + 'Subject: ' + subject + 'n'
msg = header + 'n' + subject + 'nn'
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
I came up with this importable module function. It uses the gmail email server in this example. Its split into header and message so you can clearly see whats going on:
import smtplib
def send_alert(subject=""):
to = ['email@one.com', 'email2@another_email.com', 'a3rd@email.com']
gmail_user = 'me@gmail.com'
gmail_pwd = 'my_pass'
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + ", ".join(to) + 'n' + 'From: ' + gmail_user + 'n' + 'Subject: ' + subject + 'n'
msg = header + 'n' + subject + 'nn'
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
answered Apr 5 '14 at 20:04
radtekradtek
16.6k69078
16.6k69078
add a comment |
add a comment |
I figured this out a few months back and blogged about it. The summary is:
If you want to use smtplib to send email to multiple recipients, use email.Message.add_header('To', eachRecipientAsString) to add them, and then when you invoke the sendmail method, use email.Message.get_all('To') send the message to all of them. Ditto for Cc and Bcc recipients.
Python 3.7 throws an exception with message: Exception has occurred: ValueError There may be at most 1 To headers in a message
– Wojciech Jakubas
Oct 19 '18 at 13:47
add a comment |
I figured this out a few months back and blogged about it. The summary is:
If you want to use smtplib to send email to multiple recipients, use email.Message.add_header('To', eachRecipientAsString) to add them, and then when you invoke the sendmail method, use email.Message.get_all('To') send the message to all of them. Ditto for Cc and Bcc recipients.
Python 3.7 throws an exception with message: Exception has occurred: ValueError There may be at most 1 To headers in a message
– Wojciech Jakubas
Oct 19 '18 at 13:47
add a comment |
I figured this out a few months back and blogged about it. The summary is:
If you want to use smtplib to send email to multiple recipients, use email.Message.add_header('To', eachRecipientAsString) to add them, and then when you invoke the sendmail method, use email.Message.get_all('To') send the message to all of them. Ditto for Cc and Bcc recipients.
I figured this out a few months back and blogged about it. The summary is:
If you want to use smtplib to send email to multiple recipients, use email.Message.add_header('To', eachRecipientAsString) to add them, and then when you invoke the sendmail method, use email.Message.get_all('To') send the message to all of them. Ditto for Cc and Bcc recipients.
edited Dec 6 '16 at 17:57
miken32
24.9k95173
24.9k95173
answered Jul 1 '12 at 0:20
James McPhersonJames McPherson
5911
5911
Python 3.7 throws an exception with message: Exception has occurred: ValueError There may be at most 1 To headers in a message
– Wojciech Jakubas
Oct 19 '18 at 13:47
add a comment |
Python 3.7 throws an exception with message: Exception has occurred: ValueError There may be at most 1 To headers in a message
– Wojciech Jakubas
Oct 19 '18 at 13:47
Python 3.7 throws an exception with message: Exception has occurred: ValueError There may be at most 1 To headers in a message
– Wojciech Jakubas
Oct 19 '18 at 13:47
Python 3.7 throws an exception with message: Exception has occurred: ValueError There may be at most 1 To headers in a message
– Wojciech Jakubas
Oct 19 '18 at 13:47
add a comment |
So actually the problem is that SMTP.sendmail and email.MIMEText need two different things.
email.MIMEText sets up the "To:" header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)
SMTP.sendmail, on the other hand, sets up the "envelope" of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.
So, what you need to do is COMBINE the two replies you received. Set msg['To'] to a single string, but pass the raw list to sendmail:
emails = ['a.com','b.com', 'c.com']
msg['To'] = ', '.join( emails )
....
s.sendmail( msg['From'], emails, msg.as_string())
add a comment |
So actually the problem is that SMTP.sendmail and email.MIMEText need two different things.
email.MIMEText sets up the "To:" header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)
SMTP.sendmail, on the other hand, sets up the "envelope" of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.
So, what you need to do is COMBINE the two replies you received. Set msg['To'] to a single string, but pass the raw list to sendmail:
emails = ['a.com','b.com', 'c.com']
msg['To'] = ', '.join( emails )
....
s.sendmail( msg['From'], emails, msg.as_string())
add a comment |
So actually the problem is that SMTP.sendmail and email.MIMEText need two different things.
email.MIMEText sets up the "To:" header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)
SMTP.sendmail, on the other hand, sets up the "envelope" of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.
So, what you need to do is COMBINE the two replies you received. Set msg['To'] to a single string, but pass the raw list to sendmail:
emails = ['a.com','b.com', 'c.com']
msg['To'] = ', '.join( emails )
....
s.sendmail( msg['From'], emails, msg.as_string())
So actually the problem is that SMTP.sendmail and email.MIMEText need two different things.
email.MIMEText sets up the "To:" header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)
SMTP.sendmail, on the other hand, sets up the "envelope" of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.
So, what you need to do is COMBINE the two replies you received. Set msg['To'] to a single string, but pass the raw list to sendmail:
emails = ['a.com','b.com', 'c.com']
msg['To'] = ', '.join( emails )
....
s.sendmail( msg['From'], emails, msg.as_string())
edited May 14 '18 at 7:23
Syscall
14.3k51132
14.3k51132
answered May 14 '18 at 7:17
SaiSandeep GollaSaiSandeep Golla
7114
7114
add a comment |
add a comment |
Well, the method in this asnwer method did not work for me. I don't know, maybe this is a Python3 (I am using the 3.4 version) or gmail related issue, but after some tries, the solution that worked for me, was the line
s.send_message(msg)
instead of
s.sendmail(sender, recipients, msg.as_string())
add a comment |
Well, the method in this asnwer method did not work for me. I don't know, maybe this is a Python3 (I am using the 3.4 version) or gmail related issue, but after some tries, the solution that worked for me, was the line
s.send_message(msg)
instead of
s.sendmail(sender, recipients, msg.as_string())
add a comment |
Well, the method in this asnwer method did not work for me. I don't know, maybe this is a Python3 (I am using the 3.4 version) or gmail related issue, but after some tries, the solution that worked for me, was the line
s.send_message(msg)
instead of
s.sendmail(sender, recipients, msg.as_string())
Well, the method in this asnwer method did not work for me. I don't know, maybe this is a Python3 (I am using the 3.4 version) or gmail related issue, but after some tries, the solution that worked for me, was the line
s.send_message(msg)
instead of
s.sendmail(sender, recipients, msg.as_string())
edited May 23 '17 at 12:10
Community♦
11
11
answered Dec 25 '16 at 16:03
AntoineAntoine
374115
374115
add a comment |
add a comment |
I use python 3.6 and the following code works for me
email_send = 'xxxxx@xxx.xxx,xxxx@xxx.xxx'
server.sendmail(email_user,email_send.split(','),text)
add a comment |
I use python 3.6 and the following code works for me
email_send = 'xxxxx@xxx.xxx,xxxx@xxx.xxx'
server.sendmail(email_user,email_send.split(','),text)
add a comment |
I use python 3.6 and the following code works for me
email_send = 'xxxxx@xxx.xxx,xxxx@xxx.xxx'
server.sendmail(email_user,email_send.split(','),text)
I use python 3.6 and the following code works for me
email_send = 'xxxxx@xxx.xxx,xxxx@xxx.xxx'
server.sendmail(email_user,email_send.split(','),text)
edited Aug 4 '18 at 16:33
answered Jul 23 '18 at 4:24
RobieRobie
362
362
add a comment |
add a comment |
Below worked for me.
It sends email to multiple with attachment - "To", "Cc" & "Bcc" successfully.
toaddr = ['mailid_1','mailid_2']
cc = ['mailid_3','mailid_4']
bcc = ['mailid_5','mailid_6']
subject = 'Email from Python Code'
fromaddr = 'sender_mailid'
message = "n !! Hello... !!"
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
msg['Subject'] = subject
s.sendmail(fromaddr, (toaddr+cc+bcc) , message)
add a comment |
Below worked for me.
It sends email to multiple with attachment - "To", "Cc" & "Bcc" successfully.
toaddr = ['mailid_1','mailid_2']
cc = ['mailid_3','mailid_4']
bcc = ['mailid_5','mailid_6']
subject = 'Email from Python Code'
fromaddr = 'sender_mailid'
message = "n !! Hello... !!"
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
msg['Subject'] = subject
s.sendmail(fromaddr, (toaddr+cc+bcc) , message)
add a comment |
Below worked for me.
It sends email to multiple with attachment - "To", "Cc" & "Bcc" successfully.
toaddr = ['mailid_1','mailid_2']
cc = ['mailid_3','mailid_4']
bcc = ['mailid_5','mailid_6']
subject = 'Email from Python Code'
fromaddr = 'sender_mailid'
message = "n !! Hello... !!"
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
msg['Subject'] = subject
s.sendmail(fromaddr, (toaddr+cc+bcc) , message)
Below worked for me.
It sends email to multiple with attachment - "To", "Cc" & "Bcc" successfully.
toaddr = ['mailid_1','mailid_2']
cc = ['mailid_3','mailid_4']
bcc = ['mailid_5','mailid_6']
subject = 'Email from Python Code'
fromaddr = 'sender_mailid'
message = "n !! Hello... !!"
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
msg['Subject'] = subject
s.sendmail(fromaddr, (toaddr+cc+bcc) , message)
answered Feb 1 at 11:35
OmkarOmkar
237
237
add a comment |
add a comment |
you can try this when you write the recpient emails on a text file
from email.mime.text import MIMEText
from email.header import Header
import smtplib
f = open('emails.txt', 'r').readlines()
for n in f:
emails = n.rstrip()
server = smtplib.SMTP('smtp.uk.xensource.com')
server.ehlo()
server.starttls()
body = "Test Email"
subject = "Test"
from = "me@example.com"
to = emails
msg = MIMEText(body,'plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = Header(from, 'utf-8')
msg['To'] = Header(to, 'utf-8')
text = msg.as_string()
try:
server.send(from, emails, text)
print('Message Sent Succesfully')
except:
print('There Was An Error While Sending The Message')
add a comment |
you can try this when you write the recpient emails on a text file
from email.mime.text import MIMEText
from email.header import Header
import smtplib
f = open('emails.txt', 'r').readlines()
for n in f:
emails = n.rstrip()
server = smtplib.SMTP('smtp.uk.xensource.com')
server.ehlo()
server.starttls()
body = "Test Email"
subject = "Test"
from = "me@example.com"
to = emails
msg = MIMEText(body,'plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = Header(from, 'utf-8')
msg['To'] = Header(to, 'utf-8')
text = msg.as_string()
try:
server.send(from, emails, text)
print('Message Sent Succesfully')
except:
print('There Was An Error While Sending The Message')
add a comment |
you can try this when you write the recpient emails on a text file
from email.mime.text import MIMEText
from email.header import Header
import smtplib
f = open('emails.txt', 'r').readlines()
for n in f:
emails = n.rstrip()
server = smtplib.SMTP('smtp.uk.xensource.com')
server.ehlo()
server.starttls()
body = "Test Email"
subject = "Test"
from = "me@example.com"
to = emails
msg = MIMEText(body,'plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = Header(from, 'utf-8')
msg['To'] = Header(to, 'utf-8')
text = msg.as_string()
try:
server.send(from, emails, text)
print('Message Sent Succesfully')
except:
print('There Was An Error While Sending The Message')
you can try this when you write the recpient emails on a text file
from email.mime.text import MIMEText
from email.header import Header
import smtplib
f = open('emails.txt', 'r').readlines()
for n in f:
emails = n.rstrip()
server = smtplib.SMTP('smtp.uk.xensource.com')
server.ehlo()
server.starttls()
body = "Test Email"
subject = "Test"
from = "me@example.com"
to = emails
msg = MIMEText(body,'plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = Header(from, 'utf-8')
msg['To'] = Header(to, 'utf-8')
text = msg.as_string()
try:
server.send(from, emails, text)
print('Message Sent Succesfully')
except:
print('There Was An Error While Sending The Message')
answered May 7 '18 at 10:51
Skiller DzSkiller Dz
543314
543314
add a comment |
add a comment |
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sender(recipients):
body = 'Your email content here'
msg = MIMEMultipart()
msg['Subject'] = 'Email Subject'
msg['From'] = 'your.email@gmail.com'
msg['To'] = (', ').join(recipients.split(','))
msg.attach(MIMEText(body,'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('your.email@gmail.com', 'yourpassword')
server.send_message(msg)
server.quit()
if __name__ == '__main__':
sender('email_1@domain.com,email_2@domain.com')
It only worked for me with send_message function and using the join function in the list whith recipients, python 3.6.
add a comment |
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sender(recipients):
body = 'Your email content here'
msg = MIMEMultipart()
msg['Subject'] = 'Email Subject'
msg['From'] = 'your.email@gmail.com'
msg['To'] = (', ').join(recipients.split(','))
msg.attach(MIMEText(body,'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('your.email@gmail.com', 'yourpassword')
server.send_message(msg)
server.quit()
if __name__ == '__main__':
sender('email_1@domain.com,email_2@domain.com')
It only worked for me with send_message function and using the join function in the list whith recipients, python 3.6.
add a comment |
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sender(recipients):
body = 'Your email content here'
msg = MIMEMultipart()
msg['Subject'] = 'Email Subject'
msg['From'] = 'your.email@gmail.com'
msg['To'] = (', ').join(recipients.split(','))
msg.attach(MIMEText(body,'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('your.email@gmail.com', 'yourpassword')
server.send_message(msg)
server.quit()
if __name__ == '__main__':
sender('email_1@domain.com,email_2@domain.com')
It only worked for me with send_message function and using the join function in the list whith recipients, python 3.6.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sender(recipients):
body = 'Your email content here'
msg = MIMEMultipart()
msg['Subject'] = 'Email Subject'
msg['From'] = 'your.email@gmail.com'
msg['To'] = (', ').join(recipients.split(','))
msg.attach(MIMEText(body,'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('your.email@gmail.com', 'yourpassword')
server.send_message(msg)
server.quit()
if __name__ == '__main__':
sender('email_1@domain.com,email_2@domain.com')
It only worked for me with send_message function and using the join function in the list whith recipients, python 3.6.
edited Aug 20 '18 at 12:56
answered Aug 20 '18 at 12:50
Guilherme Henrique MendesGuilherme Henrique Mendes
608
608
add a comment |
add a comment |
protected by miken32 Jan 30 at 22:28
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
1
It appears OP answered his own question:
sendmailneeds a list.– Cees Timmerman
May 20 '15 at 8:35
possible duplicate of Is there any way to add multiple receivers in Python SMTPlib?
– Cees Timmerman
May 20 '15 at 8:42
Using Python3 I had to loop through recipients;
for addr in recipients: msg['To'] = addrand then it worked. Multiple assignments actually appends a new 'To' header for each one. This is a very bizarre interface, I can't even explain how I thought to try it. I was even considering usingsubprocessto call the unixsendmailpackage to save my sanity before I figured this out.– mehtunguh
Nov 7 '18 at 21:47