我尝试使用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
型
任何帮助都是感激的。提前致谢
1条答案
按热度按时间hujrc8aj1#
字符串
你把函数
self.send_email
作为msg['From']
的值放在这里,这应该是一个字符串。Python中的字符串有一个名为encode
的属性,它实际上是一个用于将字符串从一种格式转换为另一种格式的函数(例如,将'UTF-8'转换为字节数组)。希望有帮助:)