Gugols
By:
Gugols

PhpMailer fails with 500 error, can't reach smtp server

August 8, 2015 4.2k views
PHP DigitalOcean Networking

Hi,

I have faced with problem. I use the following code to send mail on development server, but it fails when I try to do the same from Digitalocean server (Ubuntu 12.04.5 x64).

Bascially it shows that script loading but ends up with 500 error.

The connection to smtp.gmail.com fails..:
root@root:~# telnet smtp.gmail.com 587
Trying 2a00:1450:400c:c01::6c...
Trying 74.125.195.108...
Trying 74.125.195.109...

Code:

<?php
error_reporting(-1);
date_default_timezone_set('Etc/UTC');

require 'phpm/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 3;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

$mail->CharSet = 'UTF-8';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "...@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "pass...";

//Set who the message is to be sent from
$mail->setFrom('....@gmail.com', 'Example');

//Set an alternative reply-to address
$mail->addReplyTo('...@gmail.com', 'Example');

//Set who the message is to be sent to
$mail->addAddress('example@example.com', 'Test1');
$mail->addAddress('example2@example.com', 'Test2');

//Set the subject line
$mail->Subject = 'Hi!';

$mail->Body     = "Hi!";

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

//Replace the plain text body with one created manually
$mail->AltBody = 'Hi!';

//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Failed: " . $mail->ErrorInfo;
} else {
    echo "";
}

?>
2 Answers

I got it working for port 25 (wasn't blocked when checked the telnet). Still, strange that others doesn't work.

DigitalOcean blocks SMTP ports on new accounts to cub spam and abuse:

https://www.digitalocean.com/community/questions/smtp-impossible?answer=12486

So you have to create a ticket to get it unblocked.

Have another answer? Share your knowledge.