0

I built a droplet with Ubuntu with DigitalOcean and I'm trying to configure it to send emails with SMTP.

I know DigitalOcean blocks SMTP over IPv6 but not over IPv4 so I disabled IPv6 as this post says.

My script still doesn't work. I've tried with ports 25, 465 and 587. TLS and SSL.

I've installed sendmail for Ubuntu 14.04 but not working.

This is my script:

<?php
    require 'mail/PHPMailerAutoload.php';
    
    $mail = new PHPMailer;
    
    $to = $_GET['email'];
    
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = '***';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    
    $mail->setFrom('[email protected]', 'Rafael');
    
    $mail->addAddress($to);
    
    $mail->isHTML(true);
    
    $mail->Subject = 'Subject';
    $contents = ob_get_contents();
    $mail->Body    = "ao!";
    
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
    print_r(error_get_last());
    

?>

The error presented is:

SMTP connect() failed.

I'm interested in sending email using SMTP so ->isSMTP() is required!

Where am I wrong?

Thank you very much.


EDIT:

doing: telnet smtp.gmail.com 587 I get:

Trying 74.125.133.108...

Connected to gmail-smtp-msa.l.google.com.

Escape character is '^]'.

220 smtp.gmail.com ESMTP w6sm13897014wjy.31 - gsmtp

and doing: openssl s_client -connect smtp.gmail.com:465 I get an answer as well...

What's wrong?

11
  • You need to define "not working". Read the troubleshooting guide, post what you get with SMTPDebug = 4.
    – Synchro
    Dec 4, 2015 at 10:17
  • Sorry I updated the error. I forgot to write it! I've been in the troubleshooting guide before. Otherwise I wouldn't have come to StackOverflow :) Dec 4, 2015 at 10:27
  • 1
    So post what you get from debug output, like the guide says - there are over 800 questions on SO about that exact message. It's very, very likely you're running into a problem that is described exactly in the guide; connecting to gmail is pretty messy these days.
    – Synchro
    Dec 4, 2015 at 10:29
  • SMTP Ports are blocked by default on OceanDigital, you must open a support ticket to enable SMTP ports. Dec 4, 2015 at 10:39
  • try "tls" to "ssl" some times server need ssl.
    – Ravi Patel
    Dec 4, 2015 at 11:31

3 Answers 3

2

Beware of using Gmail from different devices. google doesn't allow and block immediately a location from where an account is used where it isn't supposed to use (in the eyes of google ofcourse).

5
  • you can run your code from localhost though. also check your inbox for any notification you might receive regarding suspension of your account for that particular device/ip Dec 4, 2015 at 10:39
  • the same script has been used in an AWS server and it works OK. So the problem is here Dec 4, 2015 at 12:36
  • so you have been using this account from different devices (at different hosting sites) at different geo-locations. have you checked your inbox too? Dec 4, 2015 at 18:25
  • yes. I'm trying with different servers and I can do it perfectly, even from different accounts. But not in this server (DigitalOcean) Dec 4, 2015 at 22:13
  • you were right, I got information here: stackoverflow.com/questions/21937586/… and solved my question Dec 5, 2015 at 0:10
1

On live server for send Mail using SMTP, Do it.

$mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

and Comment $mail->IsSMTP();

It's Working For Me....

1
  • this might be working, but it is then using the default mail function, not the SMTP Server
    – user6057915
    Jul 14, 2019 at 16:05
-1

Try commenting out line 8.

// $mail->isSMTP();
1
  • This will ignore all your SMTP settings and send emails using the webserver. Mar 17, 2017 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.