0

I'm trying to send email message in one of my views and would like to format the body of the message, such that it shows in different lines.

This is a snippet code in views.py:

  body = "Patient Name: " +  patient_name + \
                   "Contact: " + phone + \
                   "Doctor Requested: Dr. " +  doctor.name + \
                   "Preference: " + preference

  email = EmailMessage('New Appointment Request', body, to=['ex@gmail.com'])
  email.send()

The email is shown like this:

Patient Name: AfrojackContact: 6567892Doctor Requested: Dr. IrenaPreference: Afternoon

How do I make it show like this:

Patient Name: Afrojack

Contact: 6567892

Doctor Requested: Dr. Irena

Preference: Afternoon
Emma
  • 27,428
  • 11
  • 44
  • 69
James L.
  • 1,143
  • 22
  • 52

4 Answers4

3

you should add '\n' for newlines.

or you could try this:

body = '''Patient Name: {}
Contact: {}
Doctor Requested: Dr. {}
Preference: {}'''.format(patient_name, phone, doctor.name, preference)

or, if you are using python >= 3.6:

body = f'''Patient Name: {patient_name}
Contact: {phone}
Doctor Requested: Dr. {doctor.name}
Preference: {preference}'''
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
3

I suggest to use the django template system to do that.

You could do:

```
from django.template import loader, Context

def send_templated_email(subject, email_template_name, context_dict, recipients):


    template = loader.get_template(email_template_name)

    context = Context(context_dict)

    email = EmailMessage(subject, body, to=recipients)
    email.send()

```

The template will look like: this could for example be in the file myapp/templates/myapp/email/doctor_appointment.email:

```

Patient Name: {{patient_name}}

Contact: {{contact_number}}

Doctor Requested: {{doctor_name}}

Preference: {{preference}}
```

and you will use it like

```
context_email = {"patient_name" : patient_name,
    "contact_number" : phone,
    "doctor_name":  doctor.name,
    "preference" :  preference}

send_templated_email("New Appointment Request", 
                     "myapp/templates/myapp/email/doctor_appointment.email",
                     context_email, 
                     ['ex@gmail.com'])
```

This is very powerfull, because you can style all the email in the way you want, and you re-use the same function over and over, just need to create new template and pass appropriate context/subject and recipietns

mattions
  • 304
  • 1
  • 6
  • This seems like it could be very helpful but I think it is missing a few items for the first snippet. Could you please add: which file this is saved in, the import for EmailMessage(), the line where you set the value for body, and how you are using context? I also thing the doctor_appointment.email should be doctor_appointment.html. Please let me know if I'm wrong on any of that. This is a great answer would love to use this approach. I need to format my messages with html before sending. – notmark Oct 21 '21 at 14:29
2

You are going right but you just missed a letter n

body = "Patient Name: " +  patient_name + "\n"
                   + "Contact: " + phone + "\n"
                   + "Doctor Requested: Dr. " +  doctor.name + "\n"
                   + "Preference: " + preference

This will add new line after each and every line and most probably solve your problem.

Arpit Goyal
  • 2,212
  • 11
  • 31
1

this should do the trick for breakline:

\n
romainm
  • 281
  • 8
  • 16