Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include newline in email message using send_mail and django?

I would like to send email message with newlines. Let say, by following code:

send_mail("subject", "Hi  George\n Thanks for registration", "from", "to")

I expect:

Hi George

Thanks for registration

Whereas that what I get is: Hi George\n Thanks for registration

Email is send to a gmail account if that matters.

Any ideas?

Thanks!

like image 432
danb333 Avatar asked Sep 09 '12 13:09

danb333


People also ask

How do you add a new line in an email body in Python?

Newline code \n (LF), \r\n (CR + LF) Inserting a newline code \n , \r\n into a string will result in a line break at that location. On Unix, including Mac, \n (LF) is often used, and on Windows, \r\n (CR + LF) is often used as a newline code. Some text editors allow you to select a newline code.

How do I send an email to multiple recipients in Django?

How to send multiple mass emails django. We need to create a Tuple of messages and send them using send mass mail. In this tutorial, we create a project which sends email using Django. We fill the data in the form and send it using Django Email.

What is linebreaks in Django?

{{ text|escape|linebreaks }} is a common idiom for escaping text contents, then converting line breaks to <p> tags.


1 Answers

The best way you can accomplish that is by puting the mail text in template and use django template loader to render it with context.

from django.template.loader import render_to_string
context = {}  # Fill it with your context
send_mail(
    'Subject',
    render_to_string('core/emails/email.txt', context),
    '[email protected]',
    ['[email protected]'],
    fail_silently=False)
like image 75
Vladislav Mitov Avatar answered Oct 22 '22 03:10

Vladislav Mitov