Error in php mailer

1

I'm using the phpmailer function, follow the settings:

$file  = str_replace($comacento, $acentohtml, $file);
    require_once('../phpmailer/class.phpmailer.php');
    require_once('../phpmailer/class.pop3.php'); // required for POP before SMTP
    require_once('../phpmailer/class.smtp.php');
    $mail = new PHPMailer(); // the true param means it will throw exceptions on errors, which we need to catch

    $mail->CharSet = 'UTF-8';
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "mail.catalogovirtualnossacasa.com.br"; // SMTP server
    $mail->SMTPSecure = "tls"; 
    $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                               // 1 = errors and messages
                                               // 2 = messages only
    $mail->SMPTAuth = true;                     // enable SMTP authentication
    $mail->Port       = 587;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "[email protected]"; // SMTP account username
    $mail->Password   = "******";        // SMTP account password

    $mail->SetFrom($_POST['email'], $_POST['nome']);

    $mail->AddReplyTo($_POST['email'], $_POST['nome']);

    $mail->Subject = 'loja '.$_POST['email'].' & Grupo Nossa Casa ';

    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

    $mail->MsgHTML($file);
    $mail->isHTML(true); 

    $mail->AddAddress($_POST['email'], "Compra - Grupo Nossa Casa");
    if($mail->Send()){
        echo "
        <script type=\"text/javascript\">
            alert(\"ok!\");
        </script>";
    }
    else{
        echo "
        <script type=\"text/javascript\">
            alert(\"deu ruim\");
        </script>";
    }

displays the following error:

Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in /var/sentora/hostdata/gnc/public_html/adm_catalogovirtualnossacasa_com_br/phpmailer/class.smtp.php on line 248
SMTP -> ERROR: RSET failed: KGA�rh��    OkA4O��#��:8f�F,ol� Xe[�q�����Q�*&p�z��n�DL��͸03l��cZ���q_�G�_���i��2rJ�v.�A�WV��hR�^�3g7~k"+A�&���>Jx���'ߦ̈y�/�Q����J�s����1-{o�����+#�6M� �C�pKſ4�*3@Y�M1l�jJ@@!'�L9�q��\~���s<�;��[�u�Q����l���'I\"�J�d}]����@�q�Q�'�>���=��<�τ�I5kVe� ����cu�   ;����y?B�ל �/L&���W��%:�����[S 
SMTP -> NOTICE: EOF caught while checking if connectedThe following From address failed: [email protected]
    
asked by anonymous 30.09.2017 / 17:45

1 answer

2

This is not a problem with ports, this is a problem with security certificates, if your server by default has not configured SSL, especially from PHP 5.6 strong>:

  

link

In your php.ini, one of the following fields should not be configured:

  • openssl.cafile=
  • openssl.capath=

Or your server does not have OpenSSL installed, however it is possible to manually configure it through stream_context_create , for example:

$context = stream_context_create(array(
    'ssl' => array(
        'verify_peer'   => true,
        'verify_depth'  => 5,
        'peer_name'     => 'HOST que está tentando acessar'
    );
));

And an important detail , for your error message:

  

... phpmailer / class.smtp.php on line 248

You have clearly not downloaded the PHPMailer from the official website maintained, because in the current summers the names of the scripts are different, see the official repository:

This is probably more compatible with changes in PHP5.6 and 7, so do not download from random sources, always look for the official one (of course there are exceptions, but it always runs the risk of not working).

How to troubleshoot SSL in PHPMailer

First download the most up-to-date version of PHPMailer, it uses composer-autoload, but if you download the link (most current version so far) and then extract into your folder your script should look like this:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once '../PHPMailer/src/Exception.php';
require_once '../PHPMailer/src/PHPMailer.php';
require_once '../PHPMailer/src/SMTP.php';

... Resto do arquivo que vem antes do seu str_replace deve ficar AQUI!!! ...

$file  = str_replace($comacento, $acentohtml, $file);

...

And then adjust your script to something like this and test:

$mail = new PHPMailer(true);                              // Passing 'true' enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.foobar.com';                     // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, 'ssl' also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
    $mail->addAddress('[email protected]');               // Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $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';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

Setting SSL Parameters in php.ini

If the upgrade does not solve the problem, it is because your server is "badly" configured

And check that OpenSSL is installed and then first try configuring via php.ini the parameters that I quoted for you according to your OpenSSL:

  • openssl.cafile=
  • openssl.capath=

Configuring SSL directly in PHPMailer

Now if you do not have access to the server I recommend that you actually contact technical support, but if you can not do this you can try the manual solution, using SMTPOptions , you will need ca_cert.pem and no peer_name set the SMTP HOST path:

$mail = new PHPMailer(true);

$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.foobar.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->SMTPOptions = array(
    'ssl' => [
        'verify_peer' => true,
        'verify_depth' => 3,
        'allow_self_signed' => true,
        'peer_name' => $mail->Host, //Mesmo endereço do HOST
        'cafile' => '/etc/ssl/ca_cert.pem',
    ],
);
    
01.10.2017 / 00:06