Home > php教程 > PHP源码 > 用pear自带的mail类库发邮件 - PHP

用pear自带的mail类库发邮件 - PHP

PHP中文网
Release: 2016-05-25 17:02:15
Original
1146 people have browsed it

php代码

body = "<a href=&#39;http://www.baidu.com/&#39; target=&#39;_blank&#39;>点我重新生成密码</a>";
 
sendMail_smtp("xxxxxxxx@qq.com",&#39;测试&#39;,$body);
 
function sendMail_smtp($smtpemailto,$mailsubject,$mailbody){
    //error_reporting(7);
      
    require_once &#39;Mail.php&#39;;
    require_once &#39;Mail/mime.php&#39;;
 
    $from = &#39;admin@xxx.com&#39;;
    $to   = $smtpemailto;
    $password = &#39;xxxxxx&#39;;
      
    $mail_config=array(
            "host"=>"smtp.ym.163.com",
            "port"=>25,
            "auth"=>true,
            "username"=>$from,
            "password"=>$password,
            "from"=>$from,
    );
      
    $hdrs = array(
            &#39;From&#39;=>$from,
            &#39;To&#39; => $to, //收信地址
            &#39;Subject&#39;=>$mailsubject
    );
      
    $mime = new Mail_mime();
    //$mime->setTXTBody($text);
    //添加附件
    //$mime->addHTMLImage(&#39;php.gif&#39;,&#39;image/gif&#39;,&#39;12345&#39;,true);
    $mime->_build_params[&#39;html_charset&#39;] = "utf-8";//设置编码格式
    $mime->_build_params[&#39;head_charset&#39;] = "utf-8";//设置编码格式 
    $mime->setHTMLBody($mailbody);
    $body = $mime->get();
    $hdrs = $mime->headers($hdrs);
      
    $mail = Mail::factory(&#39;smtp&#39;,$mail_config);
    $succ = $mail->send($to,$hdrs,$body);
      
    if (PEAR::isError($succ))
    {
        //echo &#39;Email sending failed: &#39; . $succ->getMessage();
        $err = &#39;Email sending failed: &#39; . $succ->getMessage();
        $content = $to."\\t".date(&#39;Y-m-d H:i:s&#39;)."\\t ".$err." \\r\\n" ;
    }
    else
    {
        //$content = $to."\\t".date(&#39;Y-m-d H:i:s&#39;)."\\t Email sent succesfully \\r\\n" ;
        return true;
          
    }
      
}
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template