Report this

What is the reason for this report?

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

Posted on August 8, 2015

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 "";
}

?>


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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.

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

Hey all,

In case anyone accidently opens this topic, I want to give an updated answer.

By default port 25 is being blocked by DigitalOcean on new accounts to try and reduce span that is going out of the Droplets. As such, using port 465 or 587 for SSL/TLS connections would be the go to option.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.