Basic Sendmail Setup
Sending email from your server can be used for about a million different things. Working on a project recently, I realized I had never set up my server to send email to external addresses (like Gmail, for example). On a sidenote, because of the email-to-SMS services that all mobile carriers provide, this also allows you to text mobile phones (assuming you know their mobile number and carrier). Keep in mind, this tutorial is strictly for SENDING email. Receiving or processing email is not included in this tutorial.
For this post, I’ll be setting up sendmail on my CentOS virtual machine. Please see the about page for system specs. Keep in mind that this tutorial should also work for RHEL and Fedora servers.
Sendmail should be installed by default on most Linux operating systems. On CentOS/RHEL systems, the sendmail config files will be in /etc/mail
As always, it’s prudent to make backups before we start changing anything.
[alex@server mail]$ sudo cp sendmail.cf sendmail_backup.cf
You’ll need to ‘sudo’ unless you are logged in as the root user since we are in /etc. OK – now if we botch anything, at least we can go back.
Now – sendmail.cf is the configuration file of interest, but we don’t have to modify the file directly. Instead, we’ll be modifying sendmail.mc and ‘pushing’ those changes to sendmail.cf. More on this later. For now, let’s modify sendmail.mc. I use vim for my text editing in CentOS.
[alex@server mail]$ sudo vim sendmail.mc
Again, use ‘sudo’ – otherwise you won’t be able to commit/save your changes. There are two lines that we need to change, the SMART_HOST line and the DAEMON_OPTIONS line. The SMART_HOST line needs to have your email relay server information. Your ISP should provide a relay server, and you can generally find this information on Google pretty easily. As a Time Warner Cable customer in Milwaukee, I need to use ‘smtp-server.wi.rr.com’. On the DAEMON_OPTIONS line, remove the ‘127.0.0.1’ segment. Your config file should read like this:
define(`SMART_HOST', `smtp-server.wi.rr.com')dnl ... DAEMON_OPTIONS(`Port=smtp, Name=MTA')dnl
Now, here is the “black box” part of the tutorial. In theory, you should be able to commit those changes using the following command:
[alex@server mail]$ sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
Now, that command failed for me with a “Permission Denied” error. Luckily I found another sendmail tutorial with a suggestion. I still need to dig up the “why” behind this, hence my comment that this is the “blackbox” portion of the tutorial. I had to use ‘ksh’ as a workaround, thusly:
[alex@server mail]$ sudo ksh -c 'm4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf'
Now, whenever you change a config file for a service, regardless of OS, you should restart the service. This is just a best practice in general, even if you don’t NEED to restart, it won’t hurt anything either (obviously don’t do this if you’re in a high availability environment or at work or something…). I’ve seen a lot of time wasted troubleshooting because someone forgot to restart a service. In this case, you may have to switch to root to restart the sendmail service.
[alex@server mail]$ su - Password: [root@server ~]# /etc/init.d/sendmail restart Shutting down sm-client: [ OK ] Shutting down sendmail: [ OK ] Starting sendmail: [ OK ] Starting sm-client: [ OK ] [root@server ~]#
That’s it – if you want to send an email, just type “mail firstname.lastname@example.org”. You’ll be prompted for a subject line – enter text, then hit Ctrl+D. You’ll be prompted for a CC: address – either enter text or leave it blank, and hit Ctrl+D again. Your email is sent!
Update/Edit: I got help and information from a lot of sites, so I thought I would provide them here.
Setting SMART_HOST: http://www.emailquestions.com/sendmail/70-sendmail-smarthost.html
The ksh workaround for committing changes to sendmail.mc: http://www.adamsdesk.com/be/archives/2007/10/05/sendmail-setup-as-smart_host/#axzz1yctHZyLf
List of Time Warner Cable email relay servers: http://www.emailquestions.com/road-runner/588-road-runner-incoming-pop-outgoing-smtp-mail-servers.html
Basic sendmail usage: http://www.mydigitallife.info/how-to-send-an-email-mail-message-from-linux-command-line-shell/
Sendmail and cron: http://www.tomshardware.com/forum/236467-50-cron-sendmail