mmkw43
Posts: 654
Joined: Tue Dec 24, 2013 6:18 pm

SMTP email -- 2 recipients

Thu Aug 04, 2016 12:58 am

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

mmkw43
Posts: 654
Joined: Tue Dec 24, 2013 6:18 pm

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 1:02 am

ah --- they need to be separate strings or something ? just realized my variable is a single string with everything, both addresses and the comma

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 5:47 am

RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
buja
Posts: 601
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 6:58 am

Semicolon: bob@gmail.com ; bill@gmail.com
(spaces not necessary, but added here for clarity)

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 7:33 am

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)

mmkw43
Posts: 654
Joined: Tue Dec 24, 2013 6:18 pm

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 12:11 pm

Great -- thanks.

mmkw43
Posts: 654
Joined: Tue Dec 24, 2013 6:18 pm

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 7:39 pm

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.

??

SonOfAMotherlessGoat
Posts: 690
Joined: Tue Jun 16, 2015 6:01 am

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 7:42 pm

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

mmkw43
Posts: 654
Joined: Tue Dec 24, 2013 6:18 pm

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 8:07 pm

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

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 8:21 pm

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

mmkw43
Posts: 654
Joined: Tue Dec 24, 2013 6:18 pm

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 9:09 pm

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.

mmkw43
Posts: 654
Joined: Tue Dec 24, 2013 6:18 pm

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 9:18 pm

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()

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: SMTP email -- 2 recipients

Thu Aug 04, 2016 9:46 pm

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']
>>>

mmkw43
Posts: 654
Joined: Tue Dec 24, 2013 6:18 pm

Re: SMTP email -- 2 recipients

Fri Aug 05, 2016 12:12 am

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

Return to “Python”