[Zope] sending attachments

Tino Wildenhain tino at wildenhain.de
Sun May 29 03:54:30 EDT 2005


Am Samstag, den 28.05.2005, 06:47 -0700 schrieb Varun Parange:
> hi,
>  
> is it possible to send mails with attachments using Zope MailHost??
> if so,...how do u achieve it?
> 

If you have a simple product like this:

EmailTools/__init__.py
------------------- contents ---------------------------------
from AccessControl import allow_module, allow_class, allow_type
from AccessControl import ModuleSecurityInfo, ClassSecurityInfo

from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.Header import Header
from MailCrypt import signmail
from email.Encoders import encode_base64

allow_class(BlockFormatter)
allow_class(MIMEBase)
allow_class(MIMEText)
allow_class(MIMEMultipart)
allow_class(Header)
allow_class(signmail)
allow_class(encode_base64)
---------------------------------------------------------------

You can send mails with attachment like this:

from Products.EmailTools import
MIMEText,MIMEBase,MIMEMultipart,Header,encode_base64

msg=MIMEMultipart()
inner=MIMEText(mailtext,_charset='iso-8859-1')
msg.attach(inner)
msg.add_header('Subject',str(Header(subject,"iso-8859-1")))

# now the attachments:
for obj in context.attachments.objectValues('File'):
    mt,st=obj.content_type.split("/")
    p=MIMEBase(mt,st)
    p.set_payload(str(obj))
    p.add_header('content-disposition', 
                 'attachment', 
                 filename=obj.getId())
    encode_base64(p)
    msg.attach(p)

# now you are ready to send the mail like this:

context.YourMailHost.send(msg.as_string(),
                          mfrom="Foobar <foo at bar.com>",
                          mto=["Someone <someone at whereever.com>"])




More information about the Zope mailing list