34

I'm building a website that sends and email to a user when he registers.

My code (the gist of it):

<?php
$to = "helloworld@gmail.com";
$subject = "Test mail";
$message = "Hello! \nThis is a simple email message.";

$headers = "From: munged@gmail.com";
$headers .= "\r\nReply-To: munged@gmail.com";
$headers .= "\r\nX-Mailer: PHP/".phpversion();

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

echo "Mail Sent.";
?> 

the problem is that when the mail is delivered, the from header remains munged@box123.bluehost.com, while reply-to gets changed to the specified value.

box123.bluehost.com is the hostname of the server on which the website is hosted.

So what am I doing wrong? What can I do to get the "From" address the same as the reply-to address?

Is it something I'm doing wrong, or is the web host playing foul?

jrharshath
  • 25,975
  • 33
  • 97
  • 127

8 Answers8

65

Edit: I just noted that you are trying to use a gmail address as the from value. This is not going to work, and the ISP is right in overwriting it. If you want to redirect the replies to your outgoing messages, use reply-to.

A workaround for valid addresses that works with many ISPs:

try adding a fifth parameter to your mail() command:

mail($to,$subject,$message,$headers,"-f your@email.here");
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    I'm actually not using "From: hithere@gmail.com". I'm using some other address, but I guess your point is that the server will not allow me to send a mail as just some random person. but that is so wrong! – jrharshath Jan 06 '10 at 15:56
  • 21
    Sorry mate, but that is so, so right! Check your spam folder for supporting evidence. :) – Pekka Jan 06 '10 at 16:03
  • 2
    so I contacted the admin for our web host and had the reqd email id added as "authorized". Now it works. Thanks! – jrharshath Jan 06 '10 at 17:35
  • I also have this same issue but with adding this code it didnt work generally? – Ana DEV Sep 28 '15 at 05:48
25

It turns out the original poster's server (blueHost) has a FAQ concerning this very question.

Article 206.


This is because our servers require you (or your script) to use a properly formatted, valid From: field in the email's header. If the From: field is not formatted correctly, empty or the email address does not exist in the cPanel, the From: address will be changed to username@box###.bluehost.com.

You must change the script you are using to correctly use a valid From: header.

Examples of headers that should work would be:

From: user@domain.com
From: "user" <user@domain.com>

Examples of headers that will NOT work:

From: "user@domain.com"
From: user @ domain.com
From: user@domain.com <user@domain.com>

Our servers will not accept the name for the email address and the email address to be the same. It will not accept a double declaration of the email address.

For scripts such as Joomla and Wordpress, you will need to follow their documentation for formatting the from fields properly. Wordpress will require the Mail From plugin.

Note: The email address you use must be a valid created account in the cPanel.

Machavity
  • 30,841
  • 27
  • 92
  • 100
John Connolly
  • 251
  • 3
  • 3
  • 2
    Do you have a link to the article that you can add to your answer? –  Jun 08 '11 at 16:12
  • Turns out, at least on my server, all of a sudden the from header cannot contain non-english letters (e.g. Hebrew). Switching to `From: user@domain.com` worked. – Itay Dec 30 '19 at 09:09
20

I had the same Issue, I checked the php.net site. And found the right format.
This is my updated code.

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:  ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" .
            'Reply-To: '.  $fromEmail . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

The \r\n should be in double quotes(") itself, the single quotes(') will not work.

Eugine Joseph
  • 1,552
  • 3
  • 18
  • 40
  • 1
    "The \r\n should be in double quotes(") itself, the single quotes(') will not work." - Thanks! That as my issue! – Collin Anderson Sep 12 '17 at 20:20
  • 1
    `$fromName` should also be enclosed in double quotes. If the variable contains certain characters (single or double quotes e.g. "John O'Malley", period e.g. "Suzy Q.", etc) it could be invalid without quotes. The correct line would be `$headers .= 'From: "' . $fromName . '" <' . $fromEmail . '>' . "\r\n" .` – Doktor J May 07 '18 at 13:21
  • 1
    The bit `' <' . $fromEmail .'>'` is what worked for me. Soon as I used that formatting they started coming in. Thanks! – RalphTheWonderLlama Nov 27 '20 at 20:28
4

In order to prevent phishing, some mail servers prevent the From from being rewritten.

Luís Guilherme
  • 2,620
  • 6
  • 26
  • 41
  • 5
    True, but it should be possible to use a domain as sender name that is registered to that server. Forcibly overwriting *anything* to the basic hostname of the server is ridiculous. – Pekka Jan 06 '10 at 15:46
1

I realize this is an old thread, but i had the same problem since i moved to bluehost yesterday. It may not have been the selected answer but i support the bluehost article 206 reply.

I created a valid email in control panel and used it as my From address and it worked.

Rvenca
  • 11
  • 1
1

I solved this by adding email accounts in Cpanel and also adding that same email to the header from field like this

$header = 'From: XXXXXXXX <test@test.org>' . "\r\n";
Ana DEV
  • 1,018
  • 2
  • 17
  • 39
0

The web host is not really playing foul. It's not strictly according to the rules - but compared with some some of the amazing inventions intended to prevent spam, its not a particularly bad one.

If you really do want to send mail from '@gmail.com' why not just use the gmail SMTP service? If you can't reconfigure the server where PHP is running, then there are lots of email wrapper tools out there which allow you to specify a custom SMTP relay phpmailer springs to mind.

C.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • How would you do that with a contact form so users can send emails to the website owners using a contact form on the owner's website, via the mailer.php and php `mail()` from the server? – SherylHohman Mar 23 '20 at 20:56
  • Search for answers on the internet, ask your hosting provider, if you don't have a solution, post a NEW question on this site or similar and test the answers. – symcbean Mar 24 '20 at 12:25
0

headers were not working for me on my shared hosting, reason was i was using my hotmail email address in header. i created a email on my cpanel and i set that same email in the header yeah it worked like a charm!

 $header = 'From: ShopFive <site@mysite.org>' . "\r\n";
Stefan
  • 3,850
  • 2
  • 26
  • 39
zeeshan
  • 31
  • 6