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>
{{ text }}
` – Rodolfo Alvarez Apr 18 '18 at 21:13