How to Fix “Real Domain Required for Sender Address” Error in PHP mail() with Sendmail Configuration


2 views

When your PHP application uses the mail() function with default configurations, Sendmail often rejects the message with error "553 5.5.4... Real domain name required for sender address". This occurs because:

  • The default envelope sender uses apache@localhost.localdomain
  • Most mail servers require FQDN (Fully Qualified Domain Name) in sender addresses
  • SPF/DKIM validation fails for localhost domains

Your attempt with MASQUERADE_AS was correct but incomplete. Here's a complete solution:

dnl /etc/mail/sendmail.mc configuration
MASQUERADE_AS(yourdomain.com')dnl
FEATURE(masquerade_envelope')dnl
FEATURE(masquerade_entire_domain')dnl
MASQUERADE_DOMAIN(localhost')dnl
MASQUERADE_DOMAIN(localhost.localdomain')dnl
define(confDOMAIN_NAME', yourdomain.com')dnl
define(confCF_VERSION', Your Mail Server')dnl
define(confFROM_HEADER', From: $?x$x <$g>$|<$g>$')dnl

For reliable email delivery from PHP:

// Set proper headers including Return-Path
$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'Email content';
$headers = array(
    'From' => 'Apache <webmaster@yourdomain.com>',
    'Reply-To' => 'no-reply@yourdomain.com',
    'Return-Path' => 'bounces@yourdomain.com',
    'X-Mailer' => 'PHP/' . phpversion()
);

// Additional parameters for mail()
$additional_parameters = '-f bounces@yourdomain.com';

mail($to, $subject, $message, $headers, $additional_parameters);

Even with correct configurations, SELinux might block mail sending:

# Check current settings
getsebool httpd_can_sendmail

# Enable if disabled
setsebool -P httpd_can_sendmail on

# Alternative: Allow postfix to send mail from httpd
setsebool -P httpd_can_sendmail=1 allow_postfix_local_write_mail_spool=1

After making changes:

  1. Rebuild sendmail configuration:
    make -C /etc/mail
  2. Restart sendmail/postfix:
    systemctl restart sendmail
  3. Test from command line:
    echo "Test" | mail -s "Test Subject" -r "webmaster@yourdomain.com" recipient@example.com

For better deliverability, configure DNS records:

; SPF Record
yourdomain.com. IN TXT "v=spf1 a mx ip4:your.server.ip ~all"

; DKIM Record (generated with opendkim)
mail._domainkey.yourdomain.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC..."

When using PHP's mail() function, many developers encounter the frustrating "Real domain name required for sender address" error. The issue stems from how sendmail (or postfix) handles envelope sender addresses when they originate from web applications.

// Typical problematic PHP mail() call
mail('recipient@example.com', 'Subject', 'Message', 'From: webmaster@localhost');

The mail server rejects messages when:

  • The sender domain is non-existent (like localhost)
  • The envelope sender doesn't match authenticated domains
  • The reverse DNS lookup fails for the sending IP

Here's the proper way to configure sendmail.mc:

dnl Basic masquerading
MASQUERADE_AS(yourdomain.com')dnl
FEATURE(masquerade_envelope')dnl
FEATURE(masquerade_entire_domain')dnl

dnl Add all domains that might appear in From addresses
MASQUERADE_DOMAIN(localhost')dnl
MASQUERADE_DOMAIN(localhost.localdomain')dnl
MASQUERADE_DOMAIN(yourwebserverhostname')dnl

dnl Important for PHP mail()
FEATURE(always_add_domain')dnl
define(confDOMAIN_NAME', yourdomain.com')dnl

After modifying sendmail.mc, regenerate the sendmail.cf file:

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

Always use proper headers in your PHP mail() calls:

$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'This is a test email';
$headers = "From: webmaster@yourdomain.com\r\n";
$headers .= "Reply-To: noreply@yourdomain.com\r\n";
$headers .= "Return-Path: bounce@yourdomain.com\r\n";

mail($to, $subject, $message, $headers);

For more reliable email delivery, consider using PHPMailer:

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.yourdomain.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@yourdomain.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('webmaster@yourdomain.com', 'Webmaster');
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $message;

if(!$mail->send()) {
    echo 'Message could not be sent: ' . $mail->ErrorInfo;
}

Ensure these server configurations are correct:

  • SPF record for your domain
  • Valid reverse DNS (PTR) record
  • DKIM signing if available
  • SELinux context: setsebool -P httpd_can_sendmail on