5

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?

Thomas Jerkson
  • 229
  • 1
  • 2
  • 7

1 Answers1

2

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?

Community
  • 1
  • 1
Joe J
  • 9,985
  • 16
  • 68
  • 100