Warm tip: This article is reproduced from serverfault.com, please click

Error when executing rows from excel list when trying to send emails

发布于 2020-11-27 13:05:50

I'm building a mail sender which I want to take one email at a time and not add all the receipents into BCC.

So, with the code below I'm trying to make it add one email at a time from the .xlsx list, add it to "TO" and send the email, and then repeat on the next row in the .xlsx-file.

I have a code that looks like this:

import pandas as pd

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

email_list = pd.read_excel('/home/xxxx/test.xlsx')

emails = email_list['EMAIL']
email_list['EMAIL'] = email_list.index

my_list = email_list['EMAIL'].tolist()
print(my_list)


msg = MIMEMultipart()
msg['From'] = "test@xxxx.com"
msg['To'] = ",".join(str(my_list))
msg['Subject'] = "This is the subject"

message = """
<html><p>randomtext</p></html>
"""
message2 = "plain text msg"
msg.attach(MIMEText(message,'html'))
msg.attach(MIMEText(message2,'plain'))
Password = "thisispassword"
server = smtplib.SMTP('smtp.mail.com:587')
#server.ehlo()
server.starttls()
server.login(msg['From'],Password)
server.sendmail(msg['From'],msg['To'],msg.as_string())

server.quit()

Instead I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/seniori/anaconda3/lib/python3.8/smtplib.py", line 885, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'[,0,,, ,1,,, ,2,]': (501, b'<[,0,,, ,1,,,=,2,]>: missing or malformed local part')} 

It seems like it is finding the rows in the excel-file but not the email adresses.

Any idea of what I need to change here?

EDIT: Here is the total error log

Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> 
>>> from email.mime.multipart import MIMEMultipart
>>> from email.mime.text import MIMEText
>>> import smtplib
>>> 
>>> email_list = pd.read_excel('/home/xxxxx/test.xlsx')
>>> 
>>> emails = email_list['EMAIL']
>>> email_list['EMAIL'] = email_list.index
>>> 
>>> my_list = email_list['EMAIL'].tolist()
>>> print(my_list)
[0, 1, 2]
>>> 
>>> 
>>> msg = MIMEMultipart()
>>> msg['From'] = "mail@example.org"
>>> msg['To'] = ",".join(str(my_list))
>>> msg['Subject'] = "subject"
>>> 
>>> message = """
... <html><p>randomtext</p></html>
... """
>>> message2 = "plain text msg"
>>> msg.attach(MIMEText(message,'html'))
>>> msg.attach(MIMEText(message2,'plain'))
>>> Password = "Wenglish2.1"
>>> server = smtplib.SMTP('mail@example.org')
[proxychains] Dynamic chain  ...  127.0.0.1:9050  ...  127.0.0.1:9050 <--denied
[proxychains] Dynamic chain  ...  127.0.0.1:9050  ...  mail@example.org:587  ...  OK
>>> #server.ehlo()
>>> server.starttls()
(220, b'TLS go ahead')
>>> server.login(msg['From'],Password)
(235, b'Authentication succeeded')
>>> server.sendmail(msg['From'],msg['To'],msg.as_string())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/seniori/anaconda3/lib/python3.8/smtplib.py", line 885, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'[,0,,, ,1,,, ,2,]': (501, b'<[,0,,, ,1,,,=,2,]>: missing or malformed local part')}
>>> 
>>> server.quit()
(221, b'xxxx.xxxx.com closing connection')
>>> 

And the email list is in a UTF-8 excel .xlsx-file that looks like this:

EMAIL
test@gmail.com
test1@gmail.com
test2@gmail.com
Questioner
garymaker
Viewed
0
garymaker 2020-11-28 17:15:53

I've sorted it out by doing the following:

for i in range(0,len(emails)):
    msg['To'] = my_list[i]
    server.sendmail(msg['From'],msg['To'],msg.as_string())
    del msg['To']

This is making a loop and sending one email at a time from the excel-sheet.