第一,須要下載PHPMailer文件包phpmailer. http://phpmailer.sourceforge.net/
第二,確認你的服務器系統已經支持socket ,經過phpinfo();查看是否支持sockets(socket 是屬於PHP擴展部分),若是顯現爲「enabled」,那就是支持了。
第三,把文件解壓到你的web服務器目錄下,調用類就能夠了.
首先包含class.phpmailer.php,而後建立對象,設置參數,調用成員函數。php
例1,作成函數方便調用html
<?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 注意:普通郵件認證不須要加 @域名 $mail->Password = "mailPassword"; // SMTP password $mail->From = "yourmail@yourdomain.com"; // 發件人郵箱 $mail->FromName = "管理員"; // 發件人 $mail->CharSet = "GB2312"; // 這裏指定字符集! $mail->Encoding = "base64"; $mail->AddAddress($sendto_email,"username"); // 收件人郵箱和姓名 $mail->AddReplyTo("yourmail@yourdomain.com","yourdomain.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; // 郵件內容 $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 "郵件發送有誤 "; echo "郵件錯誤信息: " . $mail->ErrorInfo; exit; } else { echo "$user_name 郵件發送成功!<br />"; } } // 參數說明(發送到, 郵件主題, 郵件內容, 附加信息, 用戶名) smtp_mail("yourmail@yourdomain.com", "歡迎使用phpmailer!", "NULL", "yourdomain.com", "username"); ?>
注意:
1. 郵件的字符集設置, $mail->CharSet = "GB2312"; // 這裏指定字符集!在這裏我只指定爲GB2312由於這樣Outlook能正常顯示郵件主題,我嘗試過設爲utf-8但在Outlook下顯示亂碼。
2. 若是是發送html格式的郵件,那麼記得也指定
3. 若是你想用它來羣發郵件的話,記得修改包含文件函數,如:
require("phpmailer/class.phpmailer.php");
改成
require_once("phpmailer/class.phpmailer.php");
不然的話會產生類的重定義。web
我的認爲要使用phpmailer,首先,須要有一個郵件服務器,PHP的 mail函數沒有指定,應該是使用的PHP設置的SMTP。
而在這裏須要具體指定,同時須要指定郵件服務器的管理者和密碼。
PHPMailer 也是一個功能強大的郵件類
PHPMailer的主要功能特色:
支持郵件 s/mime加密的數字簽名
支持郵件多個 TOs, CCs, BCCs and REPLY-TOs
能夠工做在任何服務器平臺,因此不用擔憂WIN平臺沒法發送郵件的問題的
支持文本/HTML格式郵件
能夠嵌入image圖像
對於郵件客戶端不支持HTML閱讀的進行支持
功能強大的發送郵件調試功能debug
自定義郵件header
冗餘SMTP服務器支持
支持8bit, base64, binary, and quoted-printable 編碼
文字自動換行
支持多附件發送功能
支持SMTP服務器驗證功能
在Sendmail, qmail, Postfix, Gmail, Imail, Exchange 等平臺測試成功
提供的下載文件中,包括內容詳細的說明文檔及示例說明,因此不用擔憂難於上手的問題!
PHPMailer 很是小巧、簡單、方便、快捷
以上資料由Jiucool 翻譯自phpmailer 官網,轉載請註明!
PHPMailer的使用(這裏以使用gmail smtp發送郵件爲例,固然也支持sendmail pop 等其餘方式):
首先到http://phpmailer.worxware.com/ 下載最新版本的程序包
下載完成後,找到class.phpmailer.php 、class.smtp.php兩個類放到本身的目錄下!
而後新建一個php文件這裏命名爲:phpmail_jiucool.php
phpmail_jiucool.php內容以下:
我直接將郵件發送模塊寫成一個函數postmail_jiucool_com(),你們使用的時候直接調用該函數便可,函數內容爲: 安全
function postmail_jiucool_com($to,$subject = "",$body = ""){ //Author:Jiucool WebSite: http://www.jb51.net //$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 ="UTF-8";//設定郵件編碼,默認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 = "smtp.googlemail.com"; // SMTP 服務器 $mail->Port = 465; // SMTP服務器的端口號 $mail->Username = "SMTP服務器用戶名"; // SMTP服務器用戶名 $mail->Password = "SMTP服務器密碼"; // SMTP服務器密碼 $mail->SetFrom('發件人地址,如admin#jiucool.com #換成@', '發件人名稱'); $mail->AddReplyTo("郵件回覆地址,如admin#jiucool.com #換成@","郵件回覆人的名稱"); $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!恭喜,郵件發送成功!"; } }
原文連接:http://www.jb51.net/article/33629.htm服務器