How to Configure CentOS 5.5 Server to Send Emails via SMTP for Cron Jobs and PHP Applications


1 views

When setting up a CentOS 5.5 home server, one common requirement is enabling email notifications - whether for cron job alerts, PHP application messages, or system monitoring. The challenge lies in configuring a lightweight email solution without deploying a full mail server infrastructure.

CentOS 5.5 typically comes with sendmail pre-installed, but direct configuration requires understanding several key elements:

  • MTA (Mail Transfer Agent) - sendmail or postfix
  • SMTP relay configuration
  • Firewall and port considerations
  • Email authentication requirements

For basic functionality where emails only need to reach external accounts occasionally:

# Install required packages
yum install sendmail sendmail-cf m4

# Start and enable sendmail
service sendmail start
chkconfig sendmail on

# Configure sendmail.mc (minimal changes)
dnl # Remove SMART_HOST setting if present
define(confDOMAIN_NAME', yourdomain.com')dnl
FEATURE(msp', [127.0.0.1]')dnl

# Rebuild sendmail.cf
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
service sendmail restart

To verify basic functionality, use these commands:

# Simple test email
echo "Test message body" | mail -s "Test Subject" user@example.com

# View mail queue
mailq

# Check logs in real-time
tail -f /var/log/maillog

For reliable delivery to external accounts like Gmail, configure sendmail to use Gmail's SMTP:

# Edit sendmail.mc
define(SMART_HOST', smtp.gmail.com')dnl
define(confAUTH_MECHANISMS', EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(authinfo', hash -o /etc/mail/authinfo.db')dnl

# Create authinfo file
echo 'AuthInfo:smtp.gmail.com "U:root" "I:your@gmail.com" "P:yourpassword" "M:PLAIN"' > /etc/mail/authinfo
makemap hash /etc/mail/authinfo < /etc/mail/authinfo

# Rebuild configuration
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
service sendmail restart

For PHP applications, ensure proper sendmail_path in php.ini:

sendmail_path = "/usr/sbin/sendmail -t -i"

When emails don't arrive, check these aspects:

  • Verify port 25 (or 587 for Gmail) is open
  • Check DNS resolution (nslookup smtp.gmail.com)
  • Inspect /var/log/maillog for delivery attempts
  • Test with different recipient addresses

For a lighter alternative to sendmail:

# Install ssmtp
yum install ssmtp

# Configuration (/etc/ssmtp/ssmtp.conf)
root=your@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=your@gmail.com
AuthPass=yourpassword
UseSTARTTLS=YES

When you first try sending emails from CentOS using the default sendmail installation, you'll likely encounter delivery failures to external domains (especially Gmail). The mail logs might show successful local delivery, but messages never reach external recipients.

Several factors prevent your emails from being delivered:


1. ISP blocking port 25 (outbound SMTP)
2. Missing reverse DNS (PTR) record
3. Gmail's strict spam filters rejecting emails from residential IPs
4. Missing SPF/DKIM/DMARC records

The most reliable method is to configure Postfix to relay through Gmail's SMTP servers:


# Install Postfix if not present
yum install postfix cyrus-sasl-plain

# Configure /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes

Create /etc/postfix/sasl_passwd:


[smtp.gmail.com]:587    your@gmail.com:yourpassword

Protect your Gmail credentials with:


chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
systemctl restart postfix

Verify your setup works:


echo "Test message" | mail -s "Test Subject" recipient@example.com

For PHP applications, modify php.ini:


[mail function]
sendmail_path = "/usr/sbin/sendmail -t -i"
SMTP = smtp.gmail.com
smtp_port = 587

Configure cron to use your new mail system by adding to crontab:


MAILTO="your@email.com"

Check these logs when debugging:


/var/log/maillog
/var/log/messages
journalctl -u postfix

For lighter needs, consider ssmtp:


yum install ssmtp
# Configure /etc/ssmtp/ssmtp.conf
mailhub=smtp.gmail.com:587
AuthUser=your@gmail.com
AuthPass=yourpassword
UseTLS=YES
UseSTARTTLS=YES