Email attachments
Facundo Batista
facundo at taniquetil.com.ar
Thu Jan 25 13:09:29 EST 2007
Steve Holden wrote:
> I'm having some trouble getting attachments right for all recipients,
> and it seems like Apple's mail.app is the pickiest client at the moment.
> It doesn't handle attachments that both Thunderbird and Outlook find
> perfectly acceptable.
The following code works ok with Thunderbird and mail.app (at least with
all the mail I usually sent).
A friend of mine reports that he (with mail.app can see that he has an
attachment, open it, etc.)
-----------------------------------
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
class MailSender:
'''Envia un mail con archivos attached.'''
def __init__(self, server, remitente, to):
self.server = server
self.remitente = remitente
self.to = to
assert type(self.to)==list
def envia(self, subject, text, files=[]):
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = self.remitente
msg['To'] = COMMASPACE.join(self.to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
msg.attach(part)
smtp = smtplib.SMTP(self.server)
smtp.sendmail(self.remitente, self.to, msg.as_string())
smtp.close()
return
-----------------------------------
Regards,
--
. Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/
More information about the Python-list
mailing list