Today, I wanted to set up a sendmail server to let the server send mail out. The uses of this are many – I’m thinking it’d be helpful for situations like WordPress or Joomla where a user might need a password-reset from time to time, or if I’m posting resumes and a recruiter might like it if I’ve got email coming from my own domain instead of from gmail.
The ingredients:
Sendmail (was included already in my FreeBSD distro)
One complete Jail (was created earlier on) – not really a neccessity – more a nice to have for security purposes.
Procedure (what I did to make it work):
Well, like most installations, there was a lot of futzing around with the configuration to whip Sendmail into shape. Apparently Sendmail is notorious for its byzantine configuration system. A lot of people really seem to like postfix or exim. I decided to stay with Sendmail:
1. Make sure sendmail_enable=”YES”
exists in the /etc/rc.conf file.
2. edit your sendmail configuration file. Most sites, and even books are kind of wishy-washy about this – a lot of them (for example FreeBSD handbook Chapter 29, and Absolute FreeBSD 2nd edition), just pass the issue off as being “beyond their scope.” This is a nice way of saying they have no idea what they did to get it working back when they did, and you should just use postfix or ISP’s server or something. Anyway, how you edit your /etc/mail/hostname.mc file is really dependent on what you want your Sendmail server to do. Believe me, it is possible for you to get Sendmail working. It’s not that hard, especially with FreeBSD.
My configuration:
Well, like most folks these days I’ve got a firewall that uses NAT (network address translation). I do not have a static IP address through my ISP. I am using DynDNS to handle that stuff. Also, it seemed like way overkill (and was confusing with the jail involved) to set up BIND internally to get my three computers to talk to one another by name, much less to the rest of the world. Far easier just to use the hosts file in /etc/ for that.
The problem:
I tried sending email from my server to my gmail address using mutt, and the source address was always including the machine name. In other words, the email was coming from scott@jail.scottspare.com and not from scott@scottspare.com. Googling around, this sounded like a problem a lot of people have, but the documentation was pretty un-helpful.
The first set of suggestions involved adding something like this to the domainname.mc file in /etc/mail:
MASQUERADE_AS(`scottspare.com’)
MASQUERADE_DOMAIN(`scottspare.com’)
FEATURE(`masquerade_entire_domain’)
FEATURE(`masquerade_envelope’)
FEATURE(`allmasquerade’)
FEATURE(`use_cw_file’)
Note that some people around google add “dnl” to the end of all these lines, but I didn’t – I just made sure that no spaces or anything were at the end of the lines. I guess dnl is sendmail’s way of handling comments. No wonder people think it’s weird.
Anyway, in FreeBSD to make your configurations stick, after you edit and save these .mc files, you need to run the following as root from your /etc/mail directory:
make
make install
make restart
this creates the new .cf files named after your host in the /etc/mail directory, installs them, and restarts the sendmail server, respectively. More information on that procedure is located in the freebsd handbook.
So according to most sites out there, you should be all set, right? Your hostname or machine name should not appear in your emails anymore, correct? Well, that’s what I thought too – but I was wrong. In order to actually get it working, I had to remove the following line from domainname.cf file. (I know that everyone says never to edit domainname.cf, but this worked for me.)
I commented out a line that said C{E}root from my domainname.cf file by adding a # in front of it, and that made it work. I think that line is telling sendmail to append the machine name on emails coming from root. Why that’s the default I have no clue. Now when I send emails from mutt, they come from user@scottspare.com, not from user@host.scottspare.com. It’s great.
Other stuff: I had to tell my firewall to allow port forwarding on port 25. That’s required so the machine can receive mail back to the domain. If you only want to send and not receive, you probably can ignore that.
Some websites that helped me out:
Freebsd sendmail handbook article
Sendmail’s page on masquerading syntax (didn’t help that much)
This page from HP which was really confusing and obscure, but helped anyway.
And, to be able to read my email from the non-jailed server using mutt, I had to edit .muttrc outside the jail to tell it to point to the jail’s mail folder. This is under mailboxes if I remember correctly. You can start mutt using “mutt -y” to get a list of your mailboxes.
At one point, I realized that the MTA setting was not defined in DYNDNS. Actually, I had tried earlier to tell DYNDNS about the actual hostname of my mail server, but even after exposing the BIND server I’d set up to the internet by allowing port forwarding on the router on port 53 to my server’s ip address, DYNDNS just didn’t seem to want to do it. So I turned wildcard on, opened port 25 (for sendmail), entered just the hostname under the MTA field in DYNDNS, and it works.
It’s possible and likely there’s a better way to do it all. I welcome comments.
Next steps on this one: I’ll attempt to set it up so I only need to run one sendmail server for the network. Also a dovecot server for IMAP.
Until next time,
Scott