Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

3 Answers 3

The ssmtp tool does what you want: http://www.linux.com/archive/feature/132006

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 ?"
mail_srv_ip=127.0.0.1
mail_srv_port=25
recipient=jean-kevin@gmail.com

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

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: http://www.postfix.org/STANDARD_CONFIGURATION_README.html#null_client 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

 
discard

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