Sending mail with PHP, PEAR & Godaddy

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.

";
$to = "Mrs Jones ";
$subject = "What's up";
$body = "Hi,nnHow are you? What is happening";
 
 $host = "smtpout.secureserver.net";
 $username = "[email protected]";
 $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("

" . $mail->getMessage() . "

"); } else { echo("

Message successfully sent!

"); } ?>

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.

";
$to = "Mrs Jones ";
$subject = "What's up";
$body = "Hi,nnHow 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("

" . $mail->getMessage() . "

"); } else { echo("

Message successfully sent!

"); } ?>

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.

[ad name=”ad-1″]

4 Replies to “Sending mail with PHP, PEAR & Godaddy”

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

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

  3. I have been trying to get it to work. Do i need to install anything on Godaddy before this works ?

    I litterly used you code, (with my own details ofc) but without succes:
    Fatal error: Class ‘Mail’ not found in /home/******/public_html/register.php

Leave a Reply

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

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.