Thanks to help of Rudy Shockaert I was able to solve my problem and I
also added the capability to add the email body if you are curious and
don't know how. I also commented the code as well. Here is the code
result:
import smtplib
import mimetypes
from email.Encoders import encode_base64
from email.MIMEAudio import MIMEAudio
from email.MIMEBase import MIMEBase
from email.MIMEImage import MIMEImage
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
#_________________________________________________ __________________________________________________ ___
#Requirements:At least Python 2.2.2
#getAttachment takes the directory path and the filename for ex:
'c:\\MyFolder\\test.txt'
#You must put double back slashes in your path.
#getAttachment returns email version of the type of file it takes in,
#For example if it is a text file, it will encode it as a text file, a
gif, it will encode it as a gif
#_________________________________________________ __________________________________________________ ____
def getAttachment(path, filename):
ctype, encoding = mimetypes.guess_type(path) #mimetypes guesses
the type of file and stores it in ctype
if ctype is None or encoding is not None: # example: ctype
can equal "application/pdf"
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1) #We do a split on "/"
to store "application" in maintype and "pdf" in subtype
fp = open(path, 'rb') #open the file
if maintype == 'text':
attach = MIMEText(fp.read(),_subtype=subtype) #check for
maintype value and encode and return according to
elif maintype == 'message': #the type of
file.
attach = email.message_from_file(fp)
elif maintype == 'image':
attach = MIMEImage(fp.read(),_subtype=subtype)
elif maintype == 'audio':
attach = MIMEAudio(fp.read(),_subtype=subtype)
else:
print maintype, subtype #if it does not equal any of the
above we print to screen and encode and return
attach = MIMEBase(maintype, subtype) #the encoded value
attach.set_payload(fp.read())
encode_base64(attach)
fp.close
attach.add_header('Content-Disposition', 'attachment',
filename=filename)
return attach
#_________________________________________________ ______________________________________________
#Here we set up our email
From = 'm*****@hotmail.com'
To = 'y*******@hotmail.com'
msg = MIMEMultipart()
msg['From'] = From
msg['To'] = To
msg['Subject'] = 'here is your attachment'
body = MIMEText('This is the body of the email!') #Here is the body
path = 'C:\\YourPath\\YourFile.txt'
filename = 'gfe.pdf'
attach = getAttachment(path, filename) #We call our getAttachment()
function here
msg.attach(attach) #We create our message both attachment and the
body
msg.attach(body)
server = smtplib.SMTP('mySMTPServerName.com')
server.sendmail(From, To, msg.as_string()) #Send away
server.quit()
bb*******@hotmail.com (Bill) wrote in message news:<4f*************************@posting.google.c om>...
I am trying to have the capability to email attachments. Specifically
I want to be able to email a specific attachment that I name that may
be a PDF document, text doc, etc. I already have a working piece of
code that emails jpg attachments but does not work with any other
types of attachments. Could someone tell me how to modify this code to
send other types of attachments like the one's stated above(especially
PDF's)? Also how do I determine the name of the attachment? Right now
it defaults to Attach0. Here is the current code I have below:
_________________________________________________
import sys, smtplib, MimeWriter, base64, StringIO
message = StringIO.StringIO()
writer = MimeWriter.MimeWriter(message)
writer.addheader('Subject', 'The Text test')
writer.startmultipartbody('mixed')
# start off with a text/plain part
part = writer.nextpart()
body = part.startbody('text/plain')
body.write('This is a picture of chess, enjoy :)')
# now add an image part
part = writer.nextpart()
part.addheader('Content-Transfer-Encoding', 'base64')
body = part.startbody('image/jpeg')
#body = part.startbody('text/plain')
base64.encode(open('c:\email\chess01.jpg', 'rb'), body)
# finish off
writer.lastpart()
# send the mail
smtp = smtplib.SMTP('fc.hbu.edu')
smtp.sendmail('b********@hbu.edu', 'b********@hbu.edu',
message.getvalue())
smtp.quit()
__________________________________________________ ________________________