Run a PHP file from a public function

0

I have these two files. One is a function that inserts data in the base, the other sends an email using php mailer. Function that inserts:

<?php

class MvcBug{


public function nuevoReporteBugController($tabla, $redireccionamiento){

    if (isset($_POST["bugReport"])) {
        $dato = $_POST["bugReport"];
        $dato = trim($dato);
        $respuesta = MvcControllerBug::nuevoReporteBugModel($dato, $tabla, $redireccionamiento, $_SESSION["idUsuario"]);
        unset($_POST['bugReport']);
    }
}

}

And the email output:

<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'mail/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 25;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "············";
$mail->Password = "············";
$mail->setFrom('...............', '...........');
$mail->addReplyTo('[email protected]', 'Base');
$mail->addAddress("..........", "...........");

$mail->Subject = "Prueba";
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
$mail->AltBody = 'This is a plain-text message body';
$mail->send();
?>

Both archives files work separately when executing them, as I could do so that at the moment the function inserts the data and obtains the $ answer, the file of the email is executed so that besides inserting, it notifies me by email that this change was Done?

    
asked by Juan 12.06.2018 в 04:16
source

1 answer

1

You could send the mail from another method

<?php
require_once 'mail/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;

class MvcBug{


    public function nuevoReporteBugController($tabla, $redireccionamiento){

        if (isset($_POST["bugReport"])) {
            $dato = $_POST["bugReport"];
            $dato = trim($dato);
            unset($_POST['bugReport']);
            try {
                $respuesta = MvcControllerBug::nuevoReporteBugModel($dato, $tabla, $redireccionamiento, $_SESSION["idUsuario"]);
                $this->_enviarMail();
            } catch (\Exception $e {
              echo 'No se pudo insertar el registro' . $e->getMessage();
            }
        }
    }

    private function _enviarMail() {
        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->SMTPDebug = 2;
        $mail->Host = 'smtp.gmail.com';
        $mail->Port = 25;
        $mail->SMTPSecure = 'tls';
        $mail->SMTPAuth = true;
        $mail->Username = "············";
        $mail->Password = "············";
        $mail->setFrom('...............', '...........');
        $mail->addReplyTo('[email protected]', 'Base');
        $mail->addAddress("..........", "...........");

        $mail->Subject = "Prueba";
        $mail->msgHTML(file_get_contents('contents.html'), __DIR__);
        $mail->AltBody = 'This is a plain-text message body';
        $mail->send();
    }
}

If you notice, I also wrap the insertion in a try / catch block to avoid sending the mail if the insert fails. In addition, you should check if the mail was sent, I imagine that capturing the output of $mail->send() , but I do not know if PHPMailer throws an exception or returns an error message when it fails to send.

On the other hand, your code does not show where you get MvcControllerBug . If you are using composer (and you should) then the first line should call vendor/autoload.php instead of using the autoloader of PHPMailer which is only a one-time substitute. That way, if MvcControllerBug is in the same namespace as MvcBug you would not need to include the code in the class that you show us.

    
answered by 12.06.2018 / 14:01
source