Send an email with attachment (from a python script)
Hi all, How can I send an email with an attachment from a python script? Right now, we are using a script that looks as follows: 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
-----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@mrmen.com Subject: Zope To: basil@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@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-----
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
there's a MimeWriter module to handle this! http://www.python.org/doc/current/lib/module-MimeWriter.html send an email with an attachment to yourself, look over it's source and use MimeWriter -- bromden[at]gazeta.pl
participants (3)
-
bromden -
Gus Gollings -
Jean Baltus