如何使用 Python 将 pdf 文件发送到多个电子邮件?
How to send a pdf file to multiple emails using Python?
我对这个程序的目标是向多个电子邮件地址发送一条消息以及附加到电子邮件的 PDF 文件。我该怎么做?我有两个 Traceback 调用(显示在代码之后)。到目前为止,代码在没有发送附件时有效,但当我尝试发送附件时,一切似乎都崩溃了。谢谢
编辑: 我相信我必须在某处使用这两行代码。
message.attach(部分)
文本 = message.as_string()
虽然当我写下它们时它说未解析的属性引用 'attach' for class 'str'
from email.mime.base import MIMEBase
import pandas as pd
import smtplib, ssl
subject = "An email with attachment from Python"
sender_email = input("Enter your E-mail Address: ")
password = input("Enter your password: ")
email_list = pd.read_excel("D:\Learning Python\Mini_Projects\emailaddresses.xlsx", engine="openpyxl")
# Read Excel Spreadsheet
names = email_list['Name']
receiver_email = email_list['Email']
message = """Subject: Training Program!
Hi {names}, I hope you received this e-mail with the attachment"""
filename = "TrainingProgram.pdf"
with open(filename, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
context = ssl.create_default_context()
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls(context=context)
server.login(sender_email, password)
for i in range(len(receiver_email)):
name = names[i]
email = receiver_email[i]
server.sendmail(sender_email, receiver_email, message)
print("E-mails Sent!")
File "D:/Learning Python/Mini_Projects/main.py", line 117, in <module>
server.sendmail(sender_email, [email], message)
File "C:\Users\janss\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 859, in sendmail
msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f4aa' in position 27: ordinal not in range(128)
ASCII 编码不支持表情符号。删除电子邮件正文中的 </code>,它应该可以工作。如果您绝对想保留表情符号,则必须使用 <code>MIMEText
对象,请参阅 this question 示例。
编辑:
为什么不在 for 循环中使用 name
和 email
变量?
当前的代码发送一封电子邮件到一个列表的地址,我想知道为什么它没有在那里出错...
for i in range(len(receiver_email)):
name = names[i]
email = receiver_email[i]
server.sendmail(sender_email, receiver_email, message)
也许您的意思是像下面这样的 for 循环?
for email in receiver_email:
server.sendmail(sender_email, email, message)
此外,您的消息将按照您编码的方式发送,而不是填写名称。为此,您需要在字符串前面加上一个 f。我认为 message
应该是这样的:
message = f"""Subject: Training Program!
Hi {", ".join(names)}, I hope you received this e-mail with the attachment"""
编辑 2:
Unresolved attribute reference 'attach' for class 'str'
这是因为str
没有附加方法,只有email.mime.multipart.MIMEMultipart
有。请参阅 this answer 了解如何正确发送附件。
我对这个程序的目标是向多个电子邮件地址发送一条消息以及附加到电子邮件的 PDF 文件。我该怎么做?我有两个 Traceback 调用(显示在代码之后)。到目前为止,代码在没有发送附件时有效,但当我尝试发送附件时,一切似乎都崩溃了。谢谢
编辑: 我相信我必须在某处使用这两行代码。
message.attach(部分)
文本 = message.as_string()
虽然当我写下它们时它说未解析的属性引用 'attach' for class 'str'
from email.mime.base import MIMEBase
import pandas as pd
import smtplib, ssl
subject = "An email with attachment from Python"
sender_email = input("Enter your E-mail Address: ")
password = input("Enter your password: ")
email_list = pd.read_excel("D:\Learning Python\Mini_Projects\emailaddresses.xlsx", engine="openpyxl")
# Read Excel Spreadsheet
names = email_list['Name']
receiver_email = email_list['Email']
message = """Subject: Training Program!
Hi {names}, I hope you received this e-mail with the attachment"""
filename = "TrainingProgram.pdf"
with open(filename, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
context = ssl.create_default_context()
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls(context=context)
server.login(sender_email, password)
for i in range(len(receiver_email)):
name = names[i]
email = receiver_email[i]
server.sendmail(sender_email, receiver_email, message)
print("E-mails Sent!")
File "D:/Learning Python/Mini_Projects/main.py", line 117, in <module>
server.sendmail(sender_email, [email], message)
File "C:\Users\janss\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 859, in sendmail
msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f4aa' in position 27: ordinal not in range(128)
ASCII 编码不支持表情符号。删除电子邮件正文中的 </code>,它应该可以工作。如果您绝对想保留表情符号,则必须使用 <code>MIMEText
对象,请参阅 this question 示例。
编辑:
为什么不在 for 循环中使用 name
和 email
变量?
当前的代码发送一封电子邮件到一个列表的地址,我想知道为什么它没有在那里出错...
for i in range(len(receiver_email)):
name = names[i]
email = receiver_email[i]
server.sendmail(sender_email, receiver_email, message)
也许您的意思是像下面这样的 for 循环?
for email in receiver_email:
server.sendmail(sender_email, email, message)
此外,您的消息将按照您编码的方式发送,而不是填写名称。为此,您需要在字符串前面加上一个 f。我认为 message
应该是这样的:
message = f"""Subject: Training Program!
Hi {", ".join(names)}, I hope you received this e-mail with the attachment"""
编辑 2:
Unresolved attribute reference 'attach' for class 'str'
这是因为str
没有附加方法,只有email.mime.multipart.MIMEMultipart
有。请参阅 this answer 了解如何正确发送附件。