Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to be able to send email from a bash script. I currently have a server running postfix that I use from my php server to send emails. I now want to be able to run a bash script from a third server and use the mail server to send the email as I don't want to have to have multiple servers that can send email.

Looking at this question, it appears I need to either install Python (this is a bare bones linux server without much installed) or use postfix in relay-only mode but I'm not sure what that means. Do I have to install postfix on this server and use it as a passthrough to my mail server? Are there other options for sending email to an external server from bash?

share|improve this question

The ssmtp tool does what you want:

It's installable from APT on Debian/Ubuntu.

share|improve this answer

Well, if the emails you want to send are fairly basic, you could also just talk to your email server using basic SMTP commands and netcat.

The following bash script works just fine, it's pretty raw though...

#! /bin/bash

my_message="hello world, how's the weather today ?"

nc $mail_srv_ip $mail_srv_port << EOF
ehlo mail.script
mail from:<bashscript@local>
rcpt to:<$recipient>
subject: bash script email output

If the goal is to send the output of a bash script, it will work. $message can contain anything you want, smtp expects the end of the 'data' section to be marked by a line starting with a dot.

share|improve this answer
I'm not sure why this has been so heavily marked down. Yes, it's a filthy hack, but it's pretty inventive, and showcases netcat nicely. I think there are likely to be better production solutions, but for a swiss-army i-have-to-glue-the-mailerver-in approach, I rather liked it. – MadHatter Nov 20 '10 at 14:10

I recommend to install postfix and use this guide: to set up relaying.

This way mail will be handled locally and will be actually delivered by the server specified in relayhost parameter (aka. smarthost). You should put its IP address there.

The smarthost must be configured to accept mail from this machine. This can be done by putting the client IP address to mynetworks parameter on the smarthost.

share|improve this answer

Your Answer


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