Django sending emails with links

删除回忆录丶 提交于 2021-01-03 06:42:53

问题


I have a sending code:

def send_email(site_id, email):
    subject = "Sub"
    from_email, to = EMAIL_FROM, email
    text_content = 'Text'
    html_content = render_to_string(
        'app/includes/email.html',
        {'pk': site_id}
    )
    msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()

And in my template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>title</title>
</head>
<body>
    <a href="{% url 'mail_view' pk %}">Click</a>
</body>
</html>

But this code generate link like this: http://mail.google.com/en-us/results/30/

results/30 it's fine, but I get mail.google.com instead "mysite.com" and in my site there is no /en-us/ its only /en/

Do you guys have any idea?


回答1:


I suspect that you are missing the absolute url in the link within your email template. When you send out an email, django fills in the url with a relative url "/results/30/" and perhaps your gmail mail client is filling in url with it's own domain and language prefix path.

You might try something like this:

<a href="https://yoursite.com{% url 'mail_view' pk %}">Click</a>

Or to avoid hard-coding your domain in your template, you can use one of the techniques suggested below using the django sites framework or similar:

How can I get the domain name of my site within a Django template?



来源:https://stackoverflow.com/questions/33054658/django-sending-emails-with-links

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!