【问题标题】:as_string() method return an AttributeErroras_string() 方法返回一个 AttributeError
【发布时间】:2019-12-03 23:36:11
【问题描述】:

我尝试通过这部分代码使用python 3.6从gmail发送消息:

import smtplib as smtp
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os

SUBJECT = *subject message*
SOURCE = *directory*
TEXT = *some text in message*

msg = MIMEMultipart()
msg['From'] = *send from email*
msg['To'] = *send to email*
msg['Subject'] = SOURCE

#################### part with attachment
msg.attach(MIMEText(TEXT, 'plain'))
filename = os.path.basename(SOURCE)
attachment = open(SOURCE, 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename = %s'%filename)
msg.attach(part)
#################### end of attachment part

#################### server part
server = smtp.SMTP(smtp.gmail.com', 587)
server.starttls()
server.login(*send from email*, *send from email password*)
server.sendmail(*send from email*, *send to email*, msg.as_string())
server.close()

但是得到一个属性错误:

AttributeError: 'list' 对象没有属性 'encode'

如果我删除代码的附件部分并在服务器部分之前添加msg = MIMEText(TEXT),我会在我的电子邮件中收到一封信,但它不包含主题。所以我收到一封只有一些文字的信。

我做错了什么?有什么想法吗?

编辑:错误出现在msg.as_string()

【问题讨论】:

    标签: python python-3.x email smtp


    【解决方案1】:

    这是我通过 python 发送电子邮件的方式

    #this will allow us to send emails over our smtp server
    import smtplib
    
    #who the message will be from, doesn't really need to be set since the message variable will do this automatically
    sender ='sender@email.com'
    #who the message will be sent to (this one matters)
    receiver = 'receiver@email.com'
    
    #the actual email we'll be sending, note its contents- the To section is irrelevant but neccessary
    message = """From: From Person <sender@email.com>
    To: To Person <receiver@email.com>
    Subject: SMTP Email Test
    
    this is a test email
    """
    
    try:
        #proc our server to send the mail by providing it's ip to the program
        smtpObj = smtplib.SMTP('<your smtp ip address goes here>')#this is the ip of the server I'm on
        #attempt to send the piece of  mail
        smtpObj.sendmail(sender, receiver, message)
        #if the above executes send a confirmation to the user
        print "Successfully sent mail!"
    except:
        #if something goes wrong catch it ad send a message to the user
        print "Error: unable to send mail"
    
    

    要添加附件,只需添加我从 here 检索到的以下内容

    # open the file to be sent  
    filename = "File_name_with_extension"
    attachment = open("Path of the file", "rb") 
    
    # instance of MIMEBase and named as p 
    p = MIMEBase('application', 'octet-stream') 
    
    # To change the payload into encoded form 
    p.set_payload((attachment).read()) 
    
    # encode into base64 
    encoders.encode_base64(p) 
    
    p.add_header('Content-Disposition', "attachment; filename= %s" % filename) 
    
    # attach the instance 'p' to instance 'msg' 
    smtpObj.attach(p) 
    

    【讨论】:

    • 谢谢。但在这段代码中,没有任何方法可以发送附件。
    • 我很抱歉刚刚意识到我发送了一个旧版本的脚本,我已经用我用来启用附件的资源更新了它。希望这会有所帮助:) 如果您有任何问题,请告诉我
    • 感到很抱歉,但这并没有帮助。我有同样的AttributeError: 'list' object has no attribute 'encode
    • 抱歉没能帮上忙;也许我会把这个资源传递给你,作为最后的努力来帮助你:stackoverflow.com/questions/44607943/…
    【解决方案2】:

    变化:

    msg.attach(MIMEText(TEXT, 'plain'))
    filename = os.path.basename(SOURCE)
    attachment = open(SOURCE, 'rb')
    part = MIMEBase('application', 'octet-stream')
    part.set_payload(attachment.read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename = %s'%filename)
    msg.attach(part)
    

    与:

    msg.attach(MIMEText(text))
    
    for f in files:
        with open(f, "rb") as fil:
            part = MIMEApplication(fil.read(), Name = basename(f))           
        part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
        msg.attach(part)
    

    为我工作。 感谢How to send email attachments?@Oli 的回答。

    【讨论】:

      猜你喜欢
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 2023-02-26
      • 2011-09-27
      • 2013-03-20
      • 2011-11-01
      相关资源
      最近更新 更多