php使用pear_smtp發(fā)送郵件
PHP自帶的mail函數(shù)比較蛋疼,在win下配置了sendmail還是無(wú)法發(fā)送郵件。而使用第三方的pear/mail可以直接通過(guò)smtp連接郵件發(fā)送服務(wù)器。如(smtp.163.com)。從而沒(méi)有必要在本機(jī)上安裝sendmail等類(lèi)似軟件。
確保PEAR Mail包已經(jīng)安裝。
<?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 多數(shù)使用 mail 函數(shù)來(lái)發(fā)送郵件,但我們可以使用其他的 SMTP 服務(wù)器來(lái)發(fā)送,這里推薦使用 PEAR's mail package 來(lái)發(fā)送郵件。
$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());}
第三個(gè)案例:
在使用以下源代碼前,請(qǐng)配置好pear的路徑,下載net_smtp包
在php.ini文件中根據(jù)你的操作系統(tǒng)選擇不同的設(shè)置方法
; UNIX: "/path1:/path2"
include_path = ".:./php/pear"
;
; Windows: "\path1;\path2"
;include_path = ".;c:\php\pear"
require 'Net/SMTP.php';
$host = '126.com';//smtp服務(wù)器的ip或域名
$username= 'arcow';//登陸smtp服務(wù)器的用戶名
$password= 'secret';//登陸smtp服務(wù)器的密碼
$from = 'arcow@126.com'; //誰(shuí)發(fā)的郵件
$rcpt = array('test@test.com', 'arcow@126.com');//可設(shè)多個(gè)接收者
$subj = "Subject: 你是誰(shuí)\n";//主題
$body = "test it";//郵件內(nèi)容
/* 建立一個(gè)類(lèi) */
if (! ($smtp = new Net_SMTP($host))) {
die("無(wú)法初始化類(lèi)Net_SMTP!\n");
}
/* 開(kāi)始連接SMTP服務(wù)器*/
if (PEAR::isError($e = $smtp->connect())) {
die($e->getMessage() . "\n");
}
/* smtp需要身份驗(yàn)證 */
$smtp->auth($username,$password,"PLAIN");
/*設(shè)置發(fā)送者郵箱 */
if (PEAR::isError($smtp->mailFrom($from))) {
die("無(wú)法設(shè)置發(fā)送者郵箱為 <$from>\n");
}
/* 設(shè)置接收郵件者 */
foreach ($rcpt as $to) {
if (PEAR::isError($res = $smtp->rcptTo($to))) {
die("郵件無(wú)法投遞到 <$to>: " . $res->getMessage() . "\n");
}
}
/* 開(kāi)始發(fā)送郵件內(nèi)容 */
if (PEAR::isError($smtp->data($subj . "\r\n" . $body))) {
die("Unable to send data\n");
}
/* 斷開(kāi)連接 */
$smtp->disconnect();
echo "發(fā)送成功!";
?>
以上就是本文的全部?jī)?nèi)容,php利用pear_smtp發(fā)送郵件的三個(gè)案例,希望對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。
相關(guān)文章
原生php實(shí)現(xiàn)excel文件讀寫(xiě)的方法分析
這篇文章主要介紹了原生php實(shí)現(xiàn)excel文件讀寫(xiě)的方法,結(jié)合實(shí)例形式分析了采用原生php針對(duì)Excel進(jìn)行讀寫(xiě)操作的相關(guān)實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2018-04-04
如何使用SublimeText3配置 PHP IDE環(huán)境
這篇文章主要介紹了如何使用SublimeText3配置 PHP IDE環(huán)境,并使用Xdebug進(jìn)行調(diào)試,喜歡使用SublimeText的同學(xué),可以參考下2021-04-04
php常見(jiàn)的網(wǎng)絡(luò)攻擊及防御方法
這篇文章主要介紹了php常見(jiàn)的網(wǎng)絡(luò)攻擊及防御方法,對(duì)網(wǎng)絡(luò)安全感興趣的同學(xué),可以參考下2021-04-04
PHP實(shí)現(xiàn)APP微信支付的實(shí)例講解
下面小編就為大家分享一篇PHP實(shí)現(xiàn)APP微信支付的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
PHP實(shí)現(xiàn)圖片的等比縮放和Logo水印功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)圖片的等比縮放和Logo水印功能,結(jié)合實(shí)例形式分析了php圖片等比例縮放及l(fā)ogo水印添加操作技巧,需要的朋友可以參考下2017-05-05
PHP面向?qū)ο蟪绦蛟O(shè)計(jì)(OOP)之方法重寫(xiě)(override)操作示例
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)(OOP)之方法重寫(xiě)(override)操作,簡(jiǎn)單描述了php面向?qū)ο蟪绦蛟O(shè)計(jì)中方法重寫(xiě)的原理,并結(jié)合實(shí)例形式分析了php方法重寫(xiě)相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12
PHP code 驗(yàn)證碼生成類(lèi)定義和簡(jiǎn)單使用示例
這篇文章主要介紹了PHP code 驗(yàn)證碼生成類(lèi)定義和簡(jiǎn)單使用,結(jié)合實(shí)例形式分析了PHP code 驗(yàn)證碼生成類(lèi)的基本功能定義、簡(jiǎn)單使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
php版本CKEditor 4和CKFinder安裝及配置方法圖文教程
這篇文章主要介紹了php版本CKEditor 4和CKFinder安裝及配置方法,結(jié)合圖文與實(shí)例形式詳細(xì)分析了php安裝及配置CKEditor 4和CKFinder相關(guān)實(shí)現(xiàn)步驟、操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-06-06

