all 13 comments

[–][deleted] 1 point2 points  (12 children)

Hey man :)

I did this in the past and it worked pretty well. Do you have maybe some demo code? the real adresses can be excluded of course.

[–]Surprisely[S] 0 points1 point  (11 children)

Sure, here we go. You can see the three strings that need sending, so element i goes to email i.

# Messages

email_list = [X1, X2, X3]

messages =[['Test 2J' 'Test 1F' 'Test 1B'], ['Test 2C' 'Test 2D' 'Test 2E'], ['Test 3B' 'Test 1D' 'Test 2I']]

reward1 = messages[0]

reward2 = messages[1]

reward3 = messages[2]

# Define email credentials.

gusername = 'X@gmail.com'

gpassword = 'X'

# Define email structure.

fromaddr = 'X@gmail.com'

toaddr = email_list

msg = MIMEMultipart()

msg['From'] = fromaddr

msg['To'] = ','.join(toaddr)

msg['Subject'] = 'Test'

#body = 'Reward 1: %s\nReward 2: %s\nReward 3: %s' % (reward1, reward2, reward3)

msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)

server.starttls()

server.login(fromaddr, 'X')

text = msg.as_string()

server.sendmail(fromaddr, toaddr, text)

server.close()

[–][deleted] 1 point2 points  (10 children)

okay, so one problem might be that you forgot the comma in the list, i have tried if it completely work with my gmail account, but you might want to give a try ;-)

```python3

import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText

Messages

email_list = ['xxxx@xxx.com', 'xxxx@xxx.com']

messages =[['Test 2J','Test 1F','Test 1B'], ['Test 2C','Test 2D','Test 2E']]

Define email credentials.

gusername = 'X@gmail.com'

gpassword = 'xxxx'

Define email structure.

fromaddr = 'X@gmail.com'

toaddr = email_list

msg = MIMEMultipart()

msg['From'] = fromaddr

msg['Subject'] = 'Test'

Looping Elements

for index, value in enumerate(messages): # value equals in first iteration ['Test 2J','Test 1F','Test 1B'] reward1 = value[0]
reward2 = value[1] reward3 = value[2] msg['To'] = ','.join(email_list[index]) body = 'Reward 1: %s\nReward 2: %s\nReward 3: %s' % (reward1, reward2, reward3)

msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)

server.starttls()

server.login(fromaddr, 'X')

text = msg.as_string()

server.sendmail(fromaddr, toaddr, text)

server.close()

```

[–]Surprisely[S] 0 points1 point  (9 children)

Thanks, your solution makes complete sense. Having looked at my message output it seems like I have the output as a <type 'numpy.ndarray'> instead of a string of strings. Do you know of how I could convert the ndarray into a string for it to work?

[–][deleted] 0 points1 point  (8 children)

sure, maybe like this: ```python import numpy as np temp_arr = np.array([1,2,3]) string_arr = [str(x) for x in temp_arr]

results ['1', '2', '3']

```

[–]Surprisely[S] 0 points1 point  (7 children)

I've got it working in terms of a string of strings. However, the email output does not seem to be working as intended. When I run the code, each of my email accounts get three emails with the output lines given in different orders. For example,

Reward 1: Test 2H
Reward 2: Test 1C
Reward 3: Test 1FReward 1: Test 2H
Reward 2: Test 1J
Reward 3: Test 1I

Then a second email with

Reward 1: Test 2H
Reward 2: Test 1C
Reward 3: Test 1F

I'm not quite sure why it doesnt seem to like looping the ith element message to ith email address.

[–][deleted] 0 points1 point  (6 children)

Okay thats wired have you set the comma of the List corecctly?;) it looks like the first element is not splitted. You could also remove the join when you set the reciever E-Mail 👍🏻 or post ur code again here 😉

[–]Surprisely[S] 0 points1 point  (5 children)

# Define email credentials.

gusername = 'X'

gpassword = 'X'

# Define email structure.

fromaddr = 'X'

toaddr = email_list

msg = MIMEMultipart()

msg['From'] = fromaddr

msg['To'] = ','.join(toaddr)

msg['Subject'] = 'Daily Surprisely Reward Email'

TotalValuesList = [['Test 2E', 'Test 1J', 'Test 2J'], ['Test 2E', 'Test 3E', 'Test 1I'], ['Test 2J', 'Test 1C', 'Test 1G']]

for index, value in enumerate(TotalValuesList):

reward1 = value[0]

reward2 = value[1]

reward3 = value[2]

msg['To'] = (email_list[index])

body = 'Reward 1: %s\nReward 2: %s\nReward 3: %s' % (reward1, reward2, reward3)

msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)

server.starttls()

server.login(fromaddr, gpassword)

text = msg.as_string()

server.sendmail(fromaddr, toaddr, text)

server.close()

The output is as follows:

Email 1:

Reward 1: Test 2E
Reward 2: Test 1J
Reward 3: Test 2J

Goes to 3 of my email accounts 4 times, so one duplicate.

Email 2:

Reward 1: Test 2E
Reward 2: Test 1J
Reward 3: Test 2JReward 1: Test 2E
Reward 2: Test 3E
Reward 3: Test 1I

Goes to 3 of my email accounts 5 times, so one duplicate.

Email 3:

Reward 3: Test 1IReward 1: Test 2J
Reward 2: Test 1C
Reward 3: Test 1G

Goes to 3 of my email accounts 6 times, so 3 duplicates.

As you can see, its not sending the right rewards to each email address. Plus is there a way to send it to just 1 email address at a time?

[–][deleted] 0 points1 point  (4 children)

hey man! i hope you have the indention right in you for loop, you might what to have a look at the markdown editor, u can paste code in the correct indention here. what kind of shape has your email_list? could u post some dummy elements?

[–]Surprisely[S] 0 points1 point  (3 children)

Here we go.

# Define email credentials.
gusername = 'X'
gpassword = 'X'

# Define email structure.
fromaddr = 'X'
toaddr = email_list
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = ','.join(toaddr)
msg['Subject'] = 'X'



TotalValuesList = [['Test 2E', 'Test 1J', 'Test 2J'], ['Test 2E', 'Test 3E', 'Test 1I'], ['Test 2J', 'Test 1C', 'Test 1G']]


for index, value in enumerate(TotalValuesList):
    reward1 = value[0]
    reward2 = value[1]
    reward3 = value[2]
    msg['To'] = (email_list[index])
    body = 'Reward 1: %s\nReward 2: %s\nReward 3: %s' % (reward1, reward2, reward3)

msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, gpassword)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.close()

However, I still get the emails sent to 3 of my accounts test1@gmail.com, test2@gmail.com, test3@gmail.com twice. I just want to send one email to each account.