Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem with php mail 'From' header

Tags:

php

email

smtp

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

My code (the gist of it):

<?php $to = "[email protected]"; $subject = "Test mail"; $message = "Hello! \nThis is a simple email message.";  $headers = "From: [email protected]"; $headers .= "\r\nReply-To: [email protected]"; $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 [email protected], 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?

like image 643
jrharshath Avatar asked Jan 06 '10 15:01

jrharshath


People also ask

Why is my PHP mail function not working?

Make sure the localhost mail server is configuredWithout one, PHP cannot send mail by default. You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail. You can also use SMTP to send your emails.

How do you check PHP mail () is working?

to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>

Why does PHP mail take so long?

It is the SMTP mail delivery (which PHP hands off the message to) which is taking time. Possibly, the delay you see is greylisting on the receiving server, meaning that the receiving mail server refuses to accept the message until the sending server (which your PHP script handed it to) tries a few times.


2 Answers

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 [email protected]"); 
like image 85
Pekka Avatar answered Oct 05 '22 22:10

Pekka


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: [email protected]
From: "user" <[email protected]>

Examples of headers that will NOT work:

From: "[email protected]"
From: user @ domain.com
From: [email protected] <[email protected]>

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.

like image 31
John Connolly Avatar answered Oct 05 '22 23:10

John Connolly