将 MVC PHP 视图捕获到变量中以作为正文邮件传递到 PHPMailer
Capturing MVC PHP view into a variable to pass as body mail in PHPMailer
我正在使用 MVC 框架,我想将视图传递到 PHPMailer 正文中,但我无法将视图捕获到变量中。
控制器处理视图如下所示:
// 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');
}
}
在控制器中,我将视图传递给函数 sendMail,但它首先加载它,然后我得到一个错误 file_get_contents():文件名不能为空:
$bodyMail = file_get_contents($this->view('booking/mailbody'));
sendEmail($bookingNumber, $id, $mailAddress, $bodyMail);
处理PHPMailer函数的辅助函数:
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;
}
我的问题是有没有办法将视图捕获到变量中?
你会以不同的方式处理这个问题吗?如果是这样,你们能告诉我正确的方向吗?
根据我的评论,我建议使用输出缓冲来捕获视图的输出
https://www.php.net/manual/en/function.ob-get-clean.php
并且正如@synchro 所建议的那样,检查您对 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');
}
}
-或-
// Load View, but capture it with output buffering
public function viewBuffered($view, $data = []){
ob_start();
$this->view($view,$data);
return ob_get_clean();
}
我正在使用 MVC 框架,我想将视图传递到 PHPMailer 正文中,但我无法将视图捕获到变量中。
控制器处理视图如下所示:
// 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');
}
}
在控制器中,我将视图传递给函数 sendMail,但它首先加载它,然后我得到一个错误 file_get_contents():文件名不能为空:
$bodyMail = file_get_contents($this->view('booking/mailbody'));
sendEmail($bookingNumber, $id, $mailAddress, $bodyMail);
处理PHPMailer函数的辅助函数:
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;
}
我的问题是有没有办法将视图捕获到变量中? 你会以不同的方式处理这个问题吗?如果是这样,你们能告诉我正确的方向吗?
根据我的评论,我建议使用输出缓冲来捕获视图的输出
https://www.php.net/manual/en/function.ob-get-clean.php
并且正如@synchro 所建议的那样,检查您对 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');
}
}
-或-
// Load View, but capture it with output buffering
public function viewBuffered($view, $data = []){
ob_start();
$this->view($view,$data);
return ob_get_clean();
}