Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101272 > unrolled thread
| Started by | kevind0718@gmail.com |
|---|---|
| First post | 2016-01-05 07:27 -0800 |
| Last post | 2016-01-31 22:37 +0100 |
| Articles | 4 — 2 participants |
Back to article view | Back to comp.lang.python
create Email with multiple HTML blocks embedded kevind0718@gmail.com - 2016-01-05 07:27 -0800
Re: create Email with multiple HTML blocks embedded Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-06 00:58 +0100
Re: create Email with multiple HTML blocks embedded Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-31 22:34 +0100
Re: create Email with multiple HTML blocks embedded Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-31 22:37 +0100
| From | kevind0718@gmail.com |
|---|---|
| Date | 2016-01-05 07:27 -0800 |
| Subject | create Email with multiple HTML blocks embedded |
| Message-ID | <0f51af4b-dfef-4d27-92b7-be601677bf50@googlegroups.com> |
The below script will send an email with one HTML file embedded and the second attached. Not really what I need. I need a Python script to create an email that contains multiple blocks of HTML in the body of the email.
There is a process that will create at least one HTML file but very often two. A summary and an exception web pages. These need to be emailed to a user group.
The code below displays one file in the email, but the other shows up as an attachment. If I reverse the body.attach statements then the files switch. I need both to appear in the body of the email.
I messed around with the boundary attribute, but could not get that to work.
Your kind assistance is requested.
Best Regards
KD
import smtplib
from pprint import pprint
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg['Subject'] = 'email from Python with HTML content '
msg['From'] = 'kduf@xxx.com'
msg['To'] = 'kduf@xxx.com'
text = "\nBelow I hope will be the Summary Table in HTML\n\n"
body = MIMEMultipart('multipart')
with open("H:\\dev\\testHTML\\Exceptions_SheetDec30d.htm", "r") as fE:
htmlE = fE.read().replace('\n', '')
with open("H:\\dev\\testHTML\\Summary_SheetDec30d.htm", "r") as f:
html = f.read().replace('\n', '')
body.attach(MIMEText(html, 'html'))
body.attach(MIMEText(htmlE, 'html'))
msg.attach(body)
s = smtplib.SMTP('smtp.aigfpc.com') # ('localhost')
s.sendmail('kduf@xxx.com', ['kduf@xxx.com'], msg.as_string())
s.quit()
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-01-06 00:58 +0100 |
| Message-ID | <1943475.7F1emcm5UF@PointedEars.de> |
| In reply to | #101272 |
kevind0718@gmail.com wrote:
^^^^^^^^^^^^^^^^^^^^
Please either do not use Google Groups and configure your newsreader
accordingly (recommended), or use Google Groups to subscribe to the
newsgroup so that you can specify your real name.
> body = MIMEMultipart('multipart')
Obviously there is redundancy, so common sense should tell you already that
this cannot be correct. The manual says:
<https://docs.python.org/3/library/email.mime.html#email.mime.multipart.MIMEMultipart>
| class email.mime.multipart.MIMEMultipart(_subtype='mixed', boundary=None,
| _subparts=None, **_params)
|
| Module: email.mime.multipart
|
| A subclass of MIMEBase, this is an intermediate base class for MIME
| messages that are multipart. Optional _subtype defaults to mixed, but can
| be used to specify the subtype of the message. A Content-Type header of
| multipart/_subtype will be added to the message object.
So this would add a “Content-Type” header (field) with the value
“multipart/multipart” to the constructed *message object*, referred to by
*body* (see below). It has to be “multipart/mixed” in your case instead,
which is the default. So you need to write *only*
msg = MIMEMultipart()
which you did. But:
> with open("H:\\dev\\testHTML\\Exceptions_SheetDec30d.htm", "r") as fE:
> htmlE = fE.read().replace('\n', '')
>
>
> with open("H:\\dev\\testHTML\\Summary_SheetDec30d.htm", "r") as f:
> html = f.read().replace('\n', '')
>
> body.attach(MIMEText(html, 'html'))
> body.attach(MIMEText(htmlE, 'html'))
>
> msg.attach(body)
Here you are attaching to a multi-part message a *multi-part message with
two parts*:
msg (multipart/mixed)
'- body (multipart/multipart)
:- html (text/html)
'- htmlE (text/html)
You want instead:
msg (multipart/mixed)
:- html (text/html)
'- htmlE (text/html)
In code:
msg.attach(MIMEText(html, 'html'))
msg.attach(MIMEText(htmlE, 'html'))
So just skip ”body”. (Where else but to the body of the message would you
attach parts? To the header? ;-))
You can see how it works *before* you sent the e-mail if you call
print(msg)
(Of course, you can also read the e-mail source after it was delivered.)
It is unnecessary/wrong to remove the '\n's from the HTML before transfer
encoding by MIMEText().
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-01-31 22:34 +0100 |
| Message-ID | <4066913.y0Q5PJoZZo@PointedEars.de> |
| In reply to | #101272 |
kevind0718@gmail.com wrote: > Following your suggestions, I now have code that looks like the below. > I get the same result. First HTML is displayed in body of email and > second shows up as an attachment. Works as designed. The message structure is correct now, but there is no plain-text part. So a MUA that is capable and configured to display HTML e-mails has no choice but to assume that you wanted the first part to be displayed directly as it is not marked as an attachment. Consequently, it *must* consider and display the second HTML part as an attachment. It might help if you inserted a plain-text part before the HTML part, and you should have such (so that people who are not keen to see HTML messages see anything in that message), but you should also mark the parts as *attachments* that you want to be considered that by the MUA. See RFC 5233 and RTFM for details. > A dump of the email message is also below. > > Maybe this would help. It does (although you could have trimmed the parts in the middle), but next time you want to post a follow-up to *my* message (or that of whoever posted a follow-up to yours), not to your own. This newsgroup/mailing list is a discussion medium that uses *threads*. As you have not observed that, I have seen your posting only now as I checked the newsgroup manually, while otherwise I would have been notified of it when I started my newsreader and automatically checked all newsgroups for new postings. Since you are using Google Groups, see <http://twovoyagers.com/improve-usenet.org/> pp. for the details. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-01-31 22:37 +0100 |
| Message-ID | <2214839.MMRsA2lHqX@PointedEars.de> |
| In reply to | #102368 |
Thomas 'PointedEars' Lahn wrote: > See RFC 5233 and RTFM for details. RFC _5322_ (“Internet Message Format”) <http://tools.ietf.org/html/rfc5322> -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web