SMTPlib Emailing Attachments

Bill bblancett at hotmail.com
Thu Sep 18 09:12:00 EDT 2003


Thanks for your response Rudy I think you put me on the right track.
But when I ran your script I still got the following errors:
______________Errors_________________________________________________________
Traceback (most recent call last):
  File "C:\Program Files\Python22\ImgAndText.py", line 41, in ?
    attach = getAttachment(path, filename)
  File "C:\Program Files\Python22\ImgAndText.py", line 15, in
getAttachment
    fp = open(path, 'rb')
IOError: [Errno 2] No such file or directory: 'C:\\Email\test.txt'
_____________________________________________________________________________

My question to you is what version of Python are you using (I am using
2.2.2.)?
Are you using the Windows version (I am using Windows)? If you are
using the Windows version where do you have your Python folder located
(Mine is c:\Program Files\Python22\) ? These are my system parameters.
And here is the code that I used. It is exactly like your code but I
changed the TO,FROM,Subect, path (full pathname including filename)
and also the SMTP server like you suggested. Here it is posted below:
______________Code Snippet________________________________________________
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

def getAttachment(path, filename):
     ctype, encoding = mimetypes.guess_type(path)
     if ctype is None or encoding is not None:
         ctype = 'application/octet-stream'
     maintype, subtype = ctype.split('/', 1)
     fp = open(path, 'rb')
     if maintype == 'text':
         attach = MIMEText(fp.read(),_subtype=subtype)
     elif maintype == 'message':
         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
         attach = MIMEBase(maintype, subtype)
         attach.set_payload(fp.read())
         encode_base64(attach)
     fp.close
     attach.add_header('Content-Disposition', 'attachment',
filename=filename)
     return attach


msg = MIMEMultipart()
msg['From'] = 'bblancett at hbu.edu'
msg['To'] = 'bblancett at hbu.edu'
msg['Subject'] = 'here is your attachment'
path = 'C:\Email\test.txt'
filename = 'test.txt'
attach = getAttachment(path, filename)
msg.attach(attach)

server = smtplib.SMTP('fc.hbu.edu')
server.sendmail('bblancett at hbu.edu', 'bblancett at hbu.edu',
msg.as_string())
server.quit()
___________________________________________________________________________


Rudy Schockaert <rudy.schockaert at pandora.be> wrote in message news:<KI1ab.25228$dy5.1507439 at phobos.telenet-ops.be>...
> Bill wrote:
> > 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('bblancett at hbu.edu', 'bblancett at hbu.edu',
> > message.getvalue())
> > smtp.quit()
> > __________________________________________________________________________
> 
> Wouldn't it be a lot easier to use the email module?
> 
> This is a snippet of how I send attachments (simply replace 
> TO,FROM,Subect, path (full pathname including filename) and filename 
> with relevant data):
> 
> 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
> 
> def getAttachment(path, filename):
>      ctype, encoding = mimetypes.guess_type(path)
>      if ctype is None or encoding is not None:
>          ctype = 'application/octet-stream'
>      maintype, subtype = ctype.split('/', 1)
>      fp = open(path, 'rb')
>      if maintype == 'text':
>          attach = MIMEText(fp.read(),_subtype=subtype)
>      elif maintype == 'message':
>          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
>          attach = MIMEBase(maintype, subtype)
>          attach.set_payload(fp.read())
>          encode_base64(attach)
>      fp.close
>      attach.add_header('Content-Disposition', 'attachment',
> 			filename=filename)
>      return attach
> 
> 
> msg = MIMEMultipart()
> msg['From'] = FROM
> msg['To'] = TO
> msg['Subject'] = SUBJECT
> attach = getAttachment(path, filename)
> msg.attach(attach)
> 
> server = smtplib.SMTP(MAILSERVER)
> server.sendmail(FROM, [TO], msg.as_string())
> server.quit()




More information about the Python-list mailing list