Forcing sendmail to use a different port for outgoing mail

Attention: This content is 12 years old. Please keep its age in mind while reading as its contents may now be outdated or inaccurate.

In the world of Linux (specifically CentOS 5 in this case) sendmail is a very useful utility.  It is what allows linux to send email to regular ole mail servers.  But, if you’re reading this, you likely know that.

Typically sendmail uses standard SMTP port 25 to do its business.  In most cases, this works fine as is, but more and more now I am seeing ISP’s block port 25 and leave the customer with no recourse (coughAT&Tcough).  This makes it particularly difficult for sendmail to do its thing since it is supposed to work on port 25.

The simplest solution is to reconfigure sendmail to send out on a port other than 25.  The only catch-22 here is that the mail server must accept mail on that port as well.  Now, I’ve already set that up using iptables with a simple REDIRECT function and tested to make sure my CentOS box can communicate to it on that port using telnet, so I’m not going to get in to that, as anyone can forward a port).

First, you’ll likely need to install a package that allows for easy editing of the sendmail configuration file:

yum install sendmail-cf

Next you’ll edit your sendmail config file:

vi /etc/mail/sendmail.mc

And you’re going to need to add in these two magic lines:

define(`SMART_HOST’,`mail.yourmailserver.com’)dnl
define(`RELAY_MAILER_ARGS’,`TCP $h 26′)dnl

Note to be careful of backticks and single quote marks in the above two lines.  The first line is the address of the mail server you’re sending to (luckily in my case I only had to send to this one server.  If you need to send to others, you will need to have this mail server you’re sending to set up to act as a true smart host that can then forward the mail on to other mail servers).  The second line is the port you’re sending to.  In my case, I set up port 26 since it was not being restricted by the ISP.

Next you need to make the new config (this is what the sendmail-cf package helps out with) and then restart sendmail:

make
service sendmail restart

And you should be working!

To test it out, first I would tail the maillog in var log so that you can watch for errors, then do a:

mail -s “Test” youraccount@yourdomain.com <enter>
Type your body here <enter>
<CTRL+D><CTRL+D>

If all went as it should, you should see the mail you send pop in to your mailbox soon.

If not… well, good fucking luck.  Sendmail is a royal pain in the dick to configure if it doesn’t work out of the box.  However, for me, this was enough to get the box sending mail to my mail server and restore the email notification functionality.