PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 23, 2022

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

 October 23, 2022     django, email, newline     No comments   

Issue

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!


Solution

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),
    'sender@mail.com',
    ['receiver@mail.com'],
    fail_silently=False)


Answered By - Vladislav Mitov
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing