如何在yii-framweork中使用phpmailer从函数发送附件



我正在使用Yii-framwework和phpmailer向特定的使用者我的邮件功能正常工作,但我被卡住了部分原因是我正在从一个函数生成pdf。

具有生成pdf数据的功能将是动态的:

public function generatePdf()
{
$html = '';
$html .= '<div></div>'; //and same like this all html part.
echo $html;
//mpdf stuff will come here.
$mpdf->WriteHTML($stylesheet,1);  //             
$mpdf->WriteHTML($html,2); 
$mpdf->Output($path.$file_name, "F");
}

此函数可以正常生成pdf。但是现在我想用同样的功能用邮件发送附件。我不知道如何做到这一点。请帮我一下。

我有另一个发送邮件的功能:

public function sentMail(){
$mail = new PHPMailer();  // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->SMTPAutoTLS = false;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = GUSER;  
$mail->Password = GPWD;           
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo; 
return false;
} else {
$error = 'Message sent!';
return true;
}  // demo code
}

如何在我的sentMail((中使用generatePdf函数作为附件。

public function generatePdf()
{
$html = '';
$html .= '<div></div>'; //and same like this all html part.
echo $html;
//mpdf stuff will come here.
$mpdf->WriteHTML($stylesheet,1);  //             
$mpdf->WriteHTML($html,2); 
$mpdf->Output($path.$file_name, "F");
return $path.$file_name;
}

public function sentMail(){
$mail = new PHPMailer();  // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->SMTPAutoTLS = false;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;    
$mail->Username = GUSER;  
$mail->Password = GPWD;           
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
//If you know path then use this
#$mail->addAttachment("uploads/".$file_name); //Here add the proper path

//If you want to attach the use below, 
//If you write this in single class then call with $this->generatePdf()
$mail->addAttachment(generatePdf());
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo; 
return false;
} else {
$error = 'Message sent!';
return true;
}  // demo code
}

最新更新