Problem sending mail with PHP mail function
Occasionally I have had trouble sending emails using the builtin mail() function in PHP. Sometimes emails never reached their intended destination. Naturally I have assumed that there might be a problem with some spam filters used. However, lazy as I am, I have not given it a second thought. Instead I have used a PHP class that allows me to send emails using a remote smtp server using an account on that server. This has been a good solution for my setup anyways. A few days ago a friend of mine was asked to investigate the very same problem for a client.
The problem seems to be that PHP use the ini directive sendmail_from to set the from email address in the SMTP protocol. If this is not correctly set, or if it does not match the from header in the email headers, the email is caught by spam protection software.
The simplest solution is to set the directive during execution:
ini_set("sendmail_from", $email_from);
$headers = "From: $email_from";
mail($to, $subject, $message, $headers);
The problem as well as the solution was already known by others. The operating system this time was windows and I know there are differences in the implementation between the windows and linux version of mail(). I am not sure if the problem exist on both platforms.
Still I bet there are lots of people out there with this problem without them knowing it. It could be a good idea to include more detailed information about this ini entry and its implications in the actual mail() documentation. Sending emails from web pages is, to say the least, a very common task.
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
Comments
20 Responses to “Problem sending mail with PHP mail function”Trackbacks
Check out what others are saying about this post...-
[...] Problem sending mail with PHP mail function (tags: php mail) [...]
-
[...] web hosting companies do not have the right configuration for php mail. There may not always be a simple solution, and this can be quite frustrating. But what [...]

hello sir,
i m using php in win.There are mail fuctions like ini_set(); is used but its throwing syntex error. i have checked my root directory and functions too.while sending message through e-mail from php.
is there problem in my server port? please suggest…
thank’.
how i can change my SMTP server port. and where i should to use?
Hi Pravin,
Have you tried to check php.ini?
I might be able to help you if you give me more clues to your problem.
Or is it already solved? =)
You can email me with your details (yoke.lee@gmail.com)
Hi, I’m using the mail function on one of my web sites, but the mail does not get delivered. I suspect it’s a similar problem as mentioned above as it did not occur on my localhost server.
I applied your solution, however, it still does not deliver.
The website is hosted on a web hosting provider so, I don’t have much control over the ini settings.
Any suggestions?
Hey James I have the same problem. I have a site using the php mail function and it is hosted by a provider. I don’t know if they know what to do either because I’ve spoke with them several times and we cannot get it to work.
Any help would be great from anyone.
Hi everyone
When I am working on localhost (Mercury Mail),
I have to configure the Mercury Mail to point to an existing and working mail server (which happens to be my webhosting server too).
And I am able to send mail locally.
Does your webhosting company give you email service?
You can access to your email account with webmail and do a test there. Just send email to yourself and see if you got it.
We need more information to analyze, maybe phpinfo() result and php.ini config will do.
Hope it helps.
Thanks.
Yoke Lee
hi,
i have a somewhat similar problem.
here’s the scenario:
(1) using WEBMAIL on server1, sending from domain1 to domain2, domain3 and domain4 is OK.
(2) using PHP MAIL FUNCTION on server1, sending from domain1 to domain2 FAILS, but sending to domain3 and domain4 is OK.
have you encountered the same problem? what could possibly be the reason why message sending fails on (2)?
more details:
domain1 is on server1 (running whm/cpanel on RHEL 5.2)
domain2 is on server2 (running whm/cpanel on RHEL 5.2)
domain3/4 are additional 3rd party domains used to check if mails are received.
any input you could provide would be most appreciated. cheers!
Actually I’ve stopped trying. I always send email via external smtp servers now.
oh, i see. too bad. i’ll let you know then if i find a solution.
by the way, how do you implement the use of external smtp servers? what class are you using?
I use the builtin classes of Codeigniter or Zend Framework depending on which framework is used in the project. I know I’ve used standalone classes as well previously but can’t remember exactly which.
PJ,
Have you found a solution yet? I have the exact same problem.
I am using the Mail() function and trying to send mail out from my code. For example order confirmation code, online lead etc.
Typically some destinations work. Some don’t. Comcast emails addresses do not work. Yahoo almost always disappears. Gmail is ok.
How to solve this. Adding my from address to the whitelist does not seem to solve the problem. Or will it?
thanks
i have the same problem. i used the mail function on my computer that had XMail. so i sent to an account on my ;local computer. it sent the email just fine. the problem occurred when i try sending to other remote emails e.g yahoo and gmail.any body found the solution.
Hi,
We are trying to use similar code mentioned above in our php to send mail. We have done appropriate settings as well in php.ini file. mail() function is returning ‘1′ but its not sending any mail neither throwing any error.
For your reference here is the code of php file
ini file
[mail function]
; For Win32 only.
SMTP =
smtp_port = 25
; For Win32 only.
sendmail_from = xxx@yyy.com
; For Unix only. You may supply arguments as well (default: “sendmail -t -i”).
sendmail_path = /usr/sbin/sendmail
Can you please let me know what is the issue
Hi,
I m trying to send email from a web page using PHP mail() function.
example:
mail($to, $subject, $message, $from);
Mails are not received to its destination.
Please let me know the solution…
Thanks.
ini_set(“SMTP”, “mail.domain.com”);
ini_set(“smtp_port”, 25);
//ini_set(“sendmail_from”, “info@domain.com”);
Hi sir
I am using bellow code to send a mail, my client website is hosted on
Windows IIS/6.0
I am not able to send a mail using this code
please give me a solution for this
$mailTo = “name@hotmail.com”; // Recipient email id
$subject = ‘Test mail’;
$message = ‘Test mail’;
$mailHeader = “Content-type: text/html; charset=iso-8859-1\r\n”;
$mailHeader = $mailHeader . “From: info@domain.com\r\n”;
$mailHeader = $mailHeader . “BCC: name@gmail.com\r\n”;
if(mail($mailTo, $subject, $message, $mailHeader)) {
echo “ok”;
} else {
echo “error”;
}
Thanks and regards
Mahesh
Hi all,
I see there are recent comments, so here is a solution to the problem:
1. You are using a web hosting service, that includes Linux e-mail service. Just create an e-mail account on your server:
youremail@yourdomain.com
2. in your php mailing script be sure to keep the following:
$MailHeader = “From: Just created this account on WEB \n”;
NB: this solves your problem about sending emails from the localhost. If you want your e-mails not to be handled as SPAM, simply add a next line to the mail header which contains your email as Reply-to.
Best regards,
John S.
Hi,
i have been able to send email to my yahoo and gmail using the php mail script, but i don’t know why it doesn’t send to other domains like lycos.
help anyone?
Thanks a lot. I have been looking for this very long. It works.