1

I followed all of the instructions in this question:

SMTP connect() failed PHPmailer - PHP

But still I never succeeded in getting the PHPMailer to work. I searched elsewhere - no solutions.

You can view the results here: https://unidrones.co.za/JuneSecond

When I try to send a test email using my gmail account credentials, it returns the "SMTP connect() failed" error.

I am using this template code:

<?php
require 'PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);
if (!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p id="para">'.$error.'</p>';
}
else {
echo '<p id="para">Message sent!</p>';
}
}
else{
echo '<p id="para">Please enter valid data</p>';
}
?>

Edit: I don't know if there's a new way to send emails through PHP now. All the tutorials and lessons I am using teaches it this way (i.e. using the PHPMailer library).

I had a tough time finding the PHPMailer library that includes the PHPMailerAutoload.php file, which makes me think it's a little outdated or deprecated, but how else would I send emails? I don't know.

Pradeep
  • 9,667
  • 13
  • 27
  • 34
J Doe
  • 121
  • 2
  • 11
  • Add this function to your code . `$mail->SMTPDebug = 2;`. So with using this function you can debug your code and let us know what is the error. More details regarding debug is here :- https://stackoverflow.com/a/2896502/4952944 – Bhavin Jun 02 '18 at 13:52
  • @Bhavin I've added that as well as this code which someone else suggested, but neither worked: $mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); – J Doe Jun 02 '18 at 14:00
  • 2018-06-02 14:02:41 SMTP ERROR: Failed to connect to server: (0) 2018-06-02 14:02:41 SMTP connect() failed. – J Doe Jun 02 '18 at 14:04
  • Are you sure your `->Host` is valid? – Script47 Jun 02 '18 at 14:04
  • @Script47 I'm quite sure yes. I've used both 'ssl://smtp.gmail.com' and 'smtp.gmail.com' but they didn't work. If there is nothing wrong in my code syntax, what else could be the issue? I already allowed less secure apps from my gmail account. Is there someone new or different you have to do? I can't find tutorials giving a complete coverage on the PHPMailer, so I'm quite clueless. – J Doe Jun 02 '18 at 14:10

3 Answers3

1

The reason you're having a hard time finding PHPMailerAutoload.php is because it's old and no longer supported. Get the latest and base your code on the gmail example provided. If you're new to PHP, learn to use composer.

These three lines are conflicting:

$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';

The ssl:// in Host overrides the tls in SMTPSecure, resulting in it trying to use implicit TLS to a port expecting explicit TLS. Either use ssl with port 465 or tls with port 587, not other combos. Regardless, it appears that's not your problem anyway.

As the troubleshooting guide says about this exact "SMTP connect() failed" error:

This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or another issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why.

It then goes on to describe several techniques you can use to try to diagnose exactly why you can't connect. Amazing stuff, documentation.

I tried your form with some random data and saw that you failed to include the most important error message in your question:

2018-06-02 14:47:25 SMTP ERROR: Failed to connect to server: Network is unreachable (101)
2018-06-02 14:47:25 SMTP connect() failed

That suggests your ISP is probably blocking outbound SMTP, so you have your answer - perhaps confirm that using the steps the guide suggests (telnet etc), and refer to your ISP's docs or support.

You also have a major omission - you're not setting a "from" address:

$mail->setFrom('myname@gmail.com', 'My Name');

Note that if you're sending through gmail, you can only use your account's address, or preset aliases (set in gmail prefs), not arbitrary addresses.

Meanwhile, this is a somewhat crazy thing to implement as you have anyway - why would anyone ever enter their gmail credentials on a form like that?

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • A thorough answer, thank you. I will follow the steps you provided and let you know the outcome. (I used the ssl/465 combo and that worked on my localhost, though still wouldn't work on my live server). As for the implementation, of course it's a crazy way to enter gmail credentials on a form this way :) It's only a testing ground for me to send emails back and fourth my own Gmail accounts. I haven't been able to get this mailer thing to work even after hours and hours of research. I'll follow your steps and pray something comes out of it. – J Doe Jun 02 '18 at 20:33
  • 1
    Your answer pointed me in the right direction. I learned about composer, and installed the latest version of PHPMailer using composer. I am now successfully sending emails back and forth my gmail and other mail clients. – J Doe Jun 03 '18 at 17:58
0

did you try to use the following config?

$mail->Host       = 'smtp.gmail.com';
$mail->SMTPSecure = 'ssl';
$mail->Port       = 465;
$mail->CharSet    = 'UTF-8';
-2
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;

//$mail->SMTPDebug = 4;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'your gamil id';                 // SMTP username
$mail->Password = 'gmail password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('set from address', 'name');
$mail->addAddress('recipient address', 'Joe User');     // Add a recipient

$mail->addReplyTo('reply address', 'Information');


// $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
Hari
  • 43
  • 1
  • 9