简体   繁体   English

AttributeError: 'list' object 没有属性 'encode'。 我如何通过 Excel 发送 Email? Python

[英]AttributeError: 'list' object has no attribute 'encode'. How do i send Email via Excel? Python

So I'm trying to send an e-mail from Excel with Python. Right now I just have all the e-mails set up in column A1 separated by semicolons.所以我试图从 Excel 和 Python 发送一封电子邮件。现在我只在 A1 列中设置了所有电子邮件,用分号分隔。 If I put the e-mails into Python directly, they send out but when I use the Python call-out, it freaks out giving me this error.如果我直接将电子邮件放入 Python,它们会发送出去,但是当我使用 Python 调用时,它给我这个错误吓坏了。 How exactly can I make this work?我究竟怎样才能使这项工作?

import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pandas
from datetime import datetime

server = smtplib.SMTP('relay.xxx.com')
email_df = pandas.read_excel(f"E-mails.xlsx", sheet_name="Emails", nrows=1, usecols = "A")
sender = 'email@email.com'

receivers = 'Email'
receivers = list(email_df)

print(receivers)
msg = MIMEMultipart('related')

msg['Subject'] = 'Test mail'
msg['From'] = sender

with smtplib.SMTP('relay.xxx.com') as server:
    msg['To'] = receivers
    server.sendmail(sender, receivers, bytes(msg.as_string()))
    print(f"Successfully sent email to: {receivers}")

I don't want multiple e-mails to be sent out.我不想发送多封电子邮件。 I want 1 single e-mail with a bunch of e-mails in the address box.我想要一封电子邮件,地址栏中有一堆电子邮件。

Alternatively, if there's a way can read it by column, (addr1 in a1, addr in a2) that'd also be better.或者,如果有一种方法可以按列读取(a1 中的 addr1,a2 中的 addr),那也会更好。

Appreciate it.欣赏它。

If you can get your email addresses (from your df) into a list in the desired format, eg:如果您可以将您的 email 地址(来自您的 df)放入所需格式的列表中,例如:

print(receivers)    
['email1@email1.com', 'email2@email2.com', 'email3@email3.com']

then you can add this after its assignment to turn it into a string for send_mail (as it requires a string instead of a list):然后您可以在分配后添加它以将其转换为 send_mail 的字符串(因为它需要字符串而不是列表):

receivers = ", ".join(receivers)

You mention that it would be ideal if you had an email address per row under column A in your spreadsheet.您提到,如果您的电子表格中 A 列下每行有一个 email 地址,那将是理想的。 So if you can get your Excel spreadsheet into this format (with a column heading of "Email") then you can do this to get the aforementioned list:因此,如果您可以将您的 Excel 电子表格转换为这种格式(列标题为“电子邮件”),那么您可以这样做以获得上述列表:

email_df = pandas.read_excel(f"E-mails.xlsx", sheet_name="Emails", usecols = "A")
receivers = email_df['Email'].tolist()

So, for completeness (with Excel spreadsheet in its new format of one email address per row under column A), your code would change to:因此,为了完整性(使用新格式的 Excel 电子表格,A 列下每行一个 email 地址),您的代码将更改为:

import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pandas
from datetime import datetime

server = smtplib.SMTP('relay.xxx.com')

email_df = pandas.read_excel(f"E-mails.xlsx", sheet_name="Emails")
receivers = email_df['Email'].tolist()

print(receivers)  # Check list is one valid email address per element

receivers = ", ".join(receivers)

sender = 'email@email.com'
msg = MIMEMultipart('related')

msg['Subject'] = 'Test mail'
msg['From'] = sender

with smtplib.SMTP('relay.xxx.com') as server:
    msg['To'] = receivers
    server.sendmail(sender, receivers, bytes(msg.as_string(), encoding='utf-8'))
    print(f"Successfully sent email to: {receivers}")

If you build a message, you'd better use the send_message interface.如果构建消息,最好使用send_message接口。 And I would suggest to use the newer EmailMessage interface which is cleaner and simpler that the older MimeMultipart one:我建议使用较新的EmailMessage界面,它比旧的MimeMultipart界面更干净、更简单:

import smtplib
from email.message import EmailMessage

...
msg = EmailMessage()

msg['Subject'] = 'Test mail'
msg['From'] = sender

msg.set_payload('Message text.\nLine 2...')
# or eventually add parts with msg.add_related(...), msg.add_alternative(..)
#  or msg.add_attachment(...)


with smtplib.SMTP('relay.xxx.com') as server:
    msg['To'] = receivers
    server.send_messages(msg)
    print(f"Successfully sent email to: {receivers}")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'list' object 在 Python 上没有属性 'encode' - AttributeError: 'list' object has no attribute 'encode' on Python 如何修复 AttributeError: 'bytes' object has no attribute 'encode'? - How do I fix AttributeError: 'bytes' object has no attribute 'encode'? AttributeError: 'list' object 在发送 email 时没有属性 'encode' - AttributeError: 'list' object has no attribute 'encode' when sending an email AttributeError:通过 Python 发送 Excel 附件时,“列表”没有属性“编码” - AttributeError: 'list' has no attribute 'encode' when sending Excel attachment via Python 通过Python发送电子邮件的程序以“AttributeError:'str'对象退出,没有属性'get_content_maintype''” - Program which send email via Python exits with “AttributeError: 'str' object has no attribute 'get_content_maintype''” AttributeError:'list'对象没有属性'encode' - AttributeError: 'list' object has no attribute 'encode' 如何修复 AttributeError: 'list' 对象没有属性 'encode' - How to fix AttributeError: 'list' object has no attribute 'encode' Python,AttributeError:'float'对象没有属性'encode' - Python, AttributeError: 'float' object has no attribute 'encode' AttributeError: 'tuple' 对象没有属性 'encode' python - AttributeError: 'tuple' object has no attribute 'encode' python 如何解决AttributeError:'NoneType'对象在python中没有属性'encode' - How to resolve AttributeError: 'NoneType' object has no attribute 'encode' in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM