当前位置: 移动技术网 > IT编程>开发语言>PHP > php使用pear_smtp发送邮件

php使用pear_smtp发送邮件

2017年12月12日  | 移动技术网IT编程  | 我要评论

php自带的mail函数比较蛋疼,在win下配置了sendmail还是无法发送邮件。而使用第三方的pear/mail可以直接通过smtp连接邮件发送服务器。如(smtp.163.com)。从而没有必要在本机上安装sendmail等类似软件。
确保pear mail包已经安装。

<?php 
 require_once "vendor/autoload.php"; 
  
 $from = "test<test@163.com>"; 
 $to = "test <test@outlook.com>"; 
 $subject = "hi!"; 
 $body = "hi,\n\nhow are you?"; 
  
 $host = "smtp.163.com"; 
$port = "25"; 
 $username = "test@163.com"; 
 $password = "test123"; 
  
 $headers = array ('from' => $from, 
  'to' => $to, 
  'subject' => $subject); 
 $smtp = mail::factory('smtp', 
  array ('host' => $host, 
   'port' => $port, 
   'auth' => true, 
  // 'debug'=>true, 
   'username' => $username, 
   'password' => $password)); 
  
 $mail = $smtp->send($to, $headers, $body); 
  
 if (pear::iserror($mail)) { 
  echo("<p>" . $mail->getmessage() . "</p>"); 
 } else { 
  echo("<p>message successfully sent!</p>"); 
 } 
 ?>

 这是非加密方式。
 phper 多数使用 mail 函数来发送邮件,但我们可以使用其他的 smtp 服务器来发送,这里推荐使用 pear's mail package 来发送邮件。

 $subject = "this mail is sent from smtp.";
$mail_body = "this is the body of the mail which is sent using smtp.";
$from = "from: from name <fromaddress@xpertdeveloper.com>"; 
$to = "to: to name <toaddress@xpertdeveloper.com>"; 
$receiver = "toaddress@xpertdeveloper.com"; 
 
// setting up the headers
$headers["from"] = $from; 
$headers["to"] = $to; 
$headers["subject"] = $subject; 
$headers["reply-to"] = "reply@address.com"; 
$headers["content-type"] = "text/plain; charset=iso-2022-jp"; 
$headers["return-path"] = "returnpath@address.com"; 
 
// setting up the smtp setting
$smtp_info["host"] = "smtp.server.com"; 
$smtp_info["port"] = "25"; 
$smtp_info["auth"] = true; 
$smtp_info["username"] = "smtp_user"; 
$smtp_info["password"] = "smtp_password"; 
 
// creating the pear mail object :
$mail_obj =& mail::factory("smtp", $smtp_info); 
 
// sending the mail now
$mail_sent = $mail_obj->send($receiver, $headers, $mail_body); 
 
// if any error the see for that here:
if (pear::iserror($mail_sent)) { print($mail_sent->getmessage());}

第三个案例:

在使用以下源代码前,请配置好pear的路径,下载net_smtp包
在php.ini文件中根据你的操作系统选择不同的设置方法

; unix: "/path1:/path2" 
include_path = ".:./php/pear"
;
; windows: "\path1;\path2"
;include_path = ".;c:\php\pear"
require 'net/smtp.php';
$host = '126.com';//smtp服务器的ip或域名
$username= 'arcow';//登陆smtp服务器的用户名
$password= 'secret';//登陆smtp服务器的密码
$from = 'arcow@126.com';  //谁发的邮件
$rcpt = array('test@test.com', 'arcow@126.com');//可设多个接收者
$subj = "subject: 你是谁\n";//主题
$body = "test it";//邮件内容
/* 建立一个类 */
if (! ($smtp = new net_smtp($host))) {
  die("无法初始化类net_smtp!\n");
}
/* 开始连接smtp服务器*/
if (pear::iserror($e = $smtp->connect())) {
  die($e->getmessage() . "\n");
}
/* smtp需要身份验证 */
$smtp->auth($username,$password,"plain");
/*设置发送者邮箱 */
if (pear::iserror($smtp->mailfrom($from))) {
  die("无法设置发送者邮箱为 <$from>\n");
}
/* 设置接收邮件者 */
foreach ($rcpt as $to) {
  if (pear::iserror($res = $smtp->rcptto($to))) {
    die("邮件无法投递到 <$to>: " . $res->getmessage() . "\n");
  }
}
/* 开始发送邮件内容 */
if (pear::iserror($smtp->data($subj . "\r\n" . $body))) {
  die("unable to send data\n");
}
/* 断开连接 */
$smtp->disconnect();
echo "发送成功!";
?>

以上就是本文的全部内容,php利用pear_smtp发送邮件的三个案例,希望对大家学习php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网