[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");
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值