How Do I get my Python script to attach multiple files and send as a single email

wachkama at gmail.com wachkama at gmail.com
Thu Aug 8 15:05:57 EDT 2013


I have a dilemma I cant figure out how to send multiple files as an attachment to my email using this script. I can only send a single file attachment . Help!!!  Here is my script.
All filename's are txt files.


fo = with open(filename,'rb')
fo1 = open(filename2,'rb')
fo2= open(filename3, 'rb')
fo3= open(filename4,'rb')
fo4=open(filename5, "rb")
filecontent = fo.read()
filecontent1 = fo1.read()
filecontent2 = fo2.read()
filecontent3 = fo3.read()
filecontent4= fo4.read()
encodedcontent = base64.b64encode(filecontent)  # base64
encodedcontent1 = base64.b64encode(filecontent1)  # base64
encodedcontent2 = base64.b64encode(filecontent2)  # base64
encodedcontent3 = base64.b64encode(filecontent3)  # base64
encodedcontent4 = base64.b64encode(filecontent4)  # base64

sender = 'me at mysite.com'
reciever = 'me at mymail.com'

marker = "AUNIQUEMARKER"

body ="""
Hi 
This is Sam, I have a some files containing some tickets for you.
Good day 
:)
"""
# Define the main headers.
part1 = """From:  Master Tickets <amster at mysite.com>
To: To Samuel Kamau <me at myemail.com>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)

# Define the message action
part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit

%s
--%s
""" % (body,marker)

# Define the attachment section
part3 = """Content-Type: multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=%s

%s
--%s--
""" %(filename, filename, encodedcontent, marker)
message = part1 + part2 + part3


try:
   smtpObj = smtplib.SMTP('mx.usa.net')
   smtpObj.sendmail(sender, reciever, message,)
   print "Successfully sent email"
except Exception:
   print "Error: unable to send email"            
                



More information about the Python-list mailing list