PHP Mail Function with Bluehost, Working

Posted on 24 June 2013 in Blog Posts
Comments: 44

For the past few days 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:

<?php
$emailto = 'to@domain.com';
$emailfrom = 'from@domain.com';
$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.

<?php
$emailto = 'to@domain.com';
$toname = 'TO NAME';
$emailfrom = 'from@domain.com';
$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
?>

 

Share this:

Tags


Comment, ask or share something...

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

    $emailto = 'kkrishnance007@gmail.com';
    $toname = 'TO NAME';
    $emailfrom = 'krishnanbkcl@gmail.com';
    $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.

    Reply

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

      Reply

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

    Reply

    1. Hello Andrew,
      Thank you for the appreciation.
      With respect to including a CSV file, can you please elaborate on your question?
      Bharat

      Reply

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

    Reply

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

      Bharat

      Reply

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

    Reply

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

      Reply

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

        Reply

        1. I currently use SiteGround for hosting and you can find out about their services at https://www.siteground.com/go/bkwebhosting

          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 https://www.siteground.com/go/bkdatacenters.

          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.

          Reply

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

            Cheers.

            Reply

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

              Reply

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

          Bharat

          Reply

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

          Reply

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

            Reply

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

    Reply

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

    Reply

    1. Ashok,

      Try the above code as it has worked for myself, and as you can see from the comments above it has worked for others on Bluehost.

      Best wishes,

      Bharat.

      Reply

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
Do NOT follow this link or you will be banned from the site!