Table of Contents

Mail Servers

Postfix Won't Start

If you have disabled IPv6, postfix won't start because it can't connect to an IPv6 interface.

/var/log/maillog

will display something like this when trying to start postfix

Feb  3 14:02:13 template-tomcat postfix[2778]: fatal: parameter inet_interfaces: no local interface found for ::1

The solution is to update the postfix configuration file to only look for IPv4 interfaces.

sed -i "s/inet_protocols = all/inet_protocols = ipv4/g" /etc/postfix/main.cf

Configure Postfix

On some environments, the command to send email doesn't work because postfix doesn't know where to find an email relay. If you have an email relay available (in this example, mail.example.com), configure postfix to use it.

cat << EOF >> /etc/postfix/main.cf
myhostname = hostname.example.com
mydomain = example.com
relayhost = mail.example.com
EOF
service postfix restart

Send Email from Command Line

echo "Test Email" | mail -s "Hello world" target@example.com
cat file.txt | mail -s "Hello world" target@example.com
mail -s "Hello world" target@example.com < file.txt

Send Text Email from Command Line

The first step is to generate a text file with the email and then pipe the file into the

ssmtp

command.

An example file looks like this. For this example, we will call the file email.txt

Subject: Some Subject
From: John Smith <johnsmith@example.com>
To: Jane Doe <janedoe@example.com>

Hello World.

We then send the email using this command

mail target@example.com < email.txt

Send HTML Email from Command Line

The first step is to generate a text file with the email and then pipe the file into the

ssmtp

command.

An example file looks like this. For this example, we will call the file email.txt

Subject: Some Subject
From: John Smith <johnsmith@example.com>
To: Jane Doe <janedoe@example.com>
Mime-Version: 1.0;
Content-Type: text/html; charset="ISO-8859-1";
Content-Transfer-Encoding: 7bit;

<!DOCTYPE html>
<html>
<head>
<style>
table {
    width: 50%
}
</style>
<title>
Example Title
</title>
</head>
<body>
<div>
<p>Some Text</p>
</div>
</body>
</html>

We then send the email using this command

mail target@example.com < email.txt

Install SSMTP

If you want to use SSMTP instead of postfix, you can install it using the following steps.

  1. Download and configure the EPEL repository.
  2. Remove sendmail
    yum remove sendmail
  3. Install ssmtp
    yum install ssmtp

You then configure ssmtp by editing the file

/etc/ssmtp/ssmtp.conf

and editing/uncommenting the following lines.

Edit the following lines:
mailhub=mail.example.com:25
Hostname=test1.example.com
FromLineOverride=YES

Use SSMTP

The simplest way to send email with SSMTP is this way

ssmtp bstafford@example.com << EOF
Subject: Hello World
From: Source Name <source@example.com>
To: Target Name <target@example.com>

Hello World
EOF