Error in php Mailer

Asked

Viewed 1,580 times

1

I am 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>";
    }

presents 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]
  • played on my server and did not fail no

  • @Leocaracciolo would like to give me your Whats or skype so we can exchange a better idea

  • From what I remember necessary enable port 587. There are some ways to send email that requires an "authorization" through the email itself. There are cases that the host interferes, try to configure in the panel. Which is the host?

  • @Which host are you using? or is on localhost?

  • 1

    Please do not use Code snippet (Stack Snippets) for PHP, read: http://meta.pt.stackoverflow.com/q/2115/3635

  • @Rafaelgss use onclickhosting but already solved the problem

  • @Wagnerfernandomomesso use onclickhosting and already solved my problem

Show 2 more comments

1 answer

2


This is not a port issue, this is a security certificate issue if your server by default has not configured SSL, mainly from PHP 5.6:

http://php.net/manual/en/migration56.openssl.php

In 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 configure manually through the 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 a important detail, for your error message:

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

You clearly didn’t download the Phpmailer of 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 PHP5.6 and 7 changes, so do not download from random sources, always search for the official one (of course there are exceptions, but always have the risk of not working).

How to resolve SSL issues in Phpmailer

First download the most updated version of Phpmailer, it uses Composer-autoload, but if you download the https://github.com/PHPMailer/PHPMailer/releases/tag/v6.0.1 (most current version so far) and then extract in 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 similar to 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;
}

Adjusting SSL parameters in php.ini

If the upgrade does not solve the problem is because your server is "bad" configured

And check that Openssl is installed and then first try to configure via php.ini the parameters I mentioned to 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 really contact the technical support, but if it can not be done you can try the manual solution using SMTPOptions, it will be necessary to have the ca_cert.pem and in the peer_name configure 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',
    ],
);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.