Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

Capturing MVC PHP view into a variable to pass as body mail in PHPMailer

I'm using a MVC framework and i want to pass a view into PHPMailer body but I can't capture the view into a variable.

Controller handling view looks like this:

// Load View
public function view($view, $data = []){
    // Check for view file
    if(file_exists('../app/views/'.$view.'.php')){
        require_once '../app/views/'.$view.'.php';
    }else{
        // View does not exist
        die('View does not exist');
    }
}

In controller i pass the view into the function sendMail but it first loads it than i get an error file_get_contents(): Filename cannot be empty:

$bodyMail = file_get_contents($this->view('booking/mailbody'));

sendEmail($bookingNumber, $id, $mailAddress, $bodyMail);

The helper function to handle PHPMailer function:

function sendEmail($bookingNumber, $id, $mailAddress, $bodyMail){

$mail = new PHPMailer(true);

//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "*********";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "**********";
$mail->Password = "********";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;

$mail->From = "********";
$mail->FromName = "*******";

$mail->addAddress($mailAddress);
$mail->addAddress('*********');

$mail->isHTML(true);

$mail->Subject = "Reservation confirmed: ".$bookingNumber.$id."";
$mail->Body = $bodyMail;
$mail->AltBody = "This is plain text of mail";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}

My question is there's a way to capture the view into a variable? Would you approach this in a different way? If so, can you guys give me the right direction to take?

question from:https://stackoverflow.com/questions/65626728/capturing-mvc-php-view-into-a-variable-to-pass-as-body-mail-in-phpmailer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As per my comment, I suggest using output buffering to capture the output of your view

https://www.php.net/manual/en/function.ob-get-clean.php

And as @synchro suggest, review your usage of require_once()

// Load View, but capture it with output buffering
public function viewBuffered($view, $data = []){
    // Check for view file
    if(file_exists('../app/views/'.$view.'.php')){
        ob_start();
        require_once '../app/views/'.$view.'.php';
        return ob_get_clean();
    }else{
        // View does not exist
        die('View does not exist');
    }
}

-or-

// Load View, but capture it with output buffering
public function viewBuffered($view, $data = []){
    ob_start();
    $this->view($view,$data);
    return ob_get_clean();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...