Google App Engine: how to send html using send_mail

后端 未结 2 1649
忘了有多久
忘了有多久 2021-02-06 15:52

I have a app with a kind of rest api that I\'m using to send emails . However it currently sends only text email so I need to know how to modify it and make it send html . Below

相关标签:
2条回答
  • 2021-02-06 16:38

    Have a look to the Email message fields of the send_mail function.
    Here is the parameter you need:

    html
    An HTML version of the body content, for recipients that prefer HTML email.

    You should add the html input parameter like this:

    #Your html body
    mail_html_body = '<h1>Hello!</h1>'
    
    # read data from request
    mail_to = str(self.request.POST.get('to'))
    mail_from = str(self.request.POST.get('from'))
    mail_subject = str(self.request.POST.get('subject'))
    mail_body = str(self.request.POST.get('body'))
    
    mail.send_mail(mail_from, 
                   mail_to,
                   mail_subject, 
                   mail_body,
                   html = mail_html_body ) #your html body
    
    0 讨论(0)
  • 2021-02-06 16:38

    you can use the html field of EmailMessage class

    message = mail.EmailMessage(sender=emailFrom,subject=emailSubject)
    message.to = emailTo
    message.body = emailBody
    message.html = emailHtml
    message.send()
    
    0 讨论(0)
提交回复
热议问题