Why is my PHP script not sending emails?

It has happened too often to me to get stuck with a ‘form to email’ script. Everything appears right but the email sent by the script never reaches the destination address.

This article takes you through the steps to troubleshoot the problem.

Email : background

email is one of the earliest communication systems evolved on the internet. Though email is still the most prevalent communication system on the internet, email spam has grown to be a huge problem.

About 100 billion spam emails are being sent every single day (yeah, not a typo; 100 billion, every day!). Over 94% of all email sent is spam. Every email server gets a fair share of that 100 billion and has to cope with the bombardment of such a huge amount of spam. Therefore, email servers (MTA: Mail Transfer Agents) have equipped themselves with methods to scrutinize the email messages and reject the email if it suspects the message to be spam.

This is the reason why your script should craft the email carefully to avoid the message being rejected.
email-bombardment

The ‘From’ field and source address spoofing

An email message has two parts: a header and the message itself. The header contains a few pieces of identifying information. The ‘From’ field is one of the email header fields. The purpose of ‘From’ field is to indicate who sent the email. However, the email sender can set the ‘From’ field to be any address of his choice.
Spammers exploit this aspect of the protocol. For example, a spammer can send an email making it appear like the email is coming from security@bank.com and ask the victim to give the bank log-in details. The spammer does not need access to the bank server to do this. Adding a “From: security@bank.com” header to the email will make the users think that the email is coming from the bank.
For details see email source address spoofing

The cure: SPF

Sender Policy Framework (SPF) was introduced to prevent email source address spoofing.
Here is how it works:
Let’s say a spammer is sending you a fake message.

  • Your email server receives the message from the spammer.
  • The email server looks for the ‘From’ address in the email header.
    Suppose ‘From’ field is ‘security@bank.com’
  • Your email server requests for the DNS records of bank.com domain ( in simple terms, DNS is for resolving a domain name like bank.com to an IP address ). The DNS records of a domain can contain SPF records. The SPF records will indicate which IP addresses can send email on its behalf. Most of the time, it will only be the main mail server (example: bank.com in this case). Since the spammer’s IP address is different, the email gets rejected.

Imagine you received a phone call and the caller claimed that he is calling from the bank. You can verify it by looking up the incoming call’s phone number in the telephone directory. The SPF system follows the same method.

Setting the ‘From’ field right

Have a ‘From’ field in the emails that you sent through the script. The From address should belong to the domain from where you are running the script. If your script is running on your-website.com then the From address should be like someone@your-website.com.

Sample PHP code:

$headers = "From: someone@your-website.com";
mail($to,$subj,$body,$headers);

What if you want to have some-other.com as the from address?
You have update the SPF records of some-other.com to indicate that your-website.com can send emails on behalf of some-other.com. See: openspf.org for details.

Replying to the visitor who submitted a form

When sending email from a ‘form to email’ script, it will be convenient to have the website visitor’s email address in the ‘From’ field. You can reply to the message by just pressing the ‘Reply’ button in the email client. However, this can result in the email not reaching you.

For example, suppose this is the form to email code:

$visitor_email = $_POST['email'];
$headers = "From: $visitor_emailrn";
mail($to,$subj,$body,$headers);

Now, if someone submits their email address as ‘someone@a-company.com’, when the email server receives this email message, it checks whether your website can send emails on behalf of a-company.com domain and ends up rejecting the email.

The right alternative is to use the ‘Reply-To’ email header. Set the ‘From’ to be an address belonging to your web site domain and add a ‘Reply-To’ header with the web site visitor’s email address.

Example code:

$headers = "From: name@your-website.com";
$headers .= "Reply-To: $visitor_email";
mail($to,$subj,$body,$headers);

Keep in mind that you need to sanitize all the values used in the header attribute of the mail().

Make sure the php.ini is configured right

If you are still not receiving the emails, the problem could be in the PHP setup. The mail should be configured in the PHP configuration file php.ini.

The following code shows a typical PHP mail configuration (using sendmail)

[mail function]
; Setup for Linux systems
sendmail_path = /usr/sbin/sendmail -t
sendmail_from = me@myserver.com

The following code shows SMTP configuration in php.ini

[mail function]
; Setup for Windows systems
SMTP = smtp.my.isp.net
sendmail_from = me@myserver.com

Note that you can update the php.ini only if you are running your own web server. Instead, if you are hosting the website with a web hosting service, contact the service provider to setup php mail right.

A simple script to test your PHP –> mail configuration

Download this php email script. Edit the file and update the $from_add and $to_add variables. (see the instructions in the code). Upload the script to your web site. Access the page. You will see a submit button on the page. Press the submit button to send an email to yourself.
If you are not getting the email still, get help from your hosting service provider/network admin. Point them to this sample script that fails to send email.

Be Sociable, Share!

Comments on this entry are closed.

  • I had this little problem days ago… It gave me a headache, but asked my host provider to correct the php.ini problem and after just the heaven!

    We must pay attention to this things, otherwise we are caught up…

    Indeed, good post!

  • Even easier, use phpMailer class.

  • Jeff Rowberg

    I had the unfortunate experience of troubleshooting a problem where the message header ORDER actually mattered. This message didn’t go through:

    Content-type: text/html
    From: Joe Somebody

    But this one did:

    From: Joe Somebody
    Content-type: text/html

    Go figure. I guess some spam filters are really picky, or maybe the RFC dictates it must be that way and the server rejected it as malformed. I may never know.

  • Boiss

    phpMailer is only a helper. If you give a wrong ‘From’, that also can fail to send email.

    Prasanth

  • Scott

    what if the mail is getting sent to gmail and hotmail in the ‘to’ adress and not for other domain names in the ‘to’ address then what can be the issue?

    • Yeah, can someone answer Scott? I have his same problem. I’m using a seriously simple, stripped down script to test and it just won’t work even though the one I got from this site works. What if it sends to Gmail but not to bill@chooseclever.com like I want it to?

      • Looks like I’m having the same problem. I’ve tried two different gmail accounts, and it works perfectly. Then I try two different client servers email addresses (with corresponding reply-to addresses), but the form does not get delivered.

    • It works fine, the only problem is it goes to junk. You will not receive an E-mail where you specify the From Section, that’s the sender. Change the “to” section to where you want to receive the E-mail. So $to_add = “me@mywebsite.com”;
       
      Hope that helps

  • Useful post, thanks for sharing it. I have also problem to send mails but your blog is solved my problem!!!!!!

  • In my case, if I use a gmail address in my webMaster variable, the form gets delivered perfectly (even with the reply-to address being something@mywebserver.com). But when I use me@mywebserver.com in the webMaster variable, the email doesn’t get delivered.
    Does this mean that my webhost needs to make some changes to the php setup?

  • galios

    God bless you.
    i was wandering from days… and here is the light
    I still don’t understand why your sample works while instead of my code, but i really thank you a lot.
    Galios

  • Vanessa

    I downloaded the code but when I test it, it actually prompts me to save a file.  No email goes out 🙁  Any ideas?… I tried checking the php.ini but couldnt even find this file.
    thanks in advanced
    Vanessa

    • Prasanth

      most probably PHP is not configured properly on your webserver

  • jay

    i tried your code and it works but the problem is never send a message to my email account. why is it? hope for your response.

  • excelente

    excelente.
    Perfect.

  • Still in problem, I have tried all the code & stuff & still enable to send the mail
    i am using WAMP server to configure php.ini file & intelli j idea 10 java development tool to develope html code..All data after submission get display on browser(mozilla 3.5) url..& also tried in IE6 ….
    Any help will deeply appreciated ..thanks in advance …
    kartik

  • AJ

    My message doesn’t get sent.. I installed mini_sendmail

    My error log shows:

    sh: /usr/sbin/sendmail: Permission denied

    I gave it ever conceivable permission… and still receive the same error..

    Any suggestions?

  • benz

    web form is NOT sending the email body, please suggest : basically i dont know php or coding,
    Php code is as follows.

    <?php
    if(isset($_POST['submit']))
    {

    $from_add = "info@myweb.com";

    $to_add = "benz0099@gmail.com"; //<€“ put your yahoo/gmail email address here

    /* Gathering Data Variables, */

    $name = $_POST['Name'];
    $phone = $_POST['mobile'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    $body €“ <<<EOD

    Name : $name
    Mobile No. : $mobile
    Email : $email
    Message : $message
    EOD;

    $webMaster = €˜info@myweb.com€™;//

    Thank you!

    Thanks for the submission!
    <?php echo $msg

  • Dinesh Kumar

    Hi,

    Please refer to the post http://top-answers.net/webhost/web-hosting.html
    Hope this will solve your problem. Thanks

  • I am working as a web developer, but i did not know this php.ini settings, thanks for your blog. thank u

  • nivas

    is it posssible to send mail from localhost to mail id???

  • chreis

    Thanks! You save me a lot of time and money.