PHP使用pear自带的mail类库发邮件的方法

时间:2022-02-28 00:30:16

本文实例讲述了PHP使用pear自带的mail类库发邮件的方法。分享给大家供大家参考。具体如下:

这里用pear自带的mail类库发邮件,可以用pear install 命令来安装对应的库

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
body = "<a href='http://www.baidu.com/' target='_blank'>点我重新生成密码</a>";
sendMail_smtp("xxxxxxxx@qq.com",'测试',$body);
function sendMail_smtp($smtpemailto,$mailsubject,$mailbody){
 //error_reporting(7);
 require_once 'Mail.php';
 require_once 'Mail/mime.php';
 $from = 'admin@xxx.com';
 $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;
 }
}

希望本文所述对大家的php程序设计有所帮助。