Page 1 of 1

SMTP email -- 2 recipients

Posted: Thu Aug 04, 2016 12:58 am
by mmkw43
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

Posted: Thu Aug 04, 2016 1:02 am
by mmkw43
ah --- they need to be separate strings or something ? just realized my variable is a single string with everything, both addresses and the comma

Re: SMTP email -- 2 recipients

Posted: Thu Aug 04, 2016 5:47 am
by elParaguayo

Re: SMTP email -- 2 recipients

Posted: Thu Aug 04, 2016 6:58 am
by buja
Semicolon: bob@gmail.com ; bill@gmail.com
(spaces not necessary, but added here for clarity)

Re: SMTP email -- 2 recipients

Posted: Thu Aug 04, 2016 7:33 am
by tom.slick
If you want to send to multiple recipients use a list not a string.
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

Posted: Thu Aug 04, 2016 12:11 pm
by mmkw43
Great -- thanks.

Re: SMTP email -- 2 recipients

Posted: Thu Aug 04, 2016 7:39 pm
by mmkw43
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.

??

Re: SMTP email -- 2 recipients

Posted: Thu Aug 04, 2016 7:42 pm
by SonOfAMotherlessGoat
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)

Re: SMTP email -- 2 recipients

Posted: Thu Aug 04, 2016 8:07 pm
by mmkw43
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

Re: SMTP email -- 2 recipients

Posted: Thu Aug 04, 2016 8:21 pm
by tom.slick
mmkw43 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
StringVar() is a tkinter class not a python string

Code: Select all

receiver = rcvr.get()
will give you a string

Re: SMTP email -- 2 recipients

Posted: Thu Aug 04, 2016 9:09 pm
by mmkw43
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.

Re: SMTP email -- 2 recipients

Posted: Thu Aug 04, 2016 9:18 pm
by mmkw43
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 ( ',')

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

Posted: Thu Aug 04, 2016 9:46 pm
by tom.slick
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']
>>>

Re: SMTP email -- 2 recipients

Posted: Fri Aug 05, 2016 12:12 am
by mmkw43
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