尝试发送电子邮件时执行 excel 列表中的行时出错
Error when executing rows from excel list when trying to send emails
我正在构建一个邮件发件人,我想一次接收一封电子邮件,而不是将所有收件人都添加到密件抄送中。
因此,使用下面的代码,我试图让它从 .xlsx 列表中一次添加一封电子邮件,将其添加到“收件人”并发送电子邮件,然后在下一行重复.xlsx 文件。
我的代码如下所示:
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()
相反,我收到以下错误:
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')}
它似乎在 excel 文件中找到行,但找不到电子邮件地址。
知道我需要在此处更改什么吗?
编辑:这是全部错误日志
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')
>>>
电子邮件列表是 UTF-8 excel.xlsx 文件,如下所示:
EMAIL
test@gmail.com
test1@gmail.com
test2@gmail.com
我已经通过执行以下操作解决了这个问题:
for i in range(0,len(emails)):
msg['To'] = my_list[i]
server.sendmail(msg['From'],msg['To'],msg.as_string())
del msg['To']
这是一个循环,一次从 excel-sheet 发送一封电子邮件。
STMP.sendmail 期望 to_addrs
参数是一个 list:
... list of RFC 822 to-address strings (a bare string will be treated as a list with 1 address)
邮件body中的TO
header可以(必须)是comma-separated字符串,但sendmail的参数必须是列表
所以你的代码应该是
server.sendmail(msg['From'], mylist, msg.as_string())
我正在构建一个邮件发件人,我想一次接收一封电子邮件,而不是将所有收件人都添加到密件抄送中。
因此,使用下面的代码,我试图让它从 .xlsx 列表中一次添加一封电子邮件,将其添加到“收件人”并发送电子邮件,然后在下一行重复.xlsx 文件。
我的代码如下所示:
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()
相反,我收到以下错误:
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')}
它似乎在 excel 文件中找到行,但找不到电子邮件地址。
知道我需要在此处更改什么吗?
编辑:这是全部错误日志
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')
>>>
电子邮件列表是 UTF-8 excel.xlsx 文件,如下所示:
EMAIL
test@gmail.com
test1@gmail.com
test2@gmail.com
我已经通过执行以下操作解决了这个问题:
for i in range(0,len(emails)):
msg['To'] = my_list[i]
server.sendmail(msg['From'],msg['To'],msg.as_string())
del msg['To']
这是一个循环,一次从 excel-sheet 发送一封电子邮件。
STMP.sendmail 期望 to_addrs
参数是一个 list:
... list of RFC 822 to-address strings (a bare string will be treated as a list with 1 address)
邮件body中的TO
header可以(必须)是comma-separated字符串,但sendmail的参数必须是列表
所以你的代码应该是
server.sendmail(msg['From'], mylist, msg.as_string())