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 = 'rafawins@gmail.com';
    $mail->Password = '***';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    
    $mail->setFrom('rafawins@gmail.com', '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?

Community
  • 1
  • 1
Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
  • You need to define "not working". Read [the troubleshooting guide](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting), post what you get with `SMTPDebug = 4`. – Synchro Dec 04 '15 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 :) – Rafael Ruiz Muñoz Dec 04 '15 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 04 '15 at 10:29
  • SMTP Ports are blocked by default on OceanDigital, you must open a support ticket to enable SMTP ports. – André Roggeri Campos Dec 04 '15 at 10:39
  • try "tls" to "ssl" some times server need ssl. – Ravi Patel Dec 04 '15 at 11:31
  • @AndréRoggeriCampos it's unlocked for IPv4 as I said. – Rafael Ruiz Muñoz Dec 04 '15 at 12:36
  • @ravipatel I tried as I said – Rafael Ruiz Muñoz Dec 04 '15 at 12:36
  • Ocean blocks ipv4 also. You must request it for the support team. If you already did it, have you tried to telnet the gmail smtp server from your vps ? You can test using the following command: `telnet smtp.gmail.com 587` If everything still ok, please share the debug information... – André Roggeri Campos Dec 04 '15 at 12:45
  • @AndréRoggeriCampos I got that ticket and I was answered that IPv6 is blocked, but not IPv4. That's why I'm writing here :). `telnet smtp.gmail.com 587` works ok – Rafael Ruiz Muñoz Dec 04 '15 at 12:47
  • https://support.google.com/mail/answer/78775?hl=en https://support.google.com/a/answer/176600?hl=en Port 465 (SSL required) Port 587 (TLS required) – Ravi Patel Dec 04 '15 at 14:27
  • @ravipatel not solved :\ – Rafael Ruiz Muñoz Dec 04 '15 at 22:13

3 Answers3

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).

Jamil Farooq
  • 131
  • 2
  • 9
  • 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 – Jamil Farooq Dec 04 '15 at 10:39
  • the same script has been used in an AWS server and it works OK. So the problem is here – Rafael Ruiz Muñoz Dec 04 '15 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? – Jamil Farooq Dec 04 '15 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) – Rafael Ruiz Muñoz Dec 04 '15 at 22:13
  • you were right, I got information here: http://stackoverflow.com/questions/21937586/phpmailer-smtp-error-password-command-failed-when-send-mail-from-my-server and solved my question – Rafael Ruiz Muñoz Dec 05 '15 at 00: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....

Harry baldaniya
  • 167
  • 1
  • 11
-1

Try commenting out line 8.

// $mail->isSMTP();
Durul Dalkanat
  • 7,266
  • 4
  • 35
  • 36