Python smtplib send_message() 失败,返回 AttributeError: 'str' 对象没有属性 'get_all' [英] Python smtplib send_message() failing, returning AttributeError: 'str' object has no attribute 'get_all'
问题描述
我正在开发一个项目,我必须使用 Python 3.4 中的 smtplib 和 email 模块来发送电子邮件.
I'm working on an project where I have to use the smtplib and email modules in Python 3.4 to send an email.
我能够自己创建电子邮件并且能够连接到服务器,但是它返回此异常:
I'm able to create the email itself and I'm able to connect to the server, but then it returns this Exception:
reply: b'235 2.7.0 Accepted
'
reply: retcode (235); Msg: b'2.7.0 Accepted'
send: 'QUIT
'
reply: b'221 2.0.0 closing connection s66sm8304113yhp.2 - gsmtp
'
reply: retcode (221); Msg: b'2.0.0 closing connection s66sm8304113yhp.2 - gsmtp'
Traceback (most recent call last):
File "base.py", line 108, in <module>
send(fromaddr, toaddrs, msg)
File "base.py", line 61, in send
server.send_message(fromaddr, toaddrs, msg)
File "/usr/lib/python3.4/smtplib.py", line 829, in send_message
resent = msg.get_all('Resent-Date')
AttributeError: 'str' object has no attribute 'get_all'
代码(直接链接到的麻烦行)可用 此处.奇怪的是,该代码实际上在实际发送任何电子邮件正文之前发送了 QUIT - 不确定这是否会影响这一点.
The code (troublesome line linked directly to) is available here. Strangely enough, the code actually sends QUIT prior to actually sending any of the email body - not sure if that's something that would affect this.
有谁知道是什么导致了这个错误?
Does anyone know what is causing this error?
编辑 原来我的问题的一部分是我使用的格式不正确.send_message() 需要变量的顺序是Message, From, To
,而我的代码是按照From, To, Message
的顺序发送的.
EDIT Turns out that part of my issue was that I was using the incorrect format. send_message() requires the variables in the order of Message, From, To
, while my code was sending it in the order of From, To, Message
.
但是,我现在收到此错误:
However, I'm now getting this error:
reply: b'235 2.7.0 Accepted
'
reply: retcode (235); Msg: b'2.7.0 Accepted'
send: 'QUIT
'
reply: b'221 2.0.0 closing connection s66sm8443316yhp.2 - gsmtp
'
reply: retcode (221); Msg: b'2.0.0 closing connection s66sm8443316yhp.2 - gsmtp'
Traceback (most recent call last):
File "MIME-base.py", line 108, in <module>
send(fromaddr, toaddrs, msg)
File "MIME-base.py", line 61, in send
server.send_message(msg, fromaddr, toaddrs)
File "/usr/lib/python3.4/smtplib.py", line 839, in send_message
g.flatten(msg_copy, linesep='
')
File "/usr/lib/python3.4/email/generator.py", line 109, in flatten
self._write(msg)
File "/usr/lib/python3.4/email/generator.py", line 189, in _write
self._write_headers(msg)
File "/usr/lib/python3.4/email/generator.py", line 416, in _write_headers
self._fp.write(self.policy.fold_binary(h, v))
File "/usr/lib/python3.4/email/_policybase.py", line 325, in fold_binary
folded = self._fold(name, value, sanitize=self.cte_type=='7bit')
File "/usr/lib/python3.4/email/_policybase.py", line 352, in _fold
parts.append(h.encode(linesep=self.linesep,
AttributeError: 'list' object has no attribute 'encode'
推荐答案
SMTP.send_message
与 SMTP.sendmail
.所以试试:
server.send_message(msg, fromaddr, toaddrs)
编辑:
您还需要单独添加 To:
标题,而不是作为列表:
You also need to add the To:
headers separately, rather than as a list:
for item in input("To: ").split():
msg['To'] = item
这篇关于Python smtplib send_message() 失败,返回 AttributeError: 'str' 对象没有属性 'get_all'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!