simple example of mimelib? and embedding (not attaching) images in email sent with python.

Robert Amesz reqhye72zux at mailexpire.com
Mon Aug 20 10:44:55 EDT 2001


Stephen wrote:

> I've looked through the archives regarding using smtplib and the 
> numerous MIME related modules for sending email messages with
> attachments.  I've found various solutions, some of which no longer
> seem to work, and was quite surprised how this seemingly banal task
> has caused so much trouble. However, in Microsoft Outlook, it's
> possible to embed image files into the actual message as opposed to
> having them attached.  This way, they display when the recipient
> views the message.  Just how does one compose a message like that
> with Python may I ask?

With some difficulty, and I know because I've tried. 


Basic structure of a HTML message:

[standard mailheaders]
[mime section 1: multipart/related]

Text for none-MIME aware mail readers.
(This precedes the first mime part.)

[mime section 1 part 1]
   [mime section 2: multipart/alternative]
   [mime section 2 part 1: text/plain]
       Text version of message
   [mime section 2 part 2: text/html]
       HTML version of message
   [end mime section 2]

[mime section 1 part 2]
   First encoded file (an image or whatever)

[mime section 1 part 3]
    Second embedded file

etc.

(If there are no embedded files, the outer multipart/related mime 
'wrapper' can be omitted.)


Is that it? Unfortunately not, the tricky part is that a content-ID 
(basically just a GUID) must be generated for each embedded file, and 
that all references to those files *within* the HTML-part must be 
replaced by the content-ID.

What I ended up doing is using the SGML-parser module to scan the HTML-
source for files (as yet only in <IMG ...> tags) and then generate a 
content-ID for each of those.

If there are files which need to be embedded, the HTML is scanned again 
[*], and the file references (in <IMG ...> tags) are replaced by the 
content ID. As the HTML as been 'disassembled' at this point, it is a 
good opportunity to reassemble it without the flotsam and jetsam of 
comments and spurious whitespace. (So what if that makes garbage of 
your <PRE> tags? I'll fix it ... someday.)


> Also if I read between the lines of some posts in the archive, is
> it safe to assume that mimelib is the way to go for future MIME
> manipulation? If so, would it be asking too much for a simple
> example snippet showing how it is used becaues I can't work out
> where to start with it.

Simple... ha! What I have are two sourcefiles, totalling a little over 
10Kb. In Python terms, that's quite a bit of code. And, although it 
works, the code is still unfinished in many ways, so by the time it 
does everything what I expect it to do, it'll probably be a few Kb 
larger.


> In fact I'm off to download Mailman in order to read the source and
> see how it uses mimelib.  This is overkill on a par with taking
> apart your Range Rover in order to learn how carburetors work.  

Hmmm, Range Rovers, don't those have diesel engines? And don't most 
diesel engines use direct injection? I think you're on a wild goose 
chase here, mate. ;-)


Robert Amesz
-- 
[*] I know, I know: I should do only a single scan, combining the two 
functions. I'll get around to it - eventually.



More information about the Python-list mailing list