Dajngo 发送带html标签的数据到邮件中

一、客户需要发送的邮件带一定的样式

   1,这是发送邮件时调用

from EmailSend import *
def QuestionSubmit(req):
    """
    问题提交
    :param req:
    :return:
    """
    send = Mail()
    quest=json.loads(req.body)
    question=SubmitQuestion.objects.create(**quest)
    time=question.create_time.strftime("%Y-%m-%d %H:%M:%S")
    prod=question.prod.name
    types=question.quest_type
    name=question.name
    phone=question.phone
    describe=question.describe
    content='<p>提交时间:%s</p><p>产品:%s</p><p>类型:%s</p><p>用户:%s</p><p>联系方式:%s</p>问题描述:<p>%s</p>'%(time,prod,types,name,phone,describe)
    t1 = threading.Thread(target=send.SendMailWithFile, args=(content,))
    t1.start()
    return JsonResponse({'ret':0,'msg':'success'})

二、在app下有一个同级的发送邮件的py文件发送方式如下

 1,端口需要根据协议发生变化  可以查看:https://blog.csdn.net/qq_15071263/article/details/79727773

# encoding=utf-8
import os
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class Mail(object):
  def __init__(self):
    self.from_addr = 'xxxxx'
    self.password = 'xxxxxx'
    self.smtp_server = "smtp.exmail.qq.com"
    self.server = smtplib.SMTP_SSL(self.smtp_server, 465) #端口号需要根据协议发生变化
    self.server.login(self.from_addr, self.password)

  def SendMailWithFile(self,content):
      to_addr = 'xxxx'  #收件人邮箱
      msg = MIMEText(content,'html', 'utf-8')
      msg['From'] = self.from_addr
      msg['To'] = to_addr
      msg['Subject'] = Header('邮件标题', 'utf-8').encode()
      try:
        self.server.sendmail(self.from_addr, [to_addr], msg.as_string())
      except smtplib.SMTPException, e:
          print "发送失败:", e
  def quit(self):
    self.server.quit()

猜你喜欢

转载自blog.csdn.net/QQ1752506968/article/details/89680261
今日推荐