1

I am using Djrill together with a mailchimp template.

I use this snippet below to push the merge_vars into my template :

 msg.global_merge_vars = {
 SNAME': fullname, 'SEMAIL': from_email,'SMESSAGE': message
            }

The message |SMESSAGE| appears in the email however the line breaks arent preserved. How would I go about keeping the line breaks from my message?

Also please not I cant use {% autoescape on %} method as the template is sent from mailchimp

vvdect
  • 185
  • 1
  • 2
  • 14
  • Djrill passes merge_vars directly to Mandrill, so what you're really asking is how to get a Mandrill/Mailchimp template to preserve line breaks in a merge tag. Someone [asked that](http://stackoverflow.com/questions/29826425/mandrill-ignores-line-breaks-n) a few months ago, with no answer. You might try something like `
    *|SMESSAGE|*
    ` in your template, or you could encode the message to html with `
    `'s in your Django code and then use `*|HTML:SMESSAGE|*` in your template (but watch out for script injection!).
    – medmunds Jul 13 '15 at 00:12
  • I added the .replace('\r\n',r).replace('\n\r',r).replace('\r',r).replace('\n',r) to my form.cleaned_data and it seemed to work by just declaring *|SMESSAGE|* – vvdect Jul 13 '15 at 00:23

1 Answers1

1

Ok this worked for me, I did a str replace on my cleaned message

r = '<br />'
message = form.cleaned_data['message'].replace('\r\n',r).replace('\n\r',r).replace('\r',r).replace('\n',r)

Then merged it the same way

msg.global_merge_vars = {
 SNAME': fullname, 'SEMAIL': from_email,'SMESSAGE': message
            }

And declared it in my mailchimp template

<p> *|SMESSAGE|* </p>
vvdect
  • 185
  • 1
  • 2
  • 14
  • Whenever you're generating html, it's important to escape any html in the user's input. Django has an [escape function](https://docs.djangoproject.com/en/1.8/ref/utils/#django.utils.html.escape) you can use. (Since your html is going into email, [script injection](https://en.wikipedia.org/wiki/Code_injection#HTML_script_injection) may not be a *huge* concern, but it's good to be safe. Also, check whether &, < or > work if the user enters them in the message.) – medmunds Jul 13 '15 at 14:56
  • I think I also had to use `*|HTML:MESSAGE|*`. – Qwerty May 23 '19 at 18:52