Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:06:10+00:00 2026-06-17T23:06:10+00:00

My Django app needs to send emails in HTML format. As per the official

  • 0

My Django app needs to send emails in HTML format. As per the official documention:

It can be useful to include multiple versions of the content in an
email; the classic example is to send both text and HTML versions of a
message. With Django’s email library, you can do this using the
EmailMultiAlternatives class. This subclass of EmailMessage has an
attach_alternative() method for including extra versions of the
message body in the email. All the other methods (including the class
initialization) are inherited directly from EmailMessage.

…I came up with the following code:

from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives()
msg.sender = "someone@somewhere.com"
msg.subject = subject
msg.to = [target,]
msg.attach_alternative(content, "text/html")
msg.send()

This work as expected. However, in some situations I need to include PDF attachments, for which I added the following code just before msg.send():

if attachments is not None:
    for attachment in attachments:
        content = open(attachment.path, 'rb')
        msg.attach(attachment.name,content.read(),'application/pdf')

Although this works – all PDF documents are properly attached to the email – the unwanted side effect is that the HTML content of the email now has disappeared and I’m left with an empty email body with PDF documents attached to it.

What am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:06:11+00:00Added an answer on June 17, 2026 at 11:06 pm

    The EmailMultiAlternatives is to be used, if you want to provide both plain text and text/html version. Than its up to the email client of the recipient to decide which version to display. What you need is simply:

    from django.core import mail
    
    ....
    
    msg = mail.EmailMessage(subject, content,
                            to=[target], from_email='someone@somewhere.com')
    if attachments is not None:
        for attachment in attachments:
            msg.attach_file(attachment, 'application/zip')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an app that needs to send emails to multiple users.
I'm making a django app that needs to be able to make emails and
In my Django app, I have a page.html that includes forms from multiple models
I'm building a Django app that needs to store multiple selected dates for an
I've got a Django app that needs to take a list of multiple datetimes
I'm working on a Django app that needs to perform operations on specified model
In my requirements.txt file I want to specify that my app needs Django version
I am creating a web app which needs to continuously poll my django web
At one point, my Django app needs to load one of its own pages
I have a Django app that needs to pull the follower_count information from several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.