|
第一,需要下載phpMailer文件包phpmailer. http://phpmailer.sourceforge.NET/
第二,確認(rèn)你的服務(wù)器系統(tǒng)已經(jīng)支持socket ,通過phpinfo();查看是否支持sockets(socket 是屬于php擴展部分),如果顯現(xiàn)為“enabled”,那就是支持了。
第三,把文件解壓到你的web服務(wù)器目錄下,調(diào)用類就可以了.
首先包含class.phpmailer.php,然后創(chuàng)建對象,設(shè)置參數(shù),調(diào)用成員函數(shù)。
例1,做成函數(shù)方便調(diào)用
復(fù)制代碼 代碼如下:
<?php
require("phpmailer/class.phpmailer.php");
function smtp_mail( $sendto_email, $subject, $body, $extra_hdrs, $user_name){
$mail = new phpMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "200.162.244.66"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "yourmail"; // SMTP username 注意:普通郵件認(rèn)證不需要加 @域名
$mail->Password = "mailPassword"; // SMTP password
$mail->From = "yourmail@yourdomain.com"; // 發(fā)件人郵箱
$mail->FromName = "管理員"; // 發(fā)件人
$mail->CharSet = "GB2312"; // 這里指定字符集!
$mail->Encoding = "base64";
$mail->AddAddress($sendto_email,"username"); // 收件人郵箱和姓名
$mail->AddReplyTo("yourmail@yourdomain.com","yourdomain.com");
//$mail->WordWrap = 50; // set word wrap 換行字?jǐn)?shù)
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 附件
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
$mail->IsHTML(true); // send as HTML
// 郵件主題
$mail->Subject = $subject;
// 郵件內(nèi)容
$mail->Body = "
<html><head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>
I love php。
</body>
</html>
";
$mail->AltBody ="text/html";
if(!$mail->Send())
{
echo "郵件發(fā)送有誤 <p>";
echo "郵件錯誤信息: " . $mail->ErrorInfo;
exit;
}
else {
echo "$user_name 郵件發(fā)送成功!<br />";
}
}
// 參數(shù)說明(發(fā)送到, 郵件主題, 郵件內(nèi)容, 附加信息, 用戶名)
smtp_mail("yourmail@yourdomain.com", "歡迎使用phpmailer!", "NULL", "yourdomain.com", "username");
?>
注意:
1. 郵件的字符集設(shè)置, $mail->CharSet = "GB2312"; // 這里指定字符集!在這里我只指定為GB2312因為這樣Outlook能正常顯示郵件主題,我嘗試過設(shè)為utf-8但在Outlook下顯示亂碼。
2. 如果是發(fā)送html格式的郵件,那么記得也指定
3. 如果你想用它來群發(fā)郵件的話,記得修改包含文件函數(shù),如:
require("phpmailer/class.phpmailer.php");
改為
require_once("phpmailer/class.phpmailer.php");
否則的話會產(chǎn)生類的重定義。
個人認(rèn)為要使用phpmailer,首先,需要有一個郵件服務(wù)器,php的 mail函數(shù)沒有指定,應(yīng)該是使用的php設(shè)置的SMTP。
而在這里需要具體指定,同時需要指定郵件服務(wù)器的管理者和密碼。
phpMailer 也是一個功能強大的郵件類
phpMailer的主要功能特點:
支持郵件 s/mime加密的數(shù)字簽名
支持郵件多個 TOs, CCs, BCCs and REPLY-TOs
可以工作在任何服務(wù)器平臺,所以不用擔(dān)心WIN平臺無法發(fā)送郵件的問題的
支持文本/HTML格式郵件
可以嵌入image圖像
對于郵件客戶端不支持HTML閱讀的進行支持
功能強大的發(fā)送郵件調(diào)試功能debug
自定義郵件header
冗余SMTP服務(wù)器支持
支持8bit, base64, binary, and quoted-printable 編碼
文字自動換行
支持多附件發(fā)送功能
支持SMTP服務(wù)器驗證功能
在Sendmail, qmail, Postfix, Gmail, Imail, Exchange 等平臺測試成功
提供的下載文件中,包括內(nèi)容詳細(xì)的說明文檔及示例說明,所以不用擔(dān)心難于上手的問題!
phpMailer 非常小巧、簡單、方便、快捷
以上資料由Jiucool 翻譯自phpmailer 官網(wǎng),轉(zhuǎn)載請注明!
phpMailer的使用(這里以使用gmail smtp發(fā)送郵件為例,當(dāng)然也支持sendmail pop 等其他方式):
首先到http://phpmailer.worxware.com/ 下載最新版本的程序包
下載完成后,找到class.phpmailer.php 、class.smtp.php兩個類放到自己的目錄下!
然后新建一個php文件這里命名為:phpmail_jiucool.php
phpmail_jiucool.php內(nèi)容如下:
我直接將郵件發(fā)送模塊寫成一個函數(shù)postmail_jiucool_com(),大家使用的時候直接調(diào)用該函數(shù)即可,函數(shù)內(nèi)容為:
復(fù)制代碼 代碼如下:
function postmail_jiucool_com($to,$subject = "",$body = ""){
//Author:Jiucool WebSite: http://www.jb51.NET
//$to 表示收件人地址 $subject 表示郵件標(biāo)題 $body表示郵件正文
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set("Asia/Shanghai");//設(shè)定時區(qū)東八區(qū)
require_once('class.phpmailer.php');
include("class.smtp.php");
$mail = new phpMailer(); //new一個phpMailer對象出來
$body = eregi_replace("[/]",'',$body); //對郵件內(nèi)容進行必要的過濾
$mail->CharSet ="UTF-8";//設(shè)定郵件編碼,默認(rèn)ISO-8859-1,如果發(fā)中文此項必須設(shè)置,否則亂碼
$mail->IsSMTP(); // 設(shè)定使用SMTP服務(wù)
$mail->SMTPDebug = 1; // 啟用SMTP調(diào)試功能
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // 啟用 SMTP 驗證功能
$mail->SMTPSecure = "ssl"; // 安全協(xié)議
$mail->Host = "smtp.googlemail.com"; // SMTP 服務(wù)器
$mail->Port = 465; // SMTP服務(wù)器的端口號
$mail->Username = "SMTP服務(wù)器用戶名"; // SMTP服務(wù)器用戶名
$mail->Password = "SMTP服務(wù)器密碼"; // SMTP服務(wù)器密碼
$mail->SetFrom('發(fā)件人地址,如admin#jiucool.com #換成@', '發(fā)件人名稱');
$mail->AddReplyTo("郵件回復(fù)地址,如admin#jiucool.com #換成@","郵件回復(fù)人的名稱");
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer! - From www.jiucool.com"; // optional, comment out and test
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, "收件人名稱");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!恭喜,郵件發(fā)送成功!";
}
}
php技術(shù):有關(guān)phpmailer的詳細(xì)介紹及使用方法,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。