Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get a single linebreak while sending email through Sendgrid

I'm using django.core.mail to send email. I want to achieve a single linebreak at some points in the email, as shown here:

Line 1
Line 2

Line 3
Line 4

I tried doing this using \n after Line 1 and Line 3, and \n\n after Line 2 and Line 4.

When I send the email, it is received like this:

Line 1 Line 2

Line 3 Line 4

As per recommendations on other StackOverflow answers, I tried using the django.template.loader.render_to_string method to generate the appropriate string from a file, but it generates a string with the \n in the exact places where I put them, and so on sending the email it produces the same undesired result.

When I print the string to stdout it appears as desired.

In summary, the \n\n are working but the \n are getting converted to spaces while sending email through Django's send_mail. How can this be solved? Please note that this question is regarding plaintext emails, not HTML ones.

EDIT: Source, as requested in the comments:

from django.core.mail import send_mail
send_mail(
    'Test subject',
    'Line 1\nLine 2\n\nLine 3\nLine 4\n\n',
    '[email protected]',
    ['[email protected]'],
)

Sent using SMTP via SendGrid, as is specified in my settings.py file:

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'myusername'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
like image 395
Yash Tewari Avatar asked Jul 02 '16 08:07

Yash Tewari


People also ask

How many emails can SendGrid send at once?

Currently, you may make up to 10,000 requests per second to our endpoint. Each email you send may include up to 1000 recipients.

How SendGrid SMTP works?

Simple Mail Transfer Protocol (SMTP) is a quick and easy way to send email from one server to another. SendGrid provides an SMTP service that allows you to deliver your email via our servers instead of your own client or server. This means you can count on SendGrid's delivery at scale for your SMTP needs.

How do I send a SendGrid API email?

Send your email using the APIPaste the curl call into your favorite text editor. Copy your API key and paste it in the "Authorization" header. In the data section, specify the "to", "from", and "reply to" names and email addresses and enter a subject. Copy the code and paste it in your terminal.


1 Answers

This is happening because you're using Sendgrid, which in some cases will convert your plain text messages to HTML. Most likely you have configured it to do open/click tracking and it cannot do these in plain text, so it converts your message to HTML.

The link above offers these solutions:

  1. You can turn off the filters causing the conversion from plain text to HTML.

  2. If you start each line with a space, this will add a “preformatted” tag around the line.

  3. You can separate new sentences with double newlines, which will add a "paragraph" tag around the sentence.

  4. You can convert your message to HTML, bypassing our need to convert it altogether

  5. To disable conversion globally, visit Mail Settings --> Plain Content and enabled the suppression.

like image 143
solarissmoke Avatar answered Sep 23 '22 02:09

solarissmoke