【问题标题】:How to send attachment with phpmailer in yii framweork from function?如何在 yii 框架中使用 phpmailer 从函数发送附件?
【发布时间】:2020-11-04 18:26:49
【问题描述】:

我正在使用 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
}

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

【问题讨论】:

    标签: php email yii email-attachments


    【解决方案1】:
    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
    }
    

    【讨论】:

    • 感谢您的回复。但我没有将我的 pdf 存储在任何地方。它是动态创建的,我必须设置该附件。
    • @amitsutar,请检查我更新的代码,这将有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    相关资源
    最近更新 更多