Garrett
Garrett

Reputation: 1792

Django New lines in view variable

I am sending email with Django using Sendgrid. I have a variable message for the message that will send, however the message holds the value of a few other variables. I would like them to be on different lines to make the email easier to read. Here is what I have, although it is not working.

if form.is_valid():
    name = form.cleaned_data.get('name')
    phone = form.cleaned_data.get('phone')
    email = form.cleaned_data.get('email')
    party_size = form.cleaned_data.get('party_size')
    form_message = form.cleaned_data.get('message')
    listing_address = listing.address
    message = name + "\n" + phone + "<br>" + email + "<br>" + party_size + "<br>" + listing_address

    send_mail('New Lead', message, 'to email', ['[email protected]'], fail_silently=False)

The email is being sent as this:

garrett 1234234<br>[email protected]<br>2<br>address would be here

Although I would like this:

garrett
1234234
[email protected]
2
address would be here

Upvotes: 2

Views: 310

Answers (4)

Kaushal
Kaushal

Reputation: 662

The best way is to create email template & provide context to email template then use generate email content. Use this content(generated_html) in send_email as parameter html_message

    send_mail('New Lead', message, 'to email',
        ['[email protected]'], fail_silently=False, html_message=genenrated_html)

Upvotes: 1

Niranj Rajasekaran
Niranj Rajasekaran

Reputation: 778

No need to change the django snippet that you have given here

In Sendgrid there is a setting to do this

In the settings page where you need to make the status as on. All your plain text will be converted / parsed to HTML view

Upvotes: 0

laur
laur

Reputation: 500

trata utilizando el siguiente codigo:

if form.is_valid():
    name = form.cleaned_data.get('name')
    phone = form.cleaned_data.get('phone')
    email = form.cleaned_data.get('email')
    party_size = form.cleaned_data.get('party_size')
    form_message = form.cleaned_data.get('message')
    listing_address = listing.address
    message = "<html><body><p>" name + "</p><br><p>" + phone + "</p>
    <br><p>" + email + "</p><br><p>" + party_size + "</p><br><p>" + 
     listing_address + "</p><br></body></html>"
    msg = EmailMessage(subject, message, from_email,['[email protected]'])
    msg.content_subtype = "html"   # El contenido ahora sera text/html   
    send_mail('New Lead', message, 'to email', ['[email protected]'], fail_silently=False)

Upvotes: 0

neverwalkaloner
neverwalkaloner

Reputation: 47354

You can send HTML version of email with EmailMessage:

from django.core.mail import EmailMessage

message = name + "<br>" + phone + "<br>" + email + "<br>" + party_size + "<br>" + listing_address
msg = EmailMessage(subject, message, from_email, ['[email protected]'])
msg.content_subtype = "html"  # Main content is now text/html
msg.send()

Upvotes: 0

Related Questions