#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9

    Attach a file to mail...


    Well, guess i need help from more experienced.
    Im trying to get my program to email my backups.
    If my backup is named: backup.tar.gz
    What do I need to change in this code to attach the file?

    EDITED: Changed the "from" word thing... thanks.

    Code:
    import smtplib,string,time
    to = "anymail@yes.com"
    frm = "sender@yes.cl"
    subject = "Some subject"
    msg = "Some msg"
    body = string.join(("From: %s" % frm,"To: %s" % to,"Subject: %s" % subject,"",msg), "\r\n")
    host = "localhost"
    server = smtplib.SMTP(host)
    server.set_debuglevel(0)
    server.sendmail(frm, [to], body)
    server.quit()
    Note: This code does send an email...
    I have searched in google, but the code I've seen it is not clear, or understandable for my knowledge level... I'm very noob at python.
    Thanks in advance.
  2. #2
  3. No Profile Picture
    10 legit 10 quit
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    114
    Rep Power
    11
    an easy way to mail attachments with a shell script is:

    uuencode filname.tar.gz | mail -s "here's the backups" user@email.com

    i'm not sure how to do it in python though
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    First of all, you were using from as a variable name. You can never do that. From is a Python keyword. Here is the modified code:
    Code:
    import smtplib, string, time
    to = "backupmail@yes.com"
    frm = "sender@yes.cl"
    subject = "Backup done:" + time.strftime('%d_%B_%Y')
    msg = "Backup done."
    body = string.join(("From: %s" % frm,"To: %s" % to,"Subject: %s" % subject,"",msg), "\r\n")
    host = "localhost"
    server = smtplib.SMTP(host)
    server.set_debuglevel(0)
    server.sendmail(frm, [to], body)
    server.quit()
    I ran it and I got a connection refused, so I'm assuming the code should work. But I don;t have the time to finish testing it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9

    Thumbs up


    ahh yes... thats because I translated the variables names to english... I wasnt really using "from" but "desde" (spanish)
    Well, It seems that sending mail with attachments is fairly complicated for what i've read? Not an advanced topic, but surely not newbie friendly?
    No one knows a basic tutorial about this?
    thanks,
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    My best guess would be searching for topics related to this through a search engine.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You need to create a multi-part MIME message. There is a module in the python libraries called 'email' that will let you do that - read the docs for it, including the example code.

    There are also some older mime modules in the standard library, but these are deprecated in favour of 'email'.

    Dave
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9
    Ok, I'll check on that module... and thanks on the multipart MIME message... thats the tip I was looking for. Thanks both.
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    ASPN.activestate.com may help with MIME. Go to that state, go to Python, select the Python Cookbook, and use the search under 'this section' to search for MIME.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9
    Ok, thanks for the tip. I'll check it right know...
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9
    Ok, here is a script for making a backup and sending the file to some mail (It's tested on Linux)
    Code:
    #!/usr/bin/python
    import os,time,locale,smtplib,string
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email import Encoders
    locale.setlocale(locale.LC_ALL, '')
    ##--------------------------------------------------------
    
    # Backup first
    tarjet_dir = '/the/directory/to/backup/'
    final_dir = '/the/directory/to/store/the/backup'
    final_file = final_dir + 'backup_' + time.strftime('%d_%B_%Y') + '.tar.gz'
    filename = 'backup_' + time.strftime('%d_%B_%Y') + '.tar.gz'
    command = "tar cvpzf %s %s" % (final_file,tarjet_dir)
    
    if os.system(command) == 0:
    	print '\nBackup OK'
    else:
    	print '\nBackup Failed'
    
    # Mail then
    outer = MIMEMultipart()
    outer['Subject'] = 'Backup done:' + time.strftime('%d_%B_%Y')
    outer['To'] = 'your@mail.com'
    outer['From'] = 'from@someone.com'
    outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
    outer.epilogue = ''
    
    ctype = 'application/octet-stream'
    maintype, subtype = ctype.split('/', 1)
    
    fp = open(final_file, 'rb')
    msg = MIMEBase(maintype, subtype)
    msg.set_payload(fp.read())
    fp.close()
    Encoders.encode_base64(msg)
    msg.add_header('Content-Disposition', 'attachment', filename=filename)
    outer.attach(msg)
    
    host = "localhost"
    server = smtplib.SMTP(host)
    server.set_debuglevel(0)
    server.sendmail('from@someone.com', ['your@mail.com'], outer.as_string())
    server.quit()
    Hope it is useful for someone...

IMN logo majestic logo threadwatch logo seochat tools logo