Python sendemail() MIMEMultipart 不工作
Python sendemail() MIMEMultipart not working
我一直在尝试逐步创建一个 python (v3.4) 脚本来发送带附件的电子邮件。我已经使用 smtplib 库成功发送了一个没有附件的邮件。但是,要添加附件,我似乎需要创建一个 MIMEMultipart 对象。到这里为止没什么奇怪的。
基于我创建的例子
import smtplib
from email.mime.multipart import MIMEMultipart
try:
fromaddr = 'from@email.com'
toaddrs = ['to@email.com']
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = fromaddr
msg['To'] = toaddrs
msg.preamble = 'Our family reunion'
password = 'ExamplePassword88'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.ehlo()
server.login(fromaddr,password)
text = msg.as_string()
server.sendmail(fromaddr, toaddrs, text)
print("Successfully sent email")
除了 SMTPException:
打印("Error: unable to send email")
最后:
server.quit()
但是行 " text = msg.as_string()"" 抛出以下错误
Traceback (most recent call last):
File "C:/Users/user/upload_file.py", line 37, in
text = msg.as_string()
File "C:\Python34\lib\email\message.py", line 159, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\Python34\lib\email\generator.py", line 112, in flatten
self._write(msg)
File "C:\Python34\lib\email\generator.py", line 192, in _write
self._write_headers(msg)
File "C:\Python34\lib\email\generator.py", line 219, in _write_headers
self.write(self.policy.fold(h, v))
File "C:\Python34\lib\email_policybase.py", line 314, in fold
return self._fold(name, value, sanitize=True)
File "C:\Python34\lib\email_policybase.py", line 352, in _fold
parts.append(h.encode(linesep=self.linesep,
AttributeError: 'list' object has no attribute 'encode'
我能做什么?
问题是您的变量 toaddrs
是一个列表。
您需要将地址列表作为字符串,地址以逗号分隔,例如
msg['To'] = ",".join(toaddrs)
在那之后一切都应该正常工作。
有关更多详细信息,请参见此处:
How to send email to multiple recipients using python smtplib?
我一直在尝试逐步创建一个 python (v3.4) 脚本来发送带附件的电子邮件。我已经使用 smtplib 库成功发送了一个没有附件的邮件。但是,要添加附件,我似乎需要创建一个 MIMEMultipart 对象。到这里为止没什么奇怪的。
基于我创建的例子
import smtplib
from email.mime.multipart import MIMEMultipart
try:
fromaddr = 'from@email.com'
toaddrs = ['to@email.com']
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = fromaddr
msg['To'] = toaddrs
msg.preamble = 'Our family reunion'
password = 'ExamplePassword88'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.ehlo()
server.login(fromaddr,password)
text = msg.as_string()
server.sendmail(fromaddr, toaddrs, text)
print("Successfully sent email")
除了 SMTPException: 打印("Error: unable to send email") 最后: server.quit()
但是行 " text = msg.as_string()"" 抛出以下错误
Traceback (most recent call last): File "C:/Users/user/upload_file.py", line 37, in text = msg.as_string() File "C:\Python34\lib\email\message.py", line 159, in as_string g.flatten(self, unixfrom=unixfrom) File "C:\Python34\lib\email\generator.py", line 112, in flatten self._write(msg) File "C:\Python34\lib\email\generator.py", line 192, in _write self._write_headers(msg) File "C:\Python34\lib\email\generator.py", line 219, in _write_headers self.write(self.policy.fold(h, v)) File "C:\Python34\lib\email_policybase.py", line 314, in fold return self._fold(name, value, sanitize=True) File "C:\Python34\lib\email_policybase.py", line 352, in _fold parts.append(h.encode(linesep=self.linesep, AttributeError: 'list' object has no attribute 'encode'
我能做什么?
问题是您的变量 toaddrs
是一个列表。
您需要将地址列表作为字符串,地址以逗号分隔,例如
msg['To'] = ",".join(toaddrs)
在那之后一切都应该正常工作。 有关更多详细信息,请参见此处: How to send email to multiple recipients using python smtplib?