Error sending mail with PHPMailer

2

Good morning everyone.

I am presented with the following error:

  

Uncaught Error: Call to a member function isSMTP ()

When I try to send an automatic email. I have reviewed in many forums and I have not been able to find the problem, I hope you can help me.

My code:

function enviarEmail($email, $nombre, $asunto, $cuerpo){

        include "PHPMailer/PHPMailer.php";
        include "PHPMailer/SMTP.php";

        //$mail-> SMTPDebug = 2;
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl'; //Modificado Brian 
        $mail->Host = 'smtp.gmail.com'; //Modificado Brian
        $mail->Port = 465; //Modificado

        $mail->Username = '[email protected]'; //Modificado Brian
        $mail->Password = 'xxxx'; //Modificado Brian

        $mail->setFrom('xxxx@gmail', 'Sistemas de Gestion'); //Modificado
        $mail->addAddress($email, $nombre);

        $mail->Subject = $asunto;
        $mail->Body    = $cuerpo;
        $mail->IsHTML(true);

        if($mail->send())
        return true;
        else
        return false;
    }

Thank you very much!

    
asked by Brian Velez 09.08.2018 в 17:26
source

2 answers

2

You have to instantiate the PHPMailer class in a $mail object in order to use the isSMTP() method.

function enviarEmail($email, $nombre, $asunto, $cuerpo){
    include "PHPMailer/PHPMailer.php";
    include "PHPMailer/SMTP.php";
    $mail = new PHPMailer(); // <----------- Debes agregar esta línea
    // el resto de tu código...
    
answered by 09.08.2018 / 17:33
source
0

This is my configuration I hope it serves you it is not necessary that you include "PHPMailer / SMTP.php"; and you need to instantiate the PHPMAILER.

include "PHPMailer/PHPMailer.php";
$mail = new PHPMailer();
// echo "Instaciamos PHPMailer\n";

#configuracion de envio de correos
$mail->isSMTP();
$mail->Host       = "smtp.gmail.com";
$mail->Port       = 465;
$mail->SMTPAuth   = true;
$mail->SMTPSecure = 'ssl';
$mail->Username   = "[email protected]";
$mail->Password   = "xxxxxx";

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
));

$mail->setFrom('[email protected]', "EJEMPLO");  
$mail->isHTML(true);
$mail->AddAddress($email);
$mail->Subject = utf8_decode($subject);
$mail->Body = $body;
//Definimos AltBody por si el destinatario del correo no admite email con formato html 
$mail->AltBody = $body;
$exito = $mail->Send();
    
answered by 09.08.2018 в 17:36