#1
  1. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,400
    Rep Power
    1233

    Using Sendmail for attachments?


    All:

    I am currently using a python sendmail protocol to notify users that a file has been generated. (Note: I am aware of all of the sendmail security issues, but this is a closed system so it's not a concern) As opposed to just notifying the user that there is a file available would it be possible to actually send it to them.

    Code:
        def SendMail(self, toAddr, msg, attachmentPath) :
    
            fromaddr = 'localhost' # TODO: Make configurable
    
            self.SMTPServer = self.cfg.getSMTPServer()
            server = smtplib.SMTP(self.SMTPServer)
            server.sendmail(fromaddr, toAddr, msg)
            server.quit()
    Thanks in advance,
    -MBirchmeier
  2. #2
  3. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,400
    Rep Power
    1233
    For anyone interrested:

    I did find out how to attach files:

    Code:
    import smtplib, sys, getopt, os, string, mimetypes
    from email import Encoders
    from email.MIMEBase import MIMEBase
    from email.MIMEMultipart import MIMEMultipart
    
    outer = MIMEMultipart()
    outer = MIMEMultipart()
    outer['Subject'] = 'E-mail with attachment'
    outer['To'] = 'Recipient'
    outer['From'] = 'sender'
    
    outer.premamble = 'You will not see this in a MIME-aware mail reader.\n'
    outer.epilogue = ''
    msgString = "You will see this in a MIME-aware mail reader\n"
    msg = MIMEText(msgString) 
    outer.attach(msg) #attach as plain text
    
    fp = open(File)
    # Note: we should handle calculating the charset
    attach = MIMEBase('application','octet-stream')
    attach.set_payload(fp.read())
    fp.close()
    Encoders.encode_base64(attach)
    attach.add_header('Content-Disposition','attachment', filename=File) #set as an attachment
    outer.attach(attach) #attach as attachment
    		
    print("then mail " + lastFile)
    
    s = smtplib.SMTP('mail.mysmtpserver.com')
    #s.connect()
    s.sendmail(outer['From'], outer['To'], outer.as_string())
    s.close()
    information gathered from : http://python.active-venture.com/lib/node510.html

    -MBirchmeier
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0
    Originally Posted by MBirchmeier
    For anyone interrested:

    I did find out how to attach files:

    Code:
    import smtplib, sys, getopt, os, string, mimetypes
    from email import Encoders
    from email.MIMEBase import MIMEBase
    from email.MIMEMultipart import MIMEMultipart
    
    outer = MIMEMultipart()
    outer = MIMEMultipart()
    outer['Subject'] = 'E-mail with attachment'
    outer['To'] = 'Recipient'
    outer['From'] = 'sender'
    
    outer.premamble = 'You will not see this in a MIME-aware mail reader.\n'
    outer.epilogue = ''
    msgString = "You will see this in a MIME-aware mail reader\n"
    msg = MIMEText(msgString) 
    outer.attach(msg) #attach as plain text
    
    fp = open(File)
    # Note: we should handle calculating the charset
    attach = MIMEBase('application','octet-stream')
    attach.set_payload(fp.read())
    fp.close()
    Encoders.encode_base64(attach)
    attach.add_header('Content-Disposition','attachment', filename=File) #set as an attachment
    outer.attach(attach) #attach as attachment
    		
    print("then mail " + lastFile)
    
    s = smtplib.SMTP('mail.mysmtpserver.com')
    #s.connect()
    s.sendmail(outer['From'], outer['To'], outer.as_string())
    s.close()
    information gathered from : http://python.active-venture.com/lib/node510.html

    -MBirchmeier

    Hi,

    Does this code support any file types to attach? Does using MIMEBase mean that any file types can be attached?

    Thanks for the help!
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Interesting. I may find some use for that code some day. Thanks.
    Last edited by †Yegg†; June 28th, 2005 at 06:05 PM.

IMN logo majestic logo threadwatch logo seochat tools logo