generating emails from Python

  • Thread starter Michele Simionato
  • Start date
M

Michele Simionato

Is it possible, using email.MIMEMessage, to generate an email with
both HTML and plain text content, in such a way that:
1) it is displayed as HTML if the email client recognizes HTML;
2) it is displayed as plain text if the email client is HTML impaired?

I realize that the answer is most probably no, but one can always hope
....

Michele Simionato
 
B

Benjamin Niemann

Michele said:
Is it possible, using email.MIMEMessage, to generate an email with
both HTML and plain text content, in such a way that:
1) it is displayed as HTML if the email client recognizes HTML;
2) it is displayed as plain text if the email client is HTML impaired?

I realize that the answer is most probably no, but one can always hope
yep, you can:

outer = MIMEMultipart("alternative")
outer['Subject'] = subject
outer['To'] = email_to
outer['From'] = email_from
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
# To guarantee the message ends with a newline
outer.epilogue = ''

part = MIMEText(your_plain_message, "plain", "iso-8859-1")
outer.attach(part)

part = MIMEText(your_html_message, "html", "iso-8859-1")
outer.attach(part)
 
L

Larry Bates

Michele said:
Is it possible, using email.MIMEMessage, to generate an email with
both HTML and plain text content, in such a way that:
1) it is displayed as HTML if the email client recognizes HTML;
2) it is displayed as plain text if the email client is HTML impaired?

I realize that the answer is most probably no, but one can always hope
...

Michele Simionato

Google is your friend:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/67083

Larry Bates
 
M

Michele Simionato

Benjamin Niemann said:
Michele said:
Is it possible, using email.MIMEMessage, to generate an email with
both HTML and plain text content, in such a way that:
1) it is displayed as HTML if the email client recognizes HTML;
2) it is displayed as plain text if the email client is HTML impaired?

I realize that the answer is most probably no, but one can always hope
yep, you can:

outer = MIMEMultipart("alternative")
outer['Subject'] = subject
outer['To'] = email_to
outer['From'] = email_from
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
# To guarantee the message ends with a newline
outer.epilogue = ''

part = MIMEText(your_plain_message, "plain", "iso-8859-1")
outer.attach(part)

part = MIMEText(your_html_message, "html", "iso-8859-1")
outer.attach(part)

Thank you! I guess I was missing the "alternative" option.
It seems to work, at least I tried it with Pine and Mozilla mail,
I will do further checking.


Michele Simionato
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top