Solved pending: Setup a local only, forwarding Mail Server (Linux, Unix, Mac)
Setup a local only, forwarding Mail Server (Linux, Unix, Mac) 1 - Point localhost.com to your machine
Most of programs will not accept an email using just @localhost as domain. So, edit /etc/hosts file to make the domain localhost.com point to your machine by adding this line to the file:
127.0.0.1 localhost.com
To make the change, open the /etc/hosts file using nano or your favorite text editor.
sudo nano /etc/hosts 2 - Install Postfix
Fedora/CentOS/RHEL: sudo yum install postfix
Ubuntu: sudo apt-get install postfix
Open SuSE: sudo zypper install postfix
MacOSX: Postfix is already installed by default.
BSD: sudo pkg install postfix
If postfix is not found by command pkg try installing from the ports collection:
cd /usr/ports/mail/postfix; makeinstall clean;ln -s /usr/local/etc/postfix /etc/postfix
During postfix install process, the configure text dialog will display five options:
General type of mail configuration:
No configuration
Internet Site
Internet with smarthost
Satellite system
Local only
Select option "Internet"
For the domain name, use the default suggested or the one you want to use and finish the install. 3 - Allow only local mail and forward to relayhost
In this step, you'll read how to configure Postfix to process requests to send emails only from the server on which it is running, that is, from localhost.
For that to happen, Postfix needs to be configured to listen only on the loopback interface, the virtual network interface that the server uses to communicate internally. To make the change, open the main Postfix configuration file using nano or your favorite text editor.
sudo nano /etc/postfix/main.cf With the file open, scroll down and search for the following lines:
/etc/postfix/main.cf
. . . mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all . . . relayhost = . . .
Change interface line from inet_interfaces = all to inet_interfaces = loopback-only.
Change relayhost line from relayhost = to relayhost = [YOUR-SMTP-SERVER-IP-HERE]:587 where [YOUR-SMTP-SERVER-IP-HERE] has to be replaced by the IP adress of your relay mail server. depending on relay mail server you may have to change used port from 587 to 25
. . . mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = loopback-only . . . relayhost = [YOUR-SMTP-SERVER-IP-HERE]:587 . . .
Another directive you'll need to modify is mydestination, which is used to specify the list of domains that are delivered via the local_transport mail delivery transport. By default, the values are similar to these:
/etc/postfix/main.cf
. . . mydestination = $myhostname, example.com, localhost.com, , localhost . . .
The recommended defaults for that scenario are given in the code block below, so modify yours to match:
. . . mydestination = myhostname, localhost.mydomain, $mydomain . . .
Save and close the file. 4 - Activate new Config
Reload postfix with systemd: sudo systemctl restart postfix
Alternativly use service restart command: sudo service postfix reload
5 - See Mail Server in Action
You can verify what postfix is doing and that email is sent or not by watching the mail log file. Open a terminal and enter:
sudo tail -f /var/log/mail.log OR sudo journalctl -u postfix
Tail -f will display the last 10 log entrys and all new incomming log entrys are displayed until you hit + C 6 -Testing the SMTP Server
In this step, you'll test whether Postfix can send emails to an external email account using the mail command, which is part of the mailutils package that was installe.
To send a test email, type:
echo "This is the body of the email" | mail -s "This is the subject line" your_email_address
In performing your own test(s), you may use the body and subject line text as-is, or change them to your liking. However, in place of your_email_address, use a valid email address. The domain part can be gmail.com, fastmail.com, yahoo.com, or any other email service provider that you use.
Now check the email address where you sent the test message. You should see the message in your inbox. If not, check your spam folder. 7 - Forwarding System Mail
The last thing you may want to set up is forwarding, so you'll get emails sent to root on the system at your personal, external email address.
To configure Postfix so that system-generated emails will be sent to your email address, you need to edit the /etc/aliases file.
sudo nano /etc/aliases The full contents of the file on a default installation of Ubuntu 16.04 are as follows:
/etc/aliases
See man 5 aliases for format
postmaster: root
With that setting, system generated emails are sent to the root user. What you want to do is edit it so that those emails are rerouted to your email address. To accomplish that, edit the file so that it reads:
/etc/aliases
See man 5 aliases for format
postmaster: root root: your_email_address
Replace your_email_address with your personal email address. When finished, save and close the file. For the change to take effect, run the following command: sudo newaliases
You may now test that it works by sending an email to the root account using: echo "This is the body of the email" | mail -s "This is the subject line" root
You should receive the email at your email address. If not, check your spam folder.
https://gist.github.com/gnadelwartz/5029af083208a5bf8da15aeda4a046ad