PHP用pear自帶的mail類庫發郵件

用pear自帶的mail類庫發郵件,可以用pear install 命令來安裝對應的庫


$body = "<a href='http://www.baidu.com/' target='_blank'>點我重新生成密碼</a>";
 
sendMail_smtp("[email protected]",'測試',$body);
 
function sendMail_smtp($smtpemailto,$mailsubject,$mailbody){
    //error_reporting(7);
     
    require_once 'Mail.php';
    require_once 'Mail/mime.php';
 
    $from = '[email protected]';
    $to   = $smtpemailto;
    $password = 'xxxxxx';
     
    $mail_config=array(
            "host"=>"smtp.ym.163.com",
            "port"=>25,
            "auth"=>true,
            "username"=>$from,
            "password"=>$password,
            "from"=>$from,
    );
     
    $hdrs = array(
            'From'=>$from,
            'To' => $to, //收信地址
            'Subject'=>$mailsubject
    );
     
    $mime = new Mail_mime();
    //$mime->setTXTBody($text);
    //添加附件
    //$mime->addHTMLImage('php.gif','image/gif','12345',true);
    $mime->_build_params['html_charset'] = "utf-8";//設置編碼格式
    $mime->_build_params['head_charset'] = "utf-8";//設置編碼格式  
    $mime->setHTMLBody($mailbody);
    $body = $mime->get();
    $hdrs = $mime->headers($hdrs);
     
    $mail = Mail::factory('smtp',$mail_config);
    $succ = $mail->send($to,$hdrs,$body);
     
    if (PEAR::isError($succ))
    {
        //echo 'Email sending failed: ' . $succ->getMessage();
        $err = 'Email sending failed: ' . $succ->getMessage();
        $content = $to."\t".date('Y-m-d H:i:s')."\t ".$err." \r\n" ;
    }
    else
    {
        //$content = $to."\t".date('Y-m-d H:i:s')."\t Email sent succesfully \r\n" ;
        return true;
         
    }
     
}

原帖地址:https://www.oschina.net/code/snippet_88671_17839

其他方法:http://www.jb51.net/article/73978.htm


關於發郵件報錯535 Error:authentication failed解決方法

調用163郵箱服務器來發送郵件,我們需要開啓POP3/SMTP服務,這時163郵件會讓我們設置客戶端授權碼,這個授權碼替代上面代碼部分的passwd即可成功發送郵件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章