[Zope] Send an email with attachment (from a python script)

Gus Gollings gus at commonground.com.au
Tue Aug 26 12:42:00 EDT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Tuesday, August 26, 2003, at 01:45 AM, Jean Baltus wrote:
> How can I send an email with an attachment from a python script?
> Right now, we are using a script that looks as follows:

You need to add two new headers (MIME-Version and Content-Type) and you 
will need to encode the attached file as base-64 to include it into the 
body of your email. Notice the boundary string in the Content-Type 
header (it can be anything you want), and how it is prefixed with "--" 
when referenced in the body of the email, and how the last reference to 
the boundary (after the attachment) also has a suffix of '--' as well 
as the prefix. See an example below:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From: mrstrong at mrmen.com
Subject: Zope
To: basil at faulty.com
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="_===705645===_"

This is a multi-part MIME message

- --_===705645===_
Content-Type: text/plain; charset="ISO-8859-1"; format="flowed"
Content-Transfer-Encoding: 8bit

The Body Email Message

- --_===705645===_
Content-Type: text/html
Content-Disposition: attachment; filename="blah.html"
Content-Transfer-Encoding: base64

PGJvZHk+CjxkdG1sLXZhciBwcm9qZWN0X2h0bWxfaGVhZGVyPgoKCjxoMz4KQ1JFQVRPUi1U
...several lines deleted...
Pgo8L2Rpdj4KCjxkdG1sLXZhciBwcm9qZWN0X2h0bWxfZm9vdGVyPgo8L2JvZHk+

- --_===705645===_--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hope this helps, of course you will need to add in the TAL statements 
at the appropriate points (for boundary, filename, Content-Type above). 
And I'll leave you to figure out how to encode your file as base-64 ;-)

Regards,

Gus

- -
http://commongroundgroup.com
+613 9398 8000


> mailhost=context.MailHost
>
> mailApproval = context.mail_template(context, email=email)
>
> mailhost.send(mailApproval)
>
>  
>
> where “mail_template” is a page template that looks as follows:
>
>  
>
> To: <span tal:replace="options/email" />
>
> From: "<span tal:replace="here/email_from_address" />"
>
> Subject: A Subject
>
> Content-Type: text/plain; charset=<span 
> tal:replace="here/portal_properties/site_properties/default_charset" > />
>
>  
>
> BODY BODY BODY BODY BODY BODY BODY BODY BODY BODY BODY BODY BODY
>
>  
>
> Anybody has an idea to add an attachment to this?
>
>  
>
> Regards,
>
>  
>
> -- Jean Baltus
>
> _______________________________________________
> Zope maillist  -  Zope at zope.org
> http://mail.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://mail.zope.org/mailman/listinfo/zope-announce
>  http://mail.zope.org/mailman/listinfo/zope-dev )
>


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (Darwin)

iD8DBQE/SrrsFpx6ZDMgHYARAlwCAKCdOHk+nrCGi3UtUdBEUmozCyb0RACfXT0r
DiMKLIsbSuwdfk1xAYt9YEI=
=5hYq
-----END PGP SIGNATURE-----




More information about the Zope mailing list