What is the proper way to send email to more than one recipient with SMTP email ? I simply used a comma and space but it didn't send to the 2nd address, just the first.
TO: bob@gmail.com, bill@gmail.com
no email sent to bill.
of course, I just guessed with the comma thing. - thanks
Re: SMTP email -- 2 recipients
ah --- they need to be separate strings or something ? just realized my variable is a single string with everything, both addresses and the comma
- elParaguayo
- Posts: 1943
- Joined: Wed May 16, 2012 12:46 pm
- Location: London, UK
Re: SMTP email -- 2 recipients
Have you seen this: http://stackoverflow.com/a/12422921/3087339
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.
Re: SMTP email -- 2 recipients
If you want to send to multiple recipients use a list not a string.
string = one recipient
list = multiple recipients
string = one recipient
list = multiple recipients
Code: Select all
to = ["ted@bear.com", "bill_and_ted@excellent.com", "Keanu@badacting.com"]
msg = "TESTING"
smtpserver.sendmail("user@email.com", to, msg)
Re: SMTP email -- 2 recipients
Great -- thanks.
Re: SMTP email -- 2 recipients
Trying to figure out how to do it with the format I'm using which is --
mail.sendmail (sender, receiver, header + content)
which works fine with one recipient but those are all string variables -- can't substitute a list for receiver.
??
mail.sendmail (sender, receiver, header + content)
which works fine with one recipient but those are all string variables -- can't substitute a list for receiver.
??
-
- Posts: 690
- Joined: Tue Jun 16, 2015 6:01 am
Re: SMTP email -- 2 recipients
Perhaps a loop on the list? (Haven't tested this, so check the syntax, just typing this in.)
Code: Select all
to_list = ["ted@bear.com", "bill_and_ted@excellent.com", "Keanu@badacting.com"]
msg = "TESTING"
for addr in to_list:
smtpserver.sendmail("user@email.com", addr, msg)
Account Inactive
Re: SMTP email -- 2 recipients
Actually just did this and it works great -- extremely simple.
mail.sendmail (sender, receiver.split(','), header + content)
and I don't have to do a list at all -- it acts on my string variable
now maybe some can explain why it works because I never had quotes anywhere.
rcvr = StringVar()
and rcvr was used in a tkinter entry box -- so -- bob@gmail.com, bill@gmail.com became rcvr (without quotes!)
eventually receiver picks up the value of rcvr but again without quotes
fact is, I grabbed ( ',' ) from an example and not sure where things are splitting ?
anyway it works but I have no idea WHY
mail.sendmail (sender, receiver.split(','), header + content)
and I don't have to do a list at all -- it acts on my string variable
now maybe some can explain why it works because I never had quotes anywhere.
rcvr = StringVar()
and rcvr was used in a tkinter entry box -- so -- bob@gmail.com, bill@gmail.com became rcvr (without quotes!)
eventually receiver picks up the value of rcvr but again without quotes
fact is, I grabbed ( ',' ) from an example and not sure where things are splitting ?
anyway it works but I have no idea WHY
Re: SMTP email -- 2 recipients
StringVar() is a tkinter class not a python stringmmkw43 wrote:Actually just did this and it works great -- extremely simple.
mail.sendmail (sender, receiver.split(','), header + content)
and I don't have to a list at all -- it acts on my string variable which is
receiver = ' bob@gmail.com, bill@gmail.com '
now maybe some can explain why it works because I never had quotes anywhere.
rcvr = StringVar()
and rcvr was used in a tkinter entry box -- so -- bob@gmail.com, bill@gmail.com became rcvr (without quotes!)
eventually receiver picks up the value of rcvr but again without quotes
fact is, I grabbed ( ',' ) from an example and not sure where things are splitting anyway it works but I have no idea WHY
Code: Select all
receiver = rcvr.get()
Re: SMTP email -- 2 recipients
yes I know all that -- just didn't want to get wordy explaining everything. yes, that's what I'm doing.
but how split is working is a mystery.
but how split is working is a mystery.
Re: SMTP email -- 2 recipients
here's the code -- all the variables were brought in from tkinter entry boxes anyway it works fine -- no list needed
and su.rcvr is a string variable that I input without quotes -- bob@gmail.com, bill@gmail.com and both get mail even 3 or more works also
my question is -- what is split doing ? not familiar with ( ',')
and su.rcvr is a string variable that I input without quotes -- bob@gmail.com, bill@gmail.com and both get mail even 3 or more works also
my question is -- what is split doing ? not familiar with ( ',')
Code: Select all
content = str(15.message).strip('[]')
subject = 'mysubject'
sender = su.sndr
senderpass = su.sndrpass
receiver = su.rcvr
header = 'To:'+ receiver + '\n' + 'From:' + sender + '\n' + 'Subject:' + subject
mail = smtplib.SMTP(su.serv,su.pourt)
mail.ehlo()
mail.starttls()
mail.login(sender,senderpass)
mail.sendmail(sender,receiver.split(','),header + '\n' + content)
mail.close()
Re: SMTP email -- 2 recipients
the split method splits a string into a list. it takes an optional parameter on what to split the string on
>>> a = "a-b-c-d-e-f-g"
>>> a.split() # split on whitespace if not specified
['a-b-c-d-e-f-g']
>>> a.split("-")
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> a.split("d")
['a-b-c-', '-e-f-g']
>>>
it also can take a second optional parameter for the max number of times to split
a.split("-", 3)
['a', 'b', 'c', 'd-e-f-g']
>>>
>>> a.split("-", 5)
['a', 'b', 'c', 'd', 'e', 'f-g']
>>>
>>> a = "a-b-c-d-e-f-g"
>>> a.split() # split on whitespace if not specified
['a-b-c-d-e-f-g']
>>> a.split("-")
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> a.split("d")
['a-b-c-', '-e-f-g']
>>>
it also can take a second optional parameter for the max number of times to split
a.split("-", 3)
['a', 'b', 'c', 'd-e-f-g']
>>>
>>> a.split("-", 5)
['a', 'b', 'c', 'd', 'e', 'f-g']
>>>
Re: SMTP email -- 2 recipients
Knew about what split does but haven't used it much. I didn't know it actually makes it a list -- thought that maybe it just makes separate strings.
So heck easy way to make the list for multiple recipients then -- beautiful. --thanks
So heck easy way to make the list for multiple recipients then -- beautiful. --thanks