简体   繁体   English

通过Python发送电子邮件的程序以“AttributeError:'str'对象退出,没有属性'get_content_maintype''”

[英]Program which send email via Python exits with “AttributeError: 'str' object has no attribute 'get_content_maintype''”

I have python code intended to send an email with an attachment, and I've come down to this: 我有python代码用于发送带附件的电子邮件,我已经归结为:

#!/usr/bin/python
import os, re
import sys
import smtplib

#from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.MIMEText import MIMEText


SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587

sender = 'me@gmail.com'
password = "e45dt4iamkiddingthisisnotmypassword"
recipient = 'he@gmail.com'
subject = 'Python emaillib Test'
message = 'Images attached.'

def main():
    msg = MIMEMultipart()
    msg['Subject'] = 'Python emaillib Test'
    msg['To'] = recipient
    msg['From'] = sender

    msg.attach('/tmp/images/a.gif')

    part = MIMEText('text', "plain")
    part.set_payload(message)
    msg.attach(part)

    session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)

    session.ehlo()
    session.starttls()
    session.ehlo

    session.login(sender, password)

#    my_message=msg.as_string()
    qwertyuiop=msg
    session.sendmail(sender, recipient, qwertyuiop.as_string())

    session.quit()

if __name__ == '__main__':
    main()

And I get this error when running: 运行时出现此错误:

Traceback (most recent call last):
  File "./abcd.py", line 49, in <module>
    main()
  File "./abcd.py", line 44, in main
    session.sendmail(sender, recipient, qwertyuiop.as_string())
  File "/usr/lib/python2.7/email/message.py", line 137, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "/usr/lib/python2.7/email/generator.py", line 83, in flatten
    self._write(msg)
  File "/usr/lib/python2.7/email/generator.py", line 108, in _write
    self._dispatch(msg)
  File "/usr/lib/python2.7/email/generator.py", line 134, in _dispatch
    meth(msg)
  File "/usr/lib/python2.7/email/generator.py", line 203, in _handle_multipart
    g.flatten(part, unixfrom=False)
  File "/usr/lib/python2.7/email/generator.py", line 83, in flatten
    self._write(msg)
  File "/usr/lib/python2.7/email/generator.py", line 108, in _write
    self._dispatch(msg)
  File "/usr/lib/python2.7/email/generator.py", line 125, in _dispatch
    main = msg.get_content_maintype()
AttributeError: 'str' object has no attribute 'get_content_maintype'

I assume that it has to do with msg.attach("/tmp/images/a.gif") but I'm not sure. 我认为它与msg.attach(“/ tmp / images / a.gif”)有关,但我不确定。 The source of the problem is qwertyuiop.as_string() though. 问题的根源是qwertyuiop.as_string()。

The problem is that msg.attach() attaches another message, not a string/filename. 问题是msg.attach()附加了另一条消息,而不是字符串/文件名。 You need to create a MIMEImage object and attach that: 您需要创建一个MIMEImage对象并附加该对象:

# instead of msg.attach('/tmp/images/a.gif')...
fp = open('/tmp/images/a.gif', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msg.attach(msgImage)

Example adapted from here 示例改编自此处

If you want types other than Images, check out http://docs.python.org/library/email.mime.html . 如果您想要图像以外的类型,请查看http://docs.python.org/library/email.mime.html

The reason you're getting the error on the qwertyuiop.as_string() line is that the message isn't parsed until you call as_string() . 您在qwertyuiop.as_string()行上收到错误的原因是,在调用as_string()之前,不会解析该消息。

暂无
暂无

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

相关问题 Python AttributeError:“ str”对象没有属性“ get” - Python AttributeError: 'str' object has no attribute 'get' Python smtplib send_message()失败,返回AttributeError:&#39;str&#39;对象没有属性&#39;get_all&#39; - Python smtplib send_message() failing, returning AttributeError: 'str' object has no attribute 'get_all' AttributeError: 'str' object 没有属性 'send' - AttributeError: 'str' object has no attribute 'send' 如何修复“AttributeError: &#39;str&#39; object has no attribute &#39;content&#39;”python 错误 - how to fix for the "AttributeError: 'str' object has no attribute 'content' "python error AttributeError: 'list' object 没有属性 'encode'。 我如何通过 Excel 发送 Email? Python - AttributeError: 'list' object has no attribute 'encode'. How do i send Email via Excel? Python Python:AttributeError:&#39;str&#39;对象没有属性&#39;get&#39;:Lambda + Map - Python: AttributeError: 'str' object has no attribute 'get': Lambda + Map Python AttributeError:“ str”对象没有属性“ get_price” - Python AttributeError: 'str' object has no attribute 'get_price' AttributeError: &#39;str&#39; 对象没有属性 &#39;content&#39; - AttributeError: 'str' object has no attribute 'content' Azure ResourceGraph Python AttributeError: 'str' object 没有属性 'get' - Azure ResourceGraph Python AttributeError: 'str' object has no attribute 'get' Python AttributeError:&#39;str&#39;对象没有属性&#39;DataFrame&#39; - Python AttributeError: 'str' object has no attribute 'DataFrame'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM