1

I'm using EmailMessage to send emails via Amazon SES in Django. I am currently having trouble inserting new lines in the message. "\n" does not seem to work

How do I go about doing this?

As an example, this is what I've tried:

subject= "Test email with newline" 
message = "%s %s is testing an email with a newline.  \nCheck out this link too: http://www.example.com" % (user.first_name, user.last_name)
from_string = "<support@example.com>"
email_message = EmailMessage(subject, message, from_string, [user.email])
email_message.send() 

When I send this email I get:

 Michael Smith is testing an email with a newline. Check out this link too: 
 http://www.example.com

However, I expected the email to be formatted like this:

Michael Smith is testing an email with a newline. 
Check out this link too: http://www.example.com
chandu
  • 1,053
  • 9
  • 18
Michael Smith
  • 3,307
  • 5
  • 25
  • 43

2 Answers2

1

You can use attach_alternative() and provide html - in your case <p> or <br> will do the trick.

or

from django.core.mail import EmailMessage

subject= "Test email with newline"
html_context = "%s %s is testing an email with a newline.  <br>Check out this link too: http://www.example.com" % (user.first_name, user.last_name)
from_string = "<support@example.com>"
msg = EmailMessage(subject,
                   html_context,
                   from_string,
                   [user.email])
msg.content_subtype = "html"
msg.send()
Deja Vu
  • 721
  • 1
  • 8
  • 17
0

The better use is case it try sending html message where you can send email as html. There you can format your msg like whatever you want

gamer
  • 5,673
  • 13
  • 58
  • 91