Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52218 > unrolled thread

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

Started bywachkama@gmail.com
First post2013-08-08 12:05 -0700
Last post2013-08-08 15:32 -0400
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  How Do I get my Python script to attach multiple files and send as a single email wachkama@gmail.com - 2013-08-08 12:05 -0700
    Re: How Do I get my Python script to attach multiple files and send as a single email Gary Herron <gary.herron@islandtraining.com> - 2013-08-08 12:19 -0700
    Re: How Do I get my Python script to attach multiple files and send as a single email Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-08 13:31 -0600
    Re: How Do I get my Python script to attach multiple files and send as a single email Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-08 15:32 -0400

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

Fromwachkama@gmail.com
Date2013-08-08 12:05 -0700
SubjectHow Do I get my Python script to attach multiple files and send as a single email
Message-ID<28e2b5ac-6dbf-4f90-a0f0-fc5cc33fc2ef@googlegroups.com>
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@mysite.com'
reciever = 'me@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@mysite.com>
To: To Samuel Kamau <me@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"            
                

[toc] | [next] | [standalone]


#52220

FromGary Herron <gary.herron@islandtraining.com>
Date2013-08-08 12:19 -0700
Message-ID<mailman.366.1375990131.1251.python-list@python.org>
In reply to#52218
On 08/08/2013 12:05 PM, wachkama@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

[toc] | [prev] | [next] | [standalone]


#52223

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-08-08 13:31 -0600
Message-ID<mailman.368.1375990315.1251.python-list@python.org>
In reply to#52218
On Thu, Aug 8, 2013 at 1:05 PM,  <wachkama@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

[toc] | [prev] | [next] | [standalone]


#52224

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-08-08 15:32 -0400
Message-ID<mailman.369.1375990667.1251.python-list@python.org>
In reply to#52218
On Thu, Aug 8, 2013 at 3:19 PM, Gary Herron
<gary.herron@islandtraining.com> wrote:
> On 08/08/2013 12:05 PM, wachkama@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web