Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How can I send email from a shellscript (usually cronjob) without running a mailserver on the same host. Using a smtp server. Running Ubuntu.

I looked a t various tutorials, but couldnt find a suitable approach (simple and secure).



share|improve this question

2 Answers 2

If it's ubuntu then you have python and can use it's smtplib module (means no MTA on same host). There is a little sample posted below to get you started (you might want to put the username/pass/config in an ini file, do some error checking and such, but the ''starttls'' line encrypts the rest of the smtp session if the server supports it.). That gives you simple and secure. It's possible to add attachments and so on with a little extra work.

You would call it like: "This is my message".


import smtplib
import sys

message = sys.argv[1]

server = smtplib.SMTP('', 587)
server.login('', 'password')
server.sendmail('', '', message)

You can call '''' from your cron job or shellscripts.

share|improve this answer
I go for the groovy version (…) – javadude May 20 '10 at 0:41

You could install postfix or something else in relay-only mode and then use either mail(x) or mutt to send mail. Both can send mail from the command line.

A good option on Ubuntu might be nullmailer as MTA, as it is designed for relay-only operation.

share|improve this answer
ssmtp is another option that, as far as I can tell, does pretty much the same thing as nullmailer. I'd recommend something in that vein. – David Z May 17 '10 at 9:24

Your Answer


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.