How can I change email envelope from email from www-data@hostname to user1@hostname?

November 6, 2014 3.9k views

I have ubuntu 12.04, and I am using sendmail to send emails. But my emails are ending up in spam also the envelope email is www-data@hostname I want to change it to username@hostname.

1 Answer

To prevent your emails from going straight to the spam/junk folder, I would recommend configuring an SPF record: How To use an SPF Record to Prevent Spoofing & Improve E-mail Reliability | DigitalOcean. Also rename your droplet using the control panel to a valid FQDN such as depending on what your MX record is set to.

As for changing www-data@hostname to username@hostname, you will need to set a custom From header. If you're using php's mail() function, your code should look like this:

mail($to, $subject, $message, "From: username@hostname\r\n");
by Pablo Carranza
A carefully tailored SPF record will reduce the likelihood of your domain name getting fraudulently spoofed and keep your messages from getting flagged as spam before they reach your recipients.
  • SPF is set. But email source have these lines. I want to change these to custom email addresses.

    Return-Path: <>
    Received: (from www-data@localhost)

    Also, I have only zoho mail's MX record.

  • It's possible to set Return-Path to whatever you want to too:

    $from = 'username@hostname';
    mail($to, $subject, $message, "From: {$from}\r\nReturn-Path: {$from}");

    The Received header is automatically set by sendmail to the user that is sending the email (www-data in this case, the user that Apache runs as). The easiest way to change that would be to run Apache as a different user but that's not recommended and might create a whole bunch of other problems. Is there any reason you want to set it to a custom email address?

Have another answer? Share your knowledge.