2016-09-25 43 views
-1

我使用Flak Mail從網站的聯繫表單發送電子郵件。聯繫表單還有一個sender電子郵件ID字段。因此,當我收到「收件人」電子郵件ID的電子郵件時,我想將from設置爲sender電子郵件ID,以便在表單中輸入值。以下是處理電子郵件的類:如何在使用Flask郵件時設置發件人電子郵件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方法接收四個參數是哪些領域的聯繫表格上輸入。問題是,當我發送這樣的電子郵件時,儘管事實上我將郵件對象中的sender參數設置爲email_id,但收到的電子郵件中的from被設置爲SMTP MAIL_USERNAME [email protected]。 無法弄清楚如何將發件人設置爲我想要的值。

回答

0

您是否檢查過您傳入的email_id是您認爲的內容?

The Flask Mail source code建議如果您傳入的sender是虛假的(例如None或空字符串),它將僅使用默認值。

+0

是的,它是正確的。我調試了代碼來檢查它。 –

相關問題