How to Change Postfix Default Sender Address from www-data to Custom Email in Ubuntu Server


3 views

When setting up Postfix with Apache/PHP on Ubuntu, emails often get sent with "www-data" as the sender name because:

  • Postfix defaults to using the executing user's system name
  • Apache/PHP scripts typically run under the www-data user account
  • Basic PHP mail() function calls don't automatically include proper From headers

While setting sendmail_path in php.ini helps with the email address part:

sendmail_path = "/usr/sbin/sendmail -t -i -f support@example.com"

This doesn't solve the display name issue because:

  • The -f flag only sets the envelope sender (Return-Path)
  • The From header still gets generated using the system username

To properly override both the address and display name:

1. Configure Postfix's Sender Canonical Maps

Edit /etc/postfix/main.cf and add:

sender_canonical_maps = regexp:/etc/postfix/sender_canonical
smtp_generic_maps = regexp:/etc/postfix/generic

2. Create the Sender Canonical File

Create /etc/postfix/sender_canonical with:

/^www-data$/ "Example Support <support@example.com>"

3. Create the Generic Maps File

Create /etc/postfix/generic with:

/@localhost$/ support@example.com
/^www-data$/ support@example.com

4. Apply the Changes

sudo postmap /etc/postfix/sender_canonical
sudo postmap /etc/postfix/generic
sudo systemctl restart postfix

For more control in your PHP scripts, use proper mail headers:

$headers = [
    'From' => 'Example Support <support@example.com>',
    'Reply-To' => 'help@example.com',
    'Return-Path' => 'support@example.com'
];

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

Verify your settings with:

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

Check the headers in the received email to confirm the sender information is correct.

For more robust email handling, consider PHPMailer:

use PHPMailer\PHPMailer\PHPMailer;

$mail = new PHPMailer(true);
$mail->setFrom('support@example.com', 'Example Support');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email';
$mail->send();

When setting up Postfix with Apache/PHP on Ubuntu, emails sent through PHP scripts typically inherit the system user's name (www-data) as the default sender identity. This occurs because:

  • Postfix uses the executing user's name when no From header is specified
  • The sendmail interface passes through the default system identity
  • PHP's mail() function doesn't automatically set display names

The initial solution of modifying sendmail_path in php.ini:

sendmail_path = "/usr/sbin/sendmail -t -i -f support@example.com"

This sets the envelope sender but doesn't affect the displayed From name. To force both, you need additional header configuration:

For PHP applications, implement proper headers in your mail script:

$headers = array(
    'From' => 'Example Support ',
    'Reply-To' => 'no-reply@example.com',
    'X-Mailer' => 'PHP/' . phpversion()
);
mail($to, $subject, $message, $headers);

To set system-wide defaults in Postfix, edit /etc/postfix/main.cf:

# Set default sender address
sender_canonical_maps = regexp:/etc/postfix/sender_canonical

# For display name
smtp_generic_maps = regexp:/etc/postfix/generic

Create /etc/postfix/sender_canonical:

/./ support@example.com

Create /etc/postfix/generic:

/www-data/ Example Support 

After configuration:

sudo postmap /etc/postfix/sender_canonical
sudo postmap /etc/postfix/generic
sudo systemctl restart postfix

Test with:

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

For multi-domain setups, modify /etc/postfix/generic:

/www-data@domain1.com/ Support Team 
/www-data@domain2.net/ Help Desk 
  • Verify postfix logs: tail -f /var/log/mail.log
  • Check header parsing with: postconf -n
  • Test SMTP headers with: swaks --to test@example.com --server localhost