Search code examples
pythondjangodjango-email

Format email message in Django


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

Solution

  • 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.