danb333
danb333

Reputation: 1284

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!

Upvotes: 7

Views: 8575

Answers (4)

Lateef
Lateef

Reputation: 1

Hello this is working for me using "f" string see example bellow.

f'New contact from : {message_name} \n Email: {message_email}  \n 
            Subject: {message_subject}  \n Message:{message_content}'

Upvotes: 0

nitin chauhan
nitin chauhan

Reputation: 51

yes "%0A" for a new line it works well.

Upvotes: 1

James Gray
James Gray

Reputation: 11

try using:

%0A for a new line
%26 for &
%3f for ?

Upvotes: 0

Vladislav Mitov
Vladislav Mitov

Reputation: 1950

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)

Upvotes: 4

Related Questions