alwbtc alwbtc - 1 year ago 209
Python Question

How to send email with pdf attachment in Python?

Possible Duplicate:

How to send Email Attachments with python

I would like to edit the following code and send an email with an attachment. Attachment is a pdf file, it is under /home/myuser/sample.pdf, in linux environment. What should I change below?

import smtplib
fromaddr = ''
toaddrs = ''
msg = 'Hello'

# Credentials (if needed)
username = 'myemail'
password = 'yyyyyy'

# The actual mail send
server = smtplib.SMTP('')
server.sendmail(fromaddr, toaddrs, msg)

Answer Source

You create a message with an email package in this case -

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
msg = MIMEMultipart()

and then send the message.

import smtplib
mailer = smtplib.SMTP()
mailer.sendmail(from_, to, msg.as_string())

Several examples here -

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download