Django 发送带有链接的电子邮件 [英] Django sending emails with links
问题描述
我有一个发送代码:
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()
在我的模板中
<!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>
但是这段代码会生成这样的链接:http://mail.google.com/en-us/results/30/一个>
But this code generate link like this: http://mail.google.com/en-us/results/30/
results/30 没关系,但我得到的是 mail.google.com 而不是mysite.com"在我的网站中没有/en-us/它只有/en/
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/
你们有什么想法吗?
推荐答案
我怀疑您的电子邮件模板中的链接中缺少绝对网址.当您发送电子邮件时,django 会使用相对 url/results/30/"填充 url,并且您的 gmail 邮件客户端可能正在使用它自己的域和语言前缀路径填充 url.
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>
或者为了避免在模板中对域进行硬编码,您可以使用以下建议的技术之一,使用 django 站点框架或类似框架:
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:
这篇关于Django 发送带有链接的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!