-1

I don't know why I get this error PHPMailer Mail Error - >SMTP connect(). How can I solve it? I don't have any idea how can I do I need exactly explications, I'm new with PHP

<?php
    require '../plugins/phpmailer/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail->CharSet = "utf-8";
    $mail->IsSMTP();
    $mail->SMTPDebug = 1;
    $mail->SMTPAuth = true;
    $mail->Username = "myemail@gmail.com";
    $mail->Password = "mypass";
    $mail->SMTPSecure = "ssl";
    $mail->Host = "smtp.gmail.com";
    $mail->Port = "587";

    $mail->setFrom('your_gmail@gmail.com', 'your name');
    $mail->AddAddress('to_mail@mail.com', 'receivers name');

    $mail->Subject = 'using PHPMailer';
    $mail->IsHTML(true);
    $mail->Body = 'Hi there ,
                            <br />
                            this mail was sent using PHPMailer...
                            <br />
                            cheers... :)';

    if ($mail->Send()) {
        echo "Message was Successfully Send :)";
    } else {
        echo "Mail Error - >" . $mail->ErrorInfo;
    }
    ?>
Pides1937
  • 3
  • 1
  • 2
  • I would expect it is because your username/password is not correct OR perhaps Gmail are not prepared to allow you to connect to relay mail. Does the error give you any more details? If not you might want to hunt down that error in the code to find out what is going on – Paul Coldrey Sep 12 '16 at 07:33
  • Username and password it's ok, I get mail from gmail about the security. – Pides1937 Sep 12 '16 at 07:34
  • That's my complete error :2016-09-12 07:35:18 SMTP ERROR: Failed to connect to server: (0) 2016-09-12 07:35:18 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Mail Error - >SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting – Pides1937 Sep 12 '16 at 07:35
  • There are issues with relaying mail off other people's servers. It is not unlikely that GMail does not allow this by default. Hence I would look for articles on using PHPMailer with GMail like this: http://phpmailer.worxware.com/?pg=examplebgmail – Paul Coldrey Sep 12 '16 at 07:37
  • Actually - noone really support SSL anymore - change the SMTPSecure to TLS. – Paul Coldrey Sep 12 '16 at 07:38
  • It does help if you read what the error actually says and follow the URL it provides; that's why it's there! – Synchro Sep 12 '16 at 07:55
  • Possible duplicate of [PHPMailer: SMTP Error: Could not connect to SMTP host](http://stackoverflow.com/questions/3477766/phpmailer-smtp-error-could-not-connect-to-smtp-host) – Synchro Sep 12 '16 at 07:55

2 Answers2

1

Error due to SMTP connection failed.So, Check your configuration first, you can check by comment the line $mail->IsSMTP();

// $mail->IsSMTP();

See below is working demo :

  <?php
   require 'phpmailer.php';
   require 'smtp.php';
   $mail = new PHPMailer;
  //$mail->IsSMTP(); // telling the class to use SMTP
   $mail->Host       = "smtp.gmail.com"; // SMTP server
   $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                       // 1 = errors and messages
                                       // 2 = messages only
   $mail->SMTPAuth   = true;                  // enable SMTP authentication
   $mail->Host       = "smtp.gmail.com"; // sets the SMTP server
   $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
   $mail->Username   = GMAIL EMAIL ID; // SMTP account username
   $mail->Password   = GMAIL PASSWORD;        // SMTP account password
   $mail->SMTPSecure = 'ssl';


   $mail->From = 'from@example.com';
   $mail->FromName = 'Mailer';
   $mail->addAddress('MAIL ID to whom you eant to send');               // Name is optional

  $mail->addCC('CC EMAIL ID');
  $mail->addBCC('BCC EMAIL ID');
  $mail->WordWrap = 50;                                 // Set word wrap to    50 characters

  $mail->Subject = 'Here is the subject';
  $mail->Body    = 'MESSAGE';
  $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';
 }
    ?>
  • I dont' have the file 'smtp.php' I just have the PHPMailerAutoload.php – Pides1937 Sep 12 '16 at 08:15
  • I've commented the isSMTP line and the message was: Message was Successfully Send :) ... But I don't have anything in inbox – Pides1937 Sep 12 '16 at 08:17
  • So may be that file has smtp class and its methods so no need to add smtp.php. – Vishal Panchal Sep 12 '16 at 08:21
  • Did you make change in php.ini related to ssl? if not than configure it as below: If not yet, try this: 1. Open xampp->php->php.ini 2. Search for extension=php_openssl.dll 3. The initial will look like this ;extension=php_openssl.dll 4. Remove the ';' and it will look like this extension=php_openssl.dll 5. If you can't find the extension=php_openssl.dll, add this line extension=php_openssl.dll. 6. Then restart your Xampp. – Vishal Panchal Sep 12 '16 at 08:24
  • I'm working on the server, direct on my domain. How can i modify the php.ini here? – Pides1937 Sep 12 '16 at 08:30
  • So, you have private server means VPS? if it is a private server then you can find php.ini into your installation directory PHP. But if it is not then contact to your sevice provider to verify ssl and smtp enable or not? – Vishal Panchal Sep 12 '16 at 08:33
  • I'm working just with the files from the public_html...host and domain – Pides1937 Sep 12 '16 at 08:35
  • Using phpinfo(); I found this location /opt/alt/php70/etc/php.ini but I have no idea where is. – Pides1937 Sep 12 '16 at 08:42
  • so it means you have just hosting detail not vps. so you have to contact your service provider to verify. – Vishal Panchal Sep 12 '16 at 09:34
0

Change your line:

$mail->SMTPSecure = "ssl";

To:

$mail->SMTPSecure = "tls";

A lot of mail servers don't allow SSL anymore as it has some security issues.

Paul Coldrey
  • 1,389
  • 11
  • 20
  • got new errors: 2016-09-12 07:43:54 CLIENT -> SERVER: EHLO occ.webpage.com 2016-09-12 07:43:54 CLIENT -> SERVER: STARTTLS 2016-09-12 07:43:55 CLIENT -> SERVER: EHLO occ.diosoftmobile.com 2016-09-12 07:43:55 CLIENT -> SERVER: AUTH LOGIN 2016 CLIENT -> SERVER: YmFjaXUuYWFsZXhhbmRt 2016-09-12 07:43:55 CLIENT -> SERVER: cnVwdG9TA= 2016-09-12 07:43:55 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials o5sm16511065wmg.16 - gsmtp MTP Error: Could not authenticate. CLIENT -> SERVER: QUIT – Pides1937 Sep 12 '16 at 07:46
  • Your advice is correct, but the reason is not - SMTPS (SMTP over SSL) has been deprecated since 1998, and it's only still around because Microsoft chose to ignore the deprecation. It was replaced by SMTP+STARTTLS in the same year. There is no difference in the security as internally they both use exactly the same encryption. – Synchro Sep 12 '16 at 07:53
  • @Pides1937 - so how about you do what it says? – Synchro Sep 12 '16 at 07:54
  • in my code I added another i've changed the ssl connection to tls – Pides1937 Sep 12 '16 at 07:59
  • @Synchro - yes & no. Even though the underlying encryption is the same the negotiation is different which makes TLS far more secure: https://luxsci.com/blog/ssl-versus-tls-whats-the-difference.html – Paul Coldrey Sep 13 '16 at 07:39
  • From the article: "Both methods of connection (implicit and explicit) result in equally secure communications.". I am not talking about SSL as in SSLv3 (which is indeed less secure), but SMTPS vs SMTP+STARTTLS, i.e. implicit vs explicit, which is what is applicable here. – Synchro Sep 13 '16 at 07:44
  • SMTPS with SSL was deprecated because it was insecure as I said. There IS a difference in security DESPITE the fact they use the same encryption. But either way it is irrelevant to the question. He needed to change SSL to be TLS to make it work because SSL is not longer support (due to security issues). All facts and all not particularly relevant but thanks for piping up with your interesting (but irrelevant) history of when things were deprecated. – Paul Coldrey Sep 13 '16 at 07:50
  • Nope. SSL *is* supported on gmail (and hotmail and Yahoo, etc), purely because Microsoft still insists on using it in Outlook. You just need to match SMTPS with port 465, SMTP+STARTTLS with port 587. – Synchro Sep 13 '16 at 07:56