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.

Tags:

16 Responses to “Problem sending mail with PHP mail function”

  1. 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’.

  2. how i can change my SMTP server port. and where i should to use?

  3. 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)

  4. 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?

  5. 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.

  6. 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

  7. 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!

  8. Actually I’ve stopped trying. I always send email via external smtp servers now.

  9. 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?

  10. 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.

  11. 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

  12. 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.

  13. 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

  14. 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.

Trackbacks/Pingbacks

  1. links for 2008-09-12 « Richard@Home
  2. duckranger.com - Emails Not Sent? - Post Notification and WP-MAIL-SMTP Configuration

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">