Sending mail with PHP, PEAR & Godaddy

I created a very small php file to send email messages which would get called from my app. Everything worked just fine testing locally, then I deployed it to my godaddy account and it wouldn’t work. If you try to goggle specific topics for info on getting something like PEAR PHP mail to work on godaddy you’ll find it hard to come by. Hence this post.

The forum help from godaddy is OK, but more often than not they tell you why you’re having issues. So you still have to figure out how to fix it (which is good in some ways as well – don’t learn unless you figure it out yourself sometimes).

So this is the code that will work when you test locally and as it comes from my machine the mail request gets seen as a client connection (same as say outlook etc) and not as a web server. This is why you need to change the code when you deploy to a web server.

<?php
include('Mail.php');
 
$from = "Mr Smith <smith@blah.com>";
$to = "Mrs Jones <jones@hello.com>";
$subject = "What's up";
$body = "Hi,\n\nHow are you? What is happening";
 
 $host = "smtpout.secureserver.net";
 $username = "username@youraccount.com";
 $password = '************';
 
 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));
 
 $mail = $smtp->send($to, $headers, $body);
 
 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
?>

So here is the code that will work when you deploy it to your server on godaddy, this may well be the same as other server providers.

<?php
include('Mail.php');
 
$from = "Mr Smith <smith@blah.com>";
$to = "Mrs Jones <jones@hello.com>";
$subject = "What's up";
$body = "Hi,\n\nHow are you? What is happening";
 
$headers = array (
    'From' => $from,
    'To' => $to,
    'Subject' => $subject);
 
 $smtp = Mail::factory('smtp' );
 
 $mail = $smtp->send($to, $headers, $body);
 
 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
?>

So as you can see I’ve totally removed the host, username, password. This will work providing you have the following lines inside you php.ini or php5.ini file.


SMTP = relay-hosting.secureserver.net
smtp_port = 25

If you do try and use the first piece of code on the webserver it will warn you about being unable to connect. for example,
Warning: fsockopen() [function.fsockopen]: unable to connect to {some ip address} (Connection refused) in /usr/local/lib/php/Net/Socket.php on line 108
unable to connect to smtp server.

This entry was posted in php and tagged , , , . Bookmark the permalink.

3 Responses to Sending mail with PHP, PEAR & Godaddy

  1. Ron Swartz says:

    Thanks a lot. I have been struggling with this on GoDaddy.

  2. Plinio says:

    Good Help!!! I was trying for two days to figure this out. I read a bunch of replies from goDaddies Tech Help whithout any help… This is exactly what I needed. I tested similar php code on my local network and it worked, but when deploy on GoDaddy server it was useless. Thanks a million….

  3. Eric says:

    Wow. I’ve been struggling with this four a few hours now, you saved the day. Thanks a bunch :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>