PHPMailer實現PHP郵件發送

1.首先是下載PHPMailer

http://code.google.com/a/apache-extras.org/p/phpmailer/

2.解壓

從中取出class.phpmailer.php 和 class.smtp.php 放到你的項目的文件夾,因爲我們等下會引用到它們.

3.創建發送郵件的函數,其中你需要配置smtp服務器

function postmail($to,$subject = '',$body = ''){
//Author:Jiucool WebSite: http://www.jiucool.com
//$to 表示收件人地址 $subject 表示郵件標題 $body表示郵件正文
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('Asia/Shanghai');//設定時區東八區
require_once('class.phpmailer.php');
include('class.smtp.php');
$mail = new PHPMailer(); //new一個PHPMailer對象出來
$body = eregi_replace("[\]",'',$body); //對郵件內容進行必要的過濾
$mail->CharSet ="GBK";//設定郵件編碼,默認ISO-8859-1,如果發中文此項必須設置,否則亂碼
$mail->IsSMTP(); // 設定使用SMTP服務
$mail->SMTPDebug = 1; // 啓用SMTP調試功能
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // 啓用 SMTP 驗證功能
$mail->SMTPSecure = "ssl"; // 安全協議,可以註釋掉
$mail->Host = 'stmp.163.com'; // SMTP 服務器
$mail->Port = 25; // SMTP服務器的端口號
$mail->Username = 'userpp007'; // SMTP服務器用戶名,PS:我亂打的
$mail->Password = 'password'; // SMTP服務器密碼
$mail->SetFrom('[email protected]', 'who');
$mail->AddReplyTo('[email protected]','who');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // 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!恭喜,郵件發送成功!";
}
}

4. 使用函數

postmail('[email protected]','My subject','嘩啦啦');

注意充當SMTP郵箱設置,打開SMTP以及POP3服務
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章