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

Posted August 8, 2015 14.7k views


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 fails..:
root@root:~# telnet 587
Trying 2a00:1450:400c:c01::6c…



require 'phpm/PHPMailerAutoload.php';

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

//Tell PHPMailer to use SMTP

//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 = '';

//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 = "";

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

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

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

//Set who the message is to be sent to
$mail->addAddress('', 'Test1');
$mail->addAddress('', '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

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


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

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

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

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