0

I am developing a website where a user with an specific role can log in and send an email to a mailing list. I am working with Flask, Python and using Mailgun to deliver the emails. I am using an HTML template and the email text is passed as a parameter from the view function to the send email function. The problem is that the email text is delivered ignoring the line breaks. If the email text is:

Line 1
Line 2
Line 3

It will be delivered as:

Line 1 Line 2 Line 3

My view function is:

@main.route('/send-email-to-mailinglist', methods=['POST', 'GET'])
def send_email_to_mailinglist():
    form = MailToMailingList()
    if form.validate_on_submit():
        form.subject = form.subject.data
        form.email_text = form.email_text.data
        send_email('mailing_list@mydomain.com', 
                    form.subject, 'auth/email/to_mailing_list', 
                    description = "now",
                    sender='info', text = form.email_text)
        return redirect(url_for('.index'))
    return render_template ('send_email_to_all.html', form=form)

My send email function is:

def send_email(to, subject, template, **kwargs):  
    app = current_app._get_current_object()
    auth = ('api', app.config['MAILGUN_API_KEY'])
    url = 'https://api.mailgun.net/v3/{}/messages'.format(app.config['MAILGUN_DOMAIN']) 
    data = {
        'from': '<{}@{}>'.format(kwargs['sender'],app.config['MAILGUN_DOMAIN']),
        'to': to,
        'subject': subject,    
        'text': render_template(template + '.txt', **kwargs),
        'html': render_template(template + '.html', **kwargs)
    }
    response = requests.post(url, auth=auth, data=data)
    response.raise_for_status()

And the relevant part of the HTML template is:

<!doctype html>
<html>
  <head>
    <meta name="viewport" content="width=device-width">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style>
    </style>
  </head>
  <body>
..
..
    <p> {{ text }} </p>
..
..
  </body>
</html>
Rodolfo Alvarez
  • 972
  • 2
  • 10
  • 18

1 Answers1

1

Your problem may be that render_template is expecting HTML and therefore reduces newlines to spaces, which is the usual behavior for handling newlines and multiple whitespaces in HTML, see this answer.

For how to work around this, see this answer. Of course, you could also manually insert <br /> tags or else use an editor that handles whitespace-to-tag conversions for you, such as CKEditor or TinyMCE.

Jon Badiali
  • 166
  • 6
  • 3
    Thank you . It works. I used inline `css` so my code was transformed in: `

    {{ text }}

    `
    – Rodolfo Alvarez Apr 18 '18 at 21:13
  • I have done more trials an it doesn't work for `Outlook` – Rodolfo Alvarez Apr 20 '18 at 13:44
  • I was able to make it work for `Outlook`. I use the tag `
    `. On this link  [link](http://jkorpela.fi/HTML3.2/5.44.html) it says: _"In principle, a P tag is not allowed within a PRE element,  However, HTML 2.0 specification encourages browsers to accept it, with the remark a P within a PRE element should produce only one line break, not a line break plus a blank line."_. Based on this information I changed my code as follow: `
                              

    {{ text }}

    `
    – Rodolfo Alvarez Apr 20 '18 at 14:44