1

im trying to send an email just when somebody enters on a page. It's for paypal payment confirmation. You pay something and in the page where you see what you did an email is send automatically.

The code is the next:

 function send_email($from, $to, $subject, $nombre,$apellido) {          

        $pagoReal = $_SESSION["Payment_Amount"];
        $monedaReal =  $_SESSION["currencyCodeType"];
        $estado = $_SESSION['estado'];
        $id = $_SESSION['idHash'];



        $mail = new PHPMailer();
        $mail->SMTPDebug=3;
        $mail->IsSMTP();
        $mail->Host = 'localhost';
        $mail->CharSet = "UTF-8";

        $mail->From = $from;
        $mail->FromName = 'Notificación de pago via Paypal';
        $mail->addAddress($to);

        $mail->WordWrap = 50;
        $mail->IsHTML(true);
         $mail->SMTPAuth = true;



        $contenido = "<html><body>
                    <p>Han realizado un nuevo ingreso via Paypal</p><br>
                    Nombre del cliente: $nombre $apellido<br>
                    Cantidad que pagó: $pagoReal $monedaReal <br>
                    Estado de la reserva:$estado  <br>
                    Enlace a la factura : href='xxxxx/$id<br>
                    </body></html>";


        $mail->Subject = $subject;
        $mail->Body = $contenido;

       ['tmp_name'],$_FILES['cv_contacto']['name']);



        if (!$mail->Send()) {
            echo 'Error enviando mensaje.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;

            return "Mailer Error: " . $mail->ErrorInfo;
        } else {
            return 1;
        }

    }

then i call the function

    send_email('[email protected]', '[email protected]','Payment of '. $firstName." ".$lastName,$firstName,$lastName);

And I get this error

Error: authentication failed: generic failure 2016-07-11 14:20:51 SMTP ERROR: Password command failed: 535 5.7.8 Error: authentication failed: generic failure 2016-07-11 14:20:51 CLIENT -> SERVER: QUIT 2016-07-11 14:20:51 SERVER -> CLIENT: 221 2.0.0 Bye 2016-07-11 14:20:51 Connection: closed 2016-07-11 14:20:51 SMTP connect() failed. Error enviando mensaje.Mailer Error: SMTP connect() failed.

Any idea of what can i do?

Thanks!

4
  • 2
    Try $mail->SMTPAuth = false; instead of true if the local server will let you relay mail.
    – drew010
    Commented Jul 11, 2016 at 14:28
  • I'm glad you fixed this, but you're using an old version of PHPMailer and you've based your code on an obsolete example. Get the latest.
    – Synchro
    Commented Jul 12, 2016 at 13:12
  • 1
    Thank you @drew010, you saved my day! Commented Apr 25, 2018 at 15:24
  • Easy peasy, it works for me too, @drew010 Commented Nov 20, 2019 at 17:12

3 Answers 3

4

Follow this steps.

Use an App Password: If you use 2-Step Verification, try signing in with an App Password.

Allow less secure apps: If you don't use 2-Step Verification, you might need to allow less secure apps to access your account.

If you recently changed your Gmail password, you might need to re-enter your Gmail account information or completely repeat your Gmail account setup on your other email client. If the tips above didn't help, visit https://www.google.com/accounts/DisplayUnlockCaptcha and follow the steps on the page. If you use Gmail through your work, school, or other organization, visit https://www.google.com/a/yourdomain.com/UnlockCaptcha and replace yourdomain.com with your domain name.

0

I have resolved this issue :

Please follow the below steps :

1) Firstly download latest PHPmailer files. https://github.com/PHPMailer/PHPMailer

a) Check its there at all (on terminal )

  ping smtp.gmail.com

  This should give you something like this:

    Trying 173.194.67.109...
    Connected to gmail-smtp-msa.l.google.com.
    Escape character is '^]'.
    220 mx.google.com ESMTP ex2sm16805587wjd.30 - gsmtp

b) openssl s_client -starttls smtp -crlf -connect smtp.gmail.com:587

    You should expect a response like this:

    Start Time: 1460541074
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
    ---
    250 SMTPUTF8

    Notice that the verify return code is 0, which indicates successful verification. 

    refer link : https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

2) Google now doesn't accept login from less secure apps.
Open link : https://myaccount.google.com/security

     Firstly login your gmail account.

     a)Scroll to the bottom and turn ON "Allow less secure apps: ON". 

     b)Now when you add the SMTP details to "Send as" google will accept them.  
     c)You need to do this for the email ID you are adding in your Send as section.

3) Script code :

            require 'PHPMailerAutoload.php';


            $mail = new PHPMailer();

            $mail->isSMTP();                       // telling the class to use SMTP
            $mail->SMTPDebug = 2;                  
            // 0 = no output, 1 = errors and messages, 2 = messages only.

            $mail->SMTPAuth = true;                // enable SMTP authentication
            $mail->SMTPSecure = "tls";              // sets the prefix to the servier
            $mail->Host = "smtp.gmail.com";        // sets Gmail as the SMTP server
            $mail->Port = 587;                     // set the SMTP port for the GMAIL

            $mail->Username = "[email protected]";  // Gmail username
            $mail->Password = "********";      // Gmail password

            $mail->CharSet = 'windows-1250';
            $mail->SetFrom ('[email protected]'); // send to mail
            $mail->AddBCC ( '[email protected]'); // send to mail
            $mail->Subject = $subject;
            $mail->ContentType = 'text/plain';
            $mail->isHTML(false);

            $body_of_your_email ="Hello Pradeep";
            $mail->Body = $body_of_your_email; 
            // you may also use $mail->Body =       file_get_contents('your_mail_template.html');
            $mail->AddAddress ('[email protected]', 'Recipients Name');     
            // you may also use this format $mail->AddAddress ($recipient);

           if(!$mail->Send())
           {
              echo   $error_message = "Mailer Error: " . $mail->ErrorInfo;
            } else 
           {
           echo   $error_message = "Successfully sent!";
           }

I hope its working fine.

Thanks

0

I was trying doing a lot of tests (hours and hours...), but the best configuration is: - "Allow less secure apps: ON". Sometimes this change will take effect after 10 minutes. - Use SSL configuration and port 465, LOGIN. - It´s better if you can activate the debbuging messages to know other details. - Some SMTP like gmail, requires to configure the IP address to accept the verification process (https://support.google.com/accounts/answer/6010255) - All this configurations also will be required in the Admin Panel on GSuite in case that you use a bussiness edition.

Regards, hope helps

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