attach a pdf file to an email

N

nonamehkg

Hi, I am writing a simple program that sends emails with attachments.

I could use "MIMEImage" for .png and then attach it to an
"MIMEMultipart" object. But I couldn't or don't know how to attachment
a .pdf or something else to my emails.

I'm new to python, could somebody help me about that?


Thank You,


kychan
 
P

Pelmen

here is my code for Excel
outer = MIMEMultipart()
outer['Subject'] = header.decode('cp1251').encode('koi8-r')
outer['To'] = baseParam['mailto']
outer['From'] = baseParam['mailfrom']
outer.preamble = ''
# To guarantee the message ends with a newline
outer.epilogue = ''

outer.attach(MIMEText('Weekly report', 'plain', 'koi8-r'))

fp = open(reportFileName, 'rb')
msg = MIMEBase('application', 'octet-stream')
msg.set_payload(fp.read())
fp.close()
# Encode the payload using Base64
Encoders.encode_base64(msg)
# Set the filename parameter
msg.add_header('Content-Disposition', 'attachment',
filename=header.decode('cp1251').encode('koi8-r') + '.xls')
outer.attach(msg)

server = smtplib.SMTP(baseParam['server'], baseParam['port'],
'localhost')
#server.set_debuglevel(1)
server.sendmail(baseParam['mailfrom'], baseParam['mailto'],
outer.as_string())
server.quit()
 
L

Larry Bates

Hi, I am writing a simple program that sends emails with attachments.

I could use "MIMEImage" for .png and then attach it to an
"MIMEMultipart" object. But I couldn't or don't know how to attachment
a .pdf or something else to my emails.

I'm new to python, could somebody help me about that?


Thank You,


kychan

Here is a great helper class that I have used for several
years. I didn't write it, but it has worked fine. Even if
you want to write your own, it should help as a guide.

http://motion.sourceforge.net/related/send_jpg.py

Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top