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 241599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:46:01+00:00 2026-05-11T20:46:01+00:00

I’m using django.core.mail.EmailMultiAlternatives when sending e-mails from my django app in an attempt to

  • 0

I’m using django.core.mail.EmailMultiAlternatives when sending e-mails from my django app in an attempt to make sure that the message downgrades to text if the e-mail client doesn’t support HTML.

Here is my send_email method:

def send_email(self, from_address, to_list, subject, msg_text, msg_html):
        subject=subject.replace('\r','').replace('\n',' ')
        self.msg = EmailMultiAlternatives(subject, msg_text, from_address, to_list)
        self.msg.attach_alternative(msg_html, "text/html")
        self.msg.content_subtype = "html"
        self.msg.send()

It works great with Gmail, Hotmail and many other e-mail clients – displaying the HTML content without a problem. But it will not display the HTML content in Outlook 2003 running on Win2003 – just the text version.

If I forcefully put the HTML in the EmailMultiAlternatives call, i.e. use msg_html instead of msg_text like so:

self.msg = EmailMultiAlternatives(subject, msg_html, from_address, to_list)

then it works correctly in all clients; but that means that there is no text fallback for clients that don’t support HTML or (more likely) that have disabled support for it.

I think it is worth mentioning that the e-mail is being generated on a django app running on Mac OS X (just in case it has to do with line terminator differences between the OSes).

I see that people using other languages have had similar problems with outlook…

I wonder if anyone has any idea of WHY outlook would behave differently and if there is simple fix that can be applied in my code?

  • 1 1 Answer
  • 1 View
  • 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-05-11T20:46:01+00:00Added an answer on May 11, 2026 at 8:46 pm

    I don’t have an Outlook installation available to test this, so I’m wondering about the reason for the fifth line in your function.

    self.msg.content_subtype = "html"

    I don’t know much about multipart email internals, but on my system that line causes both parts of the message have a content-type of text/html. Leaving it out produces a message with “Content-Type: text/plain” on the first part and “Content-Type: text/html” on the second.

    In any case, one of the answers to the question about Java mentions changing the character set to iso-8859-1. I think you should be able to do that with django.core.mail.

    The EmailMessage class (from which EmailMultiAlternatives inherits) has an attribute named “encoding” which sets the charset to use. By default it’s None so the default charset of utf-8 (unless overridden in settings) is used instead.

    In other words, add something like the following before the send line in the function listed in the question:

    self.msg.content_subtype = "iso-8859-1"

    Unfortunately, that will only change the encoding specified on the first part (msg_text in the function above). The function that attaches the alternative content doesn’t seem to use the encoding attribute. I’m not sure it’s the correct approach but I subclassed EmailMultiAlternatives to override the relevant function and it seemed to work okay.

    class EmailMultiAlternativesWithEncoding(EmailMultiAlternatives):
        def _create_attachment(self, filename, content, mimetype=None):
            """
            Converts the filename, content, mimetype triple into a MIME attachment
            object. Use self.encoding when handling text attachments.
            """
            if mimetype is None:
                mimetype, _ = mimetypes.guess_type(filename)
                if mimetype is None:
                    mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
            basetype, subtype = mimetype.split('/', 1)
            if basetype == 'text':
                encoding = self.encoding or settings.DEFAULT_CHARSET
                attachment = SafeMIMEText(smart_str(content,
                    settings.DEFAULT_CHARSET), subtype, encoding)
                # original text being replaced above (not last argument)
                # attachment = SafeMIMEText(smart_str(content,
                #     settings.DEFAULT_CHARSET), subtype, settings.DEFAULT_CHARSET)
            else:
                # Encode non-text attachments with base64.
                attachment = MIMEBase(basetype, subtype)
                attachment.set_payload(content)
                Encoders.encode_base64(attachment)
            if filename:
                attachment.add_header('Content-Disposition', 'attachment',
                                      filename=filename)
            return attachment

    I’m not sure if the “smart_str(content, settings.DEFAULT_CHARSET)” part should also reference “encoding” rather than “settings.DEFAULT_CHARSET” but that’s the message body handling text is written (django.core.mail.EmailMessage.message).

    As I said, I don’t have Outlook so I can’t actually test the Outlook aspect but it does seem to change the charset to iso-8859-1 for both parts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 112k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The issue is likely in this line: [self.mobil addObject:[product copy]];… May 11, 2026 at 10:00 pm
  • Editorial Team
    Editorial Team added an answer I have found the issue. Excel was apparently unable to… May 11, 2026 at 10:00 pm
  • Editorial Team
    Editorial Team added an answer The worker thread just sends an async message to the… May 11, 2026 at 10:00 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.