Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using the following code to send email from unix.

Code

#!/usr/bin/python
import os
def sendMail():
    sendmail_location = "/usr/sbin/sendmail" # sendmail location
    p = os.popen("%s -t" % sendmail_location, "w")
    p.write("From: %s\n" % "[email protected]")
    p.write("To: %s\n" % "[email protected]")
    p.write("Subject: My Subject \n")
    p.write("\n") # blank line separating headers from body
    p.write("body of the mail")
    status = p.close()
    if status != 0:
           print "Mail Sent Successfully", status
    sendMail()

I am not sure how to add attachment to this email (attachment being on a different directory /my/new/dir/)

share|improve this question

3 Answers 3

Use the email.mime package to create your mail instead of trying to generate it manually, it will save you a lot of trouble.

For example, sending a text message with an attachment could be as simple as:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication

msg = MIMEMultipart()
msg['From'] = 'fromaddress'
msg['To'] = 'toaddres'
msg['Subject'] = 'subject'
msg.attach(MIMEText('your text message'))
with open(filename, 'rb') as f:
    attachment = MIMEApplication(f.read(), 'subtype')
    attachment['Content-Disposition'] = 'attachment; filename="%s";' % filename
    msg.attach(attachment)
message = msg.as_string()

Then you can write the message to sendmail, or use smtplib to send it.

'subtype' should either be replaced with the mime subtype of the attached document, or left out to send the attachment with the default type of application/octet-stream. Or if you know your file is text, you can use MIMEText instead of MIMEApplication.

share|improve this answer
    
sends the message in unreadable format --===============0631434354== Content-Type: application/subtype MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: attachment: filename=name_of_file; TG9nZ2luZyBTdGFydHMKKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioq KioqKioqKioqKioqCioqICAgICAgICAgICAgIE9kYkF1dG9wdXJnZSBGaWxlIEZvdW5kICAgICAg –  misguided Jul 9 '13 at 23:06
    
That's correct, it's the attached file, bas64 encoded, the email package does that automatically for you. any email client will be able to read that. try viewing the source of any mail with an attachment, you'll see something similar... If you want to know more, read about MIME here –  mata Jul 10 '13 at 9:45
    
For the correct filename I had to change 'attachment: filename=%s;' to 'attachment; filename=%s;' (with a ";" instead of a ":") –  Jakob Sep 27 '13 at 9:43
    
@Jakob - yes, you're right, should be a semicolon. corrected, thx. –  mata Sep 27 '13 at 9:56

Sendmail is an extremely simplistic program. It knows how to send a blob of text over smtp. If you want to have attachments, you're going to have to do the work of converting them into a blob of text and using (in your example) p.write() to add them into the message.

That's hard - but you can use the email module (part of python core) to do a lot of the work for you.

Even better, you can use smtplib (also part of core) to handle sending the mail.

Check out http://docs.python.org/2/library/email-examples.html#email-examples for a worked example showing how to send a mail with attachments using email and smtplib

share|improve this answer

I normally use the following to send a file "file_name.dat" as attachment:

uuencode file_name.dat file_name.dat | mail -s "Subject line" [email protected]
share|improve this answer
    
can this be represented in a pythonic way ? –  misguided Jul 10 '13 at 11:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.