PHPMailer郵件類利用smtp.163.com發(fā)送郵件方法
更新時(shí)間:2008年09月11日 18:47:03 作者:
利用smtp.163.com 發(fā)送郵件(本地?zé)o需服務(wù)器)
第一步:需要下載PHPMailer文件包phpmailer-1.73.tar.gz 來自開源社區(qū): http://phpmailer.sourceforge.net/
第二步:確認(rèn)你的服務(wù)器系統(tǒng)已經(jīng)支持socket 如下圖,通過phpinfo();查看是否支持sockets
如果沒有這一項(xiàng)就請(qǐng)注意: socket 是屬于PHP擴(kuò)展部分,編譯時(shí)必須給定一個(gè)用于./configure --enable-sockets 的配置選項(xiàng)。

第三步:把文件解壓到你的web服務(wù)器目錄下,調(diào)用類就可以了,說明:首先包含 class.phpmailer.php,然后創(chuàng)建對(duì)象,設(shè)置參數(shù),調(diào)用成員函數(shù)。具體請(qǐng)見下面的示例代碼:
<?php
/*******************************
* 作者:李英江
* 日期:2006-12-7
*******************************/
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@cgsir.com"; // 發(fā)件人郵箱
$mail->FromName = "cgsir.com管理員"; // 發(fā)件人
$mail->CharSet = "GB2312"; // 這里指定字符集!
$mail->Encoding = "base64";
$mail->AddAddress($sendto_email,"username"); // 收件人郵箱和姓名
$mail->AddReplyTo("yourmail@cgsir.com","cgsir.com");
//$mail->WordWrap = 50; // set word wrap
//$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>
歡迎來到<a >http://www.cgsir.com</a> <br /><br />
感謝您注冊(cè)為本站會(huì)員!<br /><br />
</body>
</html>
';
$mail->AltBody ="text/html";
if(!$mail->Send())
{
echo "郵件發(fā)送有誤 <p>";
echo "郵件錯(cuò)誤信息: " . $mail->ErrorInfo;
exit;
}
else {
echo "$user_name 郵件發(fā)送成功!<br />";
}
}
// 參數(shù)說明(發(fā)送到, 郵件主題, 郵件內(nèi)容, 附加信息, 用戶名)
smtp_mail('yourmail@cgsir.com', '歡迎來到cgsir.com!', 'NULL', 'cgsir.com', 'username');
?>
要注意的內(nèi)容:
1. 郵件的字符集設(shè)置, $mail->CharSet = "GB2312"; // 這里指定字符集!在這里我只指定為GB2312因?yàn)檫@樣Outlook能正常顯示郵件主題,我嘗試過設(shè)為utf-8,但在Outlook下顯示亂碼。
2. 如果是發(fā)送html格式的郵件,那么記得也指定為<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
3. 如果你想用它來群發(fā)郵件的話,記得修改包含文件函數(shù),如:
require("phpmailer/class.phpmailer.php");
改為
require_once("phpmailer/class.phpmailer.php");
否則的話會(huì)產(chǎn)生類的重定義。
第二步:確認(rèn)你的服務(wù)器系統(tǒng)已經(jīng)支持socket 如下圖,通過phpinfo();查看是否支持sockets
如果沒有這一項(xiàng)就請(qǐng)注意: socket 是屬于PHP擴(kuò)展部分,編譯時(shí)必須給定一個(gè)用于./configure --enable-sockets 的配置選項(xiàng)。

第三步:把文件解壓到你的web服務(wù)器目錄下,調(diào)用類就可以了,說明:首先包含 class.phpmailer.php,然后創(chuàng)建對(duì)象,設(shè)置參數(shù),調(diào)用成員函數(shù)。具體請(qǐng)見下面的示例代碼:
復(fù)制代碼 代碼如下:
<?php
/*******************************
* 作者:李英江
* 日期:2006-12-7
*******************************/
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@cgsir.com"; // 發(fā)件人郵箱
$mail->FromName = "cgsir.com管理員"; // 發(fā)件人
$mail->CharSet = "GB2312"; // 這里指定字符集!
$mail->Encoding = "base64";
$mail->AddAddress($sendto_email,"username"); // 收件人郵箱和姓名
$mail->AddReplyTo("yourmail@cgsir.com","cgsir.com");
//$mail->WordWrap = 50; // set word wrap
//$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>
歡迎來到<a >http://www.cgsir.com</a> <br /><br />
感謝您注冊(cè)為本站會(huì)員!<br /><br />
</body>
</html>
';
$mail->AltBody ="text/html";
if(!$mail->Send())
{
echo "郵件發(fā)送有誤 <p>";
echo "郵件錯(cuò)誤信息: " . $mail->ErrorInfo;
exit;
}
else {
echo "$user_name 郵件發(fā)送成功!<br />";
}
}
// 參數(shù)說明(發(fā)送到, 郵件主題, 郵件內(nèi)容, 附加信息, 用戶名)
smtp_mail('yourmail@cgsir.com', '歡迎來到cgsir.com!', 'NULL', 'cgsir.com', 'username');
?>
1. 郵件的字符集設(shè)置, $mail->CharSet = "GB2312"; // 這里指定字符集!在這里我只指定為GB2312因?yàn)檫@樣Outlook能正常顯示郵件主題,我嘗試過設(shè)為utf-8,但在Outlook下顯示亂碼。
2. 如果是發(fā)送html格式的郵件,那么記得也指定為<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
3. 如果你想用它來群發(fā)郵件的話,記得修改包含文件函數(shù),如:
require("phpmailer/class.phpmailer.php");
改為
require_once("phpmailer/class.phpmailer.php");
否則的話會(huì)產(chǎn)生類的重定義。
您可能感興趣的文章:
- PHPMailer使用教程(PHPMailer發(fā)送郵件實(shí)例分析)
- 功能齊全的PHP發(fā)送郵件類代碼附詳細(xì)說明
- phpmailer簡(jiǎn)單發(fā)送郵件的方法(附phpmailer源碼下載)
- php中mail函數(shù)發(fā)送郵件失敗的解決方法
- Linux服務(wù)器下PHPMailer發(fā)送郵件失敗的問題解決
- phpmail類發(fā)送郵件函數(shù)代碼
- PHP實(shí)現(xiàn)自動(dòng)發(fā)送郵件功能代碼(qq 郵箱)
- thinkphp實(shí)現(xiàn)發(fā)送郵件密碼找回功能實(shí)例
- PHP實(shí)現(xiàn)163郵箱自動(dòng)發(fā)送郵件
- PHP的類 功能齊全的發(fā)送郵件類
- PHP發(fā)送郵件確認(rèn)驗(yàn)證注冊(cè)功能示例【修改別人郵件類】
相關(guān)文章
thinkPHP5.0框架引入Traits功能實(shí)例分析
這篇文章主要介紹了thinkPHP5.0框架引入Traits功能,結(jié)合實(shí)例形式分析了Traits的概念、功能及thinkPHP5.0中Traits功能的使用方法,需要的朋友可以參考下2017-03-03
php和redis實(shí)現(xiàn)秒殺活動(dòng)的流程
這篇文章主要介紹了php和redis設(shè)計(jì)秒殺活動(dòng)的流程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07

