PHP Mail Function with Bluehost, Working

June 24, 2013

I had been re-configuring and re-developing some PHP code that I had for an idea for a networking site.

After moving servers to Bluehost, I uploaded the PHP code and re-configured and tested some of the code and it all worked OK apart from the PHP mail() function. It simply would not send out emails and after realising that the mail command was returning a FALSE value after execution, I went about finding out what was wrong, as the mail function had not seemed to have been changed since version PHP version 4.3.0 and I was using version 5.2.17

Simple code for sending emails that worked is as follows:

$emailto = '';
$emailfrom = '';
$subject = 'Email Subject';
$messagebody = 'Hello.';
$headers = 
	'From: ' . $emailfrom.' . "\r\n" .
	'Reply-To: ' . $emailto . ' . "\r\n" .
	'X-Mailer: PHP/' . phpversion();
mail($emailto, $subject, $message, $headers);

However, with the above example, the name of the sender and the name of the recipient aren’t included and even though my previous code worked on the old servers,  Bluehost just did not like it and emails were not being sent.

Anyway, after 3 days of re-writing and re-formatting the most complex part of the code in the mail function, which is not actually that complex (the headers variable), with multiple emails backwards and forwards with Bluehost support (thanks guys and girls), and looking at how other scripts were sending out emails that worked, I eventually found a format that worked and sent out emails and included the from and to names, on Bluehost servers.

So, here’s the code. It’s not that complex but when you don’t know, you don’t know, until you do.

Please be careful with the syntax of the single and double quotes when creating the headers variable, which is what through out my code in the first instance.

$emailto = '';
$toname = 'TO NAME';
$emailfrom = '';
$fromname = 'FROM NAME';
$subject = 'Email Subject';
$messagebody = 'Hello.';
$headers = 
	'Return-Path: ' . $emailfrom . "\r\n" . 
	'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" . 
	'X-Priority: 3' . "\r\n" . 
	'X-Mailer: PHP ' . phpversion() .  "\r\n" . 
	'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
	'MIME-Version: 1.0' . "\r\n" . 
	'Content-Transfer-Encoding: 8bit' . "\r\n" . 
	'Content-Type: text/plain; charset=UTF-8' . "\r\n";
$params = '-f ' . $emailfrom;
$test = mail($emailto, $subject, $messagebody, $headers, $params);
// $test should be TRUE if the mail function is called correctly

NOTE: Some hosting companies disable sending email using the PHP mail() command so the above code may not work on such hosting. Ask your hosting company if or how it’s possible to send email using the PHP mail() function from your server.

Share this:
  • I experienced that same problem as Jorge did, so thought I’d better share what I experienced and how it got resolved…

    For me, it turned out to be due to me changing the ‘from’ address to an email-address that didn’t exist on the blue-host server. After checking the exact spelling of my email-account and using it, the problem went away.

    In addition, I had a mediawiki installed on my bluehost account, and I also wanted the email-notifications for it to work. It turned out I needed to edit my mediawiki’s “LocalSettings.php” file and update the $wgEmergencyContact and $wgPasswordSender email-addresses to my valid bluehost address too (as the default values were invalid).

  • Another thing to be careful with. If the recipient email address has a domain that matches the website, Bluehost will check if that email address is in the cPanel. If it isn’t, the email fails even if it is a valid recipient. If the recipient email address’s domain does not match the site, it will send the email without issue. I discovered this while using my work email to test email functionality on our work site that did not rely on the From header. Instead I just used the Reply-To header since it does not have to be an address listed in the cPanel even if the domain of the Reply-To email is the same as the site. Once I entered my gmail address, the functionality worked perfectly. Two days hitting my head against a wall just to find out I am not crazy after all.

        • Hi Venu,
          I don’t use Bluehost at the moment so I can’t do a test but other people seem to have recently made the above mail code work.

          I am busy at the moment but when free I would be happy to look at it if you give me FTP access to the server – you can send the details using the contact page now and I will look at it as soon as I can. I need the FTP address, username and password.

          Speak soon and best wishes,


        • Hi, I tried several tricks mentioned here but none worked. What did work was just putting the email without the name. So instead of FROM: name I just put FROM: email Not ideal but it worked/works for me.

          • Thank you for a solution Rodolfo,
            However, I find it a little odd that some people get the code to work as is, some have to make slight tweaks and some cannot get it to work. Perhaps Bluehost server configurations may not all be the same.

  • Try
    $params = ‘-f’ . $emailfrom;

    instead of
    $params = ‘-f ‘ . $emailfrom;

    The space after ‘-f’ was the case of matter for me. I was able to mail only after removing the space.

    • As I do not use Bluehost now, can someone please try Paullus’s suggestion above and let us know if it works for them also so that I can then update the code on the main post above. Thank you.

      • I’m still struggling with this, but on an unrelated note, I noticed that you indicated to several commenters that you no longer use Bluehost. May I ask what you are using now, and is it better than Bluehost? Would really appreciate it if you’d answer this, thanks.

        • I currently use SiteGround for hosting and you can find out about their services at

          I moved my site and client sites from BlueHost as I found their service slow when accessed from the U.K.

          I moved to another U.K. host which was OK for a while but site access seemed to slow down and there was too much downtime for my site and client sites.

          I chose SiteGround previously for a client project and found the support to be very good in general.

          After finishing with the client project, I tried my site with SiteGround but with a different hosting package than the above client and found SiteGround’s hosting to be faster than any other shared hosting that I had previously experienced, and their general support to be much better.

          Access speed and up-time is very good from the U.K. and especially as I chose U.K. data centers for U.K. based organisations and clients.

          SiteGround’s fees are not the cheapest, but as above, their service is better than I have experienced for shared hosting, and their support is great.

          SitegGround have data centers in the U.S., U.K. and Singapore which gives you a good choice for your sites depending on where a site is mostly accessed from and where most access may be from for new sites. You can read more about their data centers here

          I haven’t done any php code development on SiteGround servers but you can test SiteGround’s hosting for php development as they seem to have a 30 day money back guarantee which you can read about on their site.

          I hope that helps.

          • Thanks a lot for the recommendation, I’m taking it for a spin now. I agree, Bluehost’s shared servers are extremely underpowered, but I still think it’s one of the most ‘complete’ hosting services out there, haven’t come across anything that can match it yet.

            And, I finally managed to solve Bluehost’s mailing problem, by the way, turned out that Bluehost enables the “mail.add_x_header” php option, and that exposes the UID as well as filename of the originating script, which is sometimes filtered out by public/commercial servers. It had nothing to do with the code, in fact, even the most basic email header (i.e. with only a “From” parameter) would, otherwise, work just fine.

            I haven’t found any reference about this on the web, not even in Bluehost’s documentation, so, feel free to add it to your article if you’d like.


            • Hi M Rawash, your reply here saved my life. I have been searching for a solution for weeks, and finally saw a sentence from your comment “…as well as filename of the originating script…”. it works after i rename the filename, it’s really because of the filename…OMG!

    • Dax,

      The $toname variable was put in when originally creating the PHP code with a hope that the email received by $emailto could have a friendly name in the recipients inbox.
      I will leave it there for now so that hopefully it can perhaps be integrated and made to appear.

      Thank you,


  • Excellent piece of code! I was having mail() function issues in a php mail form on a Bluehost server and found your code. While it didn’t explicitly fix my problem is does work well.

    I found my issue was with Local Mail Exchanger and Remote Mail Exchanger. Emails being sent to “Google Apps” emails with the same name as the main domain never passed through. Seems obvious in retrospect but switching my configuration from the Local Mail Exchanger to the Remote Mail Exchanger worked just fine.

  • Thank you. This is awesome. I have been fighting with Bluehost for ages.

    Quick Question. Do you know how to extend on this to include a CSV file?

    • Tamer, I do not recall if I had a similar DKIM setup when I found the code to work and my hosting is now with another company so I cannot replicate this set-up. Can anyone with a Bluehost account work with Tamer?

  • <?php

    $emailto = '';
    $toname = 'TO NAME';
    $emailfrom = '';
    $fromname = $_POST['name'];
    $subject = $_POST['need'];
    $messagebody = $_POST['message'];
    $headers =
    'Return-Path: ' . $emailfrom . "\r\n" .
    'From: ' . $fromname . ' ‘ . “\r\n” .
    ‘X-Priority: 3’ . “\r\n” .
    ‘X-Mailer: PHP ‘ . phpversion() . “\r\n” .
    ‘Reply-To: ‘ . $fromname . ‘ ‘ . “\r\n” .
    ‘MIME-Version: 1.0’ . “\r\n” .
    ‘Content-Transfer-Encoding: 8bit’ . “\r\n” .
    ‘Content-Type: text/plain; charset=UTF-8’ . “\r\n”;
    $params = ‘-f ‘ . $emailfrom;
    $test = mail($emailto, $subject, $messagebody, $headers, $params);

    Contact Us

    Select Service
    Building Construction
    Interior Design
    Renovation Work
    Plan & Approval

    Hello Sir, The above code not working if possible pls help me i’m struggling with that past 5 days.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}