Tuesday, April 3, 2012

Get PHPMailer to work for GoDaddy Shared Hosting

I inherited a GoDaddy website that is setup as, I think, a shared hosting environment. With this setup, it seems you are somewhat limited in many ways including how to send emails.

The website I was working with was using the default no authentication relay email service. You simply submit the command like this:

mail($email, $subject, $sub);

add this to your php.ini file: SMTP: SMTP = relay-hosting.secureserver.net

And it magically sends the email out for you. It works but it does seem to be rather slow and the other downfall is that the "To:" looks like this on the receiver's end:

MY NAME via p444nlh038.xxx.bbbb.xxxx.secureserver.net

Many of the recipients of the emails could not receive this message. Maybe their emails marked this as SPAM or whatever. I could tell the users to dig into the SPAM mark it not as SPAM and then add that weird email address as a known contact...but that seems too hard and it just does not look right (that email).

In and out of months, I hoped and tried to setup the email to work with GMAIL. So many postings lead me to believe I could. And it may be possible in a non-shared environment on GoDaddy. Ultimately, I found that IT WAS NOT POSSIBLE with all the knowledge I found to do this setup: PHPMailer, GoDaddy, Gmail, and Shared hosting. I could run emails all day locally, but when I would move it to the server it would not work.

What I did find that turned out to be an acceptable solution was to use the email account provided by GoDaddy that used the domain name in the email like 'info@junk.com'.

Some yahoo who took care of the site before me had setup the MX records to point to Google and then took off leaving no evidence of the login info for that account. So I had to revert the MX records to GoDadddy, wait and hour...and the press on.

1. Modify php.ini: SMTP = relay-hosting.secureserver.net

2. Download PHPMailer. I don't know exactly where the "good" download was that I got. The first download I tried named the main file as "phpmailer.inc.php" which I found had a single syntax error. And never seemed to work right. So luckily I found another download that names the main file "class.phpmailer.php". This one worked better with a couple of well-documented hacks that I applied.

3. Apply Hacks:

Around line 104 in the class.smtp.php file add this:

$host = "relay-hosting.secureserver.net";

under the comment: #connect to the smtp server

Around line 1036 in the same file,

Replace this line:

while($str = fgets($this->smtp_conn,515)) {

with this:

while($str = @fgets($this->smtp_conn,515)){

4. Setup your test:


include('phpmailer/class.phpmailer.php');

$mail = new PHPMailer ();

$mail -> From = "junk@junk.com";
$mail -> FromName = "JUNK";
$mail -> AddAddress ("junk2@junk.com");
$mail -> Subject = "Test";
$mail -> Body = "

From Me!

";
$mail -> IsHTML (true);

$mail->IsSMTP();

$mail->Host = 'hosting';
$mail->Username = 'junk@junk.com';
$mail->Password = '***';

if(!$mail->Send())
echo "Error:" . $mail->ErrorInfo;
else
echo "Success.";

?>

2 comments: