“function”对象没有属性“encode”错误使用AWS SES + Python SMTP

wwwo4jvm  于 2023-08-02  发布在  Python
关注(0)|答案(1)|浏览(129)

我尝试使用AWS SES从Python SMTP发送电子邮件,但收到此错误。

'function' object has no attribute 'encode'

字符串
发送的电子邮件如下所示

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

import common.config_constants as constants

class Email_util:

    def __init__(self) -> None:
        self.smtp_server = 'email-smtp.us-east-1.amazonaws.com'
        self.smtp_port = constants.SMTP_PORT
        self.workmail_user = constants.SMTP_USER
        self.workmail_pass = constants.SMTP_PASS
        self.sender = constants.FORGOT_PASSWORD_EMAIL_HANDLER
        pass

    def send_email(self, receiver, subject, body):
        msg = MIMEMultipart()
        msg['From'] = self.send_email
        msg['To'] = receiver
        msg['Subject'] = subject

        # Attach the email body
        msg.attach(MIMEText(body, 'plain'))

        try:
            # Establish a connection to Amazon WorkMail SMTP server
            server = smtplib.SMTP(self.smtp_server, self.smtp_port)
            server.starttls()

            # Login to Amazon WorkMail
            server.login(self.workmail_user, self.workmail_pass)

            # Send the email
            server.sendmail(self.sender, receiver, msg.as_string())

            print("Email sent successfully!")
            return True
        except for Exception as e:
            print(f"Error sending email: {e}")
            return False


任何帮助都是感激的。提前致谢

hujrc8aj

hujrc8aj1#

def send_email(self, receiver, subject, body):
        msg = MIMEMultipart()
        msg['From'] = self.send_email

字符串
你把函数self.send_email作为msg['From']的值放在这里,这应该是一个字符串。Python中的字符串有一个名为encode的属性,它实际上是一个用于将字符串从一种格式转换为另一种格式的函数(例如,将'UTF-8'转换为字节数组)。
希望有帮助:)

相关问题