AttributeError: 'str' 对象没有属性 'policy' [英] AttributeError: 'str' object has no attribute 'policy'

查看:66
本文介绍了AttributeError: 'str' 对象没有属性 'policy'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手.我正在尝试制作一个可以发送电子邮件的电子邮件脚本.首先,我制作了一个没有任何类的 Python 脚本,只是为了确保脚本按预期运行.在我得到预期的结果之后.我正在尝试使用类重写脚本,以便学习.但我收到错误,我不明白.我不明白问题究竟出在哪里.

I am new to Python. I am trying to make an email script that can send an email. First, I made a Python script without any classes, just function just to make sure that the script runs as expected. After I got the expected result. I am trying to rewrite the script using classes, so as to learn. But I am getting error, which I don't understand. I don't understand where actually the problem lies.

下面是代码和错误截图

import smtplib
import os
import sys
import mimetypes #for guess mime types of attachment

from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage

class Email(object):
  message = None
  subject = None
  from_address = None
  to_address = None
  body = None
  email_server = None
  attachment = None

  def __init__(self,from_address,to_address,subject,body,attachment,email_server):
    self.message = MIMEMultipart()
    self.message['subject'] = subject
    self.message['From'] = from_address
    self.message['TO'] = to_address
    self.body = MIMEText(body, 'plain')
    self.message.attach(body)
    self.email_server = email_server

    if attachment is not None:
      self.attachment = attachment
      self.attach_attachment()

  def get_message(self):
    return self.message

  def send_message(self,auth):
    username, password = auth.get_user_auth_details()

    server = smtplib.SMTP(self.email_server)
    server.starttls() #For Encryption
    server.login(username, password)
    server.send_message(self.message)
    server.quit()

  def attach_attachment(self):
    self.messaege = self.attachment.set_attachment_type(self.message)



class Security(object):
  username = ""
  password = ""

  def __init__(self,username, password):
    self.username = username
    self.password = password


  def get_user_auth_details(self):
    return self.username, self.password

class Attachment(object):
  attachment_path = ''

  def __init__(self,attachment_path):
    self.attachment_path = attachment_path

  def is_directory(self):
    return os.path.isdir(self.attachment_path)

  def is_file(self):
    return os.path.isfile(self.attachment_path)

  def guess_and_get_attachment_type(self, filenamepath):
    ctype, encoding = mimetypes.guess_type(filenamepath)

    if ctype is None or encoding is not None:
      # No guess could be made, or the file is encoded (compressed), so
      # use a generic bag-of-bits type.
      ctype = "application/octet-stream"

    maintype , subtype = ctype.split('/' , 1)

    if maintype == 'text':
      fp = open(filenamepath)
      attachment = MIMEText(fp.read() , subtype)
      fp.close()
    elif maintype == 'image':
      fp = open(filenamepath , 'rb')
      attachment = MIMEImage(fp.read() , subtype)
      fp.close()
    elif maintype == 'audio':
      fp = open(filenamepath , 'rb')
      attachment = MIMEAudio(fp.read() , subtype)
      fp.close()
    else:
      fp = open(filenamepath , 'rb')
      attachment = MIMEBase(maintype , subtype)
      attachment.set_payload(fp.read()) #Actual message
      fp.close()
      encoders.encode_base64(attachment) # Encode the payload using Base64

    return attachment

  def set_attachment_type(self,message):
    if(self.is_directory()):
      for filename in os.listdir(self.attachment_path):
        filenamepath = os.path.join(self.attachment_path , filename)
        attachment = self.guess_and_get_attachment_type(filenamepath)
        # Set the filename parameter
        attachment.add_header('Content-Disposition', 'attachment', filename = filenamepath)
        message.attach(attachment)

    elif(self.is_file()):
      attachment = self.guess_and_get_attachment_type(self.attachment_path)
      # Set the filename parameter
      attachment.add_header('Content-Disposition', 'attachment', filename = self.attachment_path)
      message.attach(attachment)
    else:
      print("Unable to open file or directory")

    return message



def main():
  #Constants
  GMAIL_SERVER = "smtp.gmail.com:587"
  FROM_ADDRESS = "xyz@gmail.com"
  TO_ADDRESS = "xzy@gmail.com"

  auth = Security("xyz@gmail.com" , "MySuperSecretPassword")
  attachment = Attachment("/path/to/attachment/file/or/directory")
  email = Email(FROM_ADDRESS ,TO_ADDRESS, "Hi from class Python" , "OOPs Python at Work!!" ,attachment,GMAIL_SERVER )

  email.send_message(auth)
if __name__ == '__main__':
  main()

推荐答案

我改了

self.message.attach(body) #In the class email constructor

self.message.attach(self.body) #In the class email constructor

它奏效了.

我将字符串类型附加到消息而不是 MIMEText

I was attaching string type to message instead of MIMEText

这篇关于AttributeError: 'str' 对象没有属性 'policy'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆