954,168 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

send email in python

i have a python script and would like to know how to email the output of the script in python.
i can do it from the command line(kshell) with in *nix but would like something like this in the script.
somescript.py; echo "test" | mailx [email]john_smith@xyz.com[/email]

axn
Light Poster
39 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

A very crude method would be to capture your output, and then execute the same command via os.system or a similar call.

Alternately you could make use of smtplib to send the email to a mail server's listener, and send it that way.
Example of using smtplib .
Keep in mind that in this example, the mail listening agent is on the same machine as the script is running. If this is not the case you'll need the ip address and port that the mail daemon is listening on of the server where this service is running.

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

Something like the following is a simple example and usually works. If not, then you will have to Google for your specific sendmail app.

fromaddr = "<me@localhost>"                                            
toaddrs  = "<you@localhost>"
msg = "Test of Python's sendmail\n"
                                                                                
# The actual mail send part                                
server = smtplib.SMTP('localhost')                                       
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
print server.verify( toaddrs )
server.quit()
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You