Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

475 views
Skip to first unread message

wach...@gmail.com

unread,
Aug 8, 2013, 3:05:57 PM8/8/13
to
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 = 'm...@mysite.com'
reciever = 'm...@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 <ams...@mysite.com>
To: To Samuel Kamau <m...@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"

Gary Herron

unread,
Aug 8, 2013, 3:19:43 PM8/8/13
to pytho...@python.org
On 08/08/2013 12:05 PM, wach...@gmail.com wrote:
> 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

Ian Kelly

unread,
Aug 8, 2013, 3:31:11 PM8/8/13
to Python
On Thu, Aug 8, 2013 at 1:05 PM, <wach...@gmail.com> wrote:
> 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

Joel Goldstick

unread,
Aug 8, 2013, 3:32:28 PM8/8/13
to Gary Herron, pytho...@python.org
On Thu, Aug 8, 2013 at 3:19 PM, Gary Herron
<gary....@islandtraining.com> wrote:
> On 08/08/2013 12:05 PM, wach...@gmail.com wrote:
>>
>> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list

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 ;)

--
Joel Goldstick
http://joelgoldstick.com
0 new messages