[PHP]运用PHPMailer发送带压缩附件的邮件

某些情况经常要定期收集信息,当数据量大时,附件就超过了邮件的限制,此时将附件压缩,在通过PHPMailer发送是一个简单可靠的方式。

1,首先下载PHPMailer的函数库  http://sourceforge.net/projects/phpmailer/,放在可以加载的地方

2,配置邮件参数

3,用Linux自带的命令zip -r $filename.zip $filename 压缩附件(或者安装的其他压缩方式,tar等)

4,加载附件发送邮件

    protected function _sendMail($subject, $content, $address = '')
    {           
        $mailer = new PHPmailer();
        
        $mailer->Host = 'smtp.163.com';
        $mailer->IsSMTP();
        $mailer->SMTPAuth = true;
        
        //链家邮件发件人设置
        $mailer->Username = "willwcw";
        $mailer->Password = 'XXXXXXX';  //密码
        
        $mailer->From = "willwcw@163.com";
        $mailer->FromName = "通知邮件";
        $mailer->CharSet = "UTF-8";

        //收件人设置
        $mailer->Encoding = "base64";
        $mailer->AddAddress('xxxx@163.com'); 
        //$mailer->AddCC('webrd@l63.com');  //抄送

        $filename = $subject.".html";
        $file = fopen($filename, "w+");
        fwrite($file, $content);
        fclose($file);
        //文件太大,采取压缩后附件形式
        //shell_exec("tar zcvf $filename.tar $filename ");
        shell_exec("zip -r $filename.zip $filename ");
        
        $mailer->IsHTML(true);
        $mailer->Subject = $subject;
        $mailer->Body =  $title = "<h1 align='center'>".$this->yesterday.$filename."邮件提醒</h1>";    
        $tarName = $filename.'.zip';
        $mailer->AddAttachment('./'.$tarName, "$tarName");//附件的路径和附件名称
       
        if($mailer->Send()){
            Yii::info("send email $subject successful!");
        }else{
            Yii::warning("sendmail wrong", $mailer->ErrorInfo);
        }
        shell_exec("rm $tarName");
        shell_exec("rm $filename");
    }

发表评论

邮箱地址不会被公开。 必填项已用*标注


*