国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php使用pear_smtp發送郵件

2024-05-04 23:45:02
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了php使用pear_smtp發送郵件的相關資料,內容很豐富,感興趣的小伙伴們可以參考一下
 

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程序設計有所幫助。



注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 句容市| 绥江县| 海门市| 沛县| 台江县| 曲麻莱县| 方城县| 大足县| 甘肃省| 福清市| 佛教| 安义县| 武夷山市| 鸡东县| 龙井市| 育儿| 濮阳市| 论坛| 凉城县| 内乡县| 荆州市| 株洲市| 荥阳市| 两当县| 喀什市| 永胜县| 黄龙县| 隆德县| 巧家县| 宝清县| 丰镇市| 娱乐| 桐乡市| 盐山县| 仁怀市| 陈巴尔虎旗| 安塞县| 竹溪县| 阿城市| 贵州省| 南昌市|