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

W

wachkama

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 = '(e-mail address removed)'
reciever = '(e-mail address removed)'

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 <[email protected]>
To: To Samuel Kamau <[email protected]>
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"
 
G

Gary Herron

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.

There is a standard Python module named "email" which you should look
at. It can build an email with all the parts, alternates, and
attachments you want. Then you send the resulting message using your
smtplib code. The email module is large and complex, but reasonably
easy to learn (following the documentation examples). It's far, FAR,
easier than rolling your message, especially when attachments are needed.

Gary Herron
 
I

Ian Kelly

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.

You just need to repeat part3 for each attachment. Also the content
type in part3 should be the content type of the attachment, not
multipart/mixed.

You might also want to take a look at the email package in the
standard library which will do a lot of this stuff for you.

http://docs.python.org/3/library/email.html
 
J

Joel Goldstick

There is a standard Python module named "email" which you should look at.
It can build an email with all the parts, alternates, and attachments you
want. Then you send the resulting message using your smtplib code. The
email module is large and complex, but reasonably easy to learn (following
the documentation examples). It's far, FAR, easier than rolling your
message, especially when attachments are needed.

Gary Herron

Here is a good example:

http://stackoverflow.com/questions/3362600/how-to-send-email-attachments-with-python

I found it using the following google search: "python send email using
smtp with multiple attachment"

It was the second result.

Google is your friend ;)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top