PHP中用pear包自带的类发送带有附件的邮件

测试通过,大家可以试试,前题要有pear包

<?php
 require_once 'Mail.php';
 require_once 'Mail/mime.php';
  class Send_Mail{
   
    var $headers = array();
    var $config = array();
    var $mail_object;
    var $mime;
    var $hdrs = array();
   
    /*
     * 构造函数
     * $host邮箱主机
     */
    function Send_Mail($host){
        $this->config['mail'] = array(
                        'host' => $host,   //smtp服务器地址,可以用ip地址或者域名
                        'auth' => false,             //true表示smtp服务器需要验证
                        'username' => 'wanggaoliang',//用户名
                        'password' => 'wanggaoliang'    //密码
                        );
        $this->mail_object = Mail::factory('smtp', $this->config['mail']);//返回一个smtp类
       
    }
   
    /*
     * 邮件发送
     * $to 发送人邮箱
     * $from 发信人地址
     * $subject 邮件标题
     * $file 附件内容
     */
    function send($to,$from,$subject,$body,$file){
        $this->hdrs['From'] = $from; //发信地址
        $this->hdrs['To'] = $to; //收信地址
        $this->hdrs['Subject'] = $subject; //邮件标题
        $this->mime = new Mail_mime();
        $this->mime->_build_params['html_charset'] = "utf-8";//设置编码格式
        $this->mime->_build_params['head_charset'] = "utf-8";//设置编码格式
        $this->mime->setHTMLBody($body); //设置邮件正文
        if($file <> ""){
            $this->mime->addAttachment($file, 'text/html');//设置附件内容
        }
        $new_body = $this->mime->get();
        $headers = $this->mime->headers($this->hdrs);
        $this->mail_object->send($to,$headers,$new_body);//发送邮件

    }
}

?> 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值