我正在使用 Flak Mail 从网站的联系表单发送电子邮件。联系表单还有一个
sender
电子邮件 ID 字段。因此,当我收到“收件人”电子邮件 ID 的电子邮件时,我希望将 from
设置为 sender
电子邮件 ID,以便在表单上输入值。以下是处理电子邮件的类:
from flask_mail import Mail, Message
class SendMail:
def __init__(self, app):
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = '<password>'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
self.recipient_email = '[email protected]'
self.app = app
self.mail = Mail(self.app)
def compile_mail(self, name, email_id, phone_no, msg):
message = Message(name+" has a query",
sender = email_id,
recipients=[self.recipient_email])
message.html = "<b>Name : </b>"+name+"<br><b>Phone No :</b> "+phone_no+"<br><b>Message : </b>"+msg
return message
def send_mail(self, name, email_id, phone_no, msg):
message = self.compile_mail(name, email_id, phone_no, msg)
with self.app.app_context():
self.mail.send(message)
send_mail
方法接收四个参数,它们是在联系表单上输入的字段。问题是,当我像这样发送电子邮件时,收到的电子邮件中的 from
被设置为 SMTP MAIL_USERNAME [email protected]
,尽管事实上我将 Message 对象中的 sender
参数设置为从 SMTP 接收的 email_id
联系表。
无法弄清楚如何将发送者设置为我想要的值。
如果您的意思是要将发件人名称设置为非电子邮件地址(例如发件人=“Stackoverflow”),您可以将发件人争论设置为元组。
message = Message(name+" has a query",
sender = (email_id, app.config["MAIL_USERNAME"]),
# 'email_id' will replace 'app.config["MAIL_USERNAME"]' as the name that shows up in your email
recipients=[self.recipient_email])