【问题标题】:Django Email Templates Rendering as Text呈现为文本的 Django 电子邮件模板
【发布时间】:2020-12-29 23:21:26
【问题描述】:

我正在尝试通过 SendGrid API 使用 Django 动态电子邮件模板。我知道已经有一些线程在这方面,但是,我无法让这些工作。

我有一个基本模板,其中包含一些基于我希望通过电子邮件发送给用户的搜索的动态变量。尽管应用了我在此处的许多其他线程中看到的内容,但除了 HTML 始终呈现在文本中之外,这都是有效的。

如果有任何帮助,将不胜感激。

Views.py

def customer_selections_sent(request):
   #make=request.POST['makeselection']
    if request.method == 'GET':

        current_site = get_current_site(request)
        emailto = request.user.email_user
        user = request.user.username

        #call session values from SearchInventory function
        modelvalue =request.session.get('modelvalue')
        makevalue =request.session.get('makevalue')
        subject = 'ShowVroom Selections'

        #create variables to be used in the email template
        Email_Vars = {
            'user': user,
            'make': makevalue,
            'model': modelvalue,
            'domain': current_site.domain,
        }

        #create the email msg
        message = get_template('customer_selections_email.html').render(Email_Vars)
    html_message = get_template('customer_selections_email.html').render(Email_Vars)
    message.content_subtype = "html"  # Main content is now text/html
    

#send email


request.user.email_user(subject,message)
#request.user.email_user(subject, html_message)
#return redirect('customer_selections_sent')
return render(
    request,
    'customer_selections_sent.html',
    {
        'title':'Deals are on the way',
        'body':'We will email you shortly with more detail on your choices, you can respond to the dealers via the app until you agree to a viewing, test or purchase ',
        'year':datetime.now().year,
    }
)

send_mail(subject, message,  'Abc@xyz.org', emailto, fail_silently=False,html_message=html_message)
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
#log respon
print(response.status_code)
print(response.body)
print(response.headers)

HTML 模板

<!DOCTYPE html>
<html lang="en">
{% load static %}
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Your ShowVroom Selections</title>

<img align="left" src="Logo1.png" alt="ShowVroom">
</head>

<body>
Hi {{ user }},

Here are your search selections;

<Strong>
    <br>Make;           {{make}} </br>
    <br>Model;          {{model}} </br>
</Strong>

</body>

<foot>
<a href = "mailto:info@showvroom.ie?subject = Offer&body = Message">
Contact ShowVroom
</a>
</foot>

</html>

更新视图.PY

def customer_selections_sent(request):

    if request.method == 'GET':

        #User variables
        emailto = request.user.email_user
        user = request.user.username

        #call session values from another function
        
        modelvalue =request.session.get('modelvalue')
        makevalue =request.session.get('makevalue')
        subject = 'ShowVroom Selections'

        #create variables to be used in the email template
        
        Email_Vars = {
            'user': user,
            'make': makevalue,
            'model': modelvalue,
            #'offer': DisplayInventory.GaragePrice,
            'domain': current_site.domain,
        }

        #create the email msg
        
        message = get_template('customer_selections_email.html').render(Email_Vars)
        html_message = get_template('customer_selections_email.html').render(Email_Vars)
        message.content_subtype = "html"  # Main content is now text/html


    #Redirect to sent email Webpage
    
    request.user.email_user(subject,message)
    return render(
        request,
        'customer_selections_sent.html',
        {
            'title':'Deals are on the way',
            'body':'We will email you shortly with more detail on your choices, you can respond to the dealers via the app until you agree to a viewing, test or purchase ',
            'year':datetime.now().year,
        }
    )
    
    #send email
    
    send_mail(subject, message,  'email@domain.com', emailto, fail_silently=False,html_message=html_message)
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sg.send(message)
    
    #log responses
    
    print(response.status_code)
    print(response.body)
    print(response.headers)

【问题讨论】:

    标签: django django-templates sendgrid email


    【解决方案1】:

    您需要为html_message parameter to the send_email function. 提供一个值,正如您所发现的,message 是电子邮件文本版本的值。

    【讨论】:

    • 所以我为 html_message 添加了一个附加变量,并将 html_message 添加到 send_mail 函数中。尽管收到的电子邮件仍然是文本格式的 html,但我在这里似乎仍然做错了什么。
    • 请使用您正在运行的代码创建一个新的代码段。还请确保其格式正确。目前很难调试你的情况。
    • 现在应该更容易理解了!
    • 发送电子邮件的目的是什么?
    • send_mail(主题,消息,'email@domain.com',emailto,fail_silently=False,html_message=html_message)
    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2015-03-30
    • 2020-09-18
    相关资源
    最近更新 更多