Linux平臺(tái)php通過(guò)phpmailer郵件類發(fā)送郵件代碼詳解及源碼下載
通過(guò)phpmailer郵件類發(fā)送郵件代碼及下載
PHPMailer是一個(gè)用于發(fā)送電子郵件的PHP函數(shù)包。它提供的功能包括:
*.在發(fā)送郵時(shí)指定多個(gè)收件人,抄送地址,暗送地址和回復(fù)地址
*.支持多種郵件編碼包括:8bit,base64,binary和quoted-printable
*.支持SMTP驗(yàn)證
*.支持冗余SMTP服務(wù)器
*.支持帶附件的郵件和Html格式的郵件
*.自定義郵件頭
*.支持在郵件中嵌入圖片
*.調(diào)試靈活
*.經(jīng)測(cè)試兼容的SMTP服務(wù)器包括:Sendmail,qmail,Postfix,Imail,Exchange等
*.可運(yùn)行在任何平臺(tái)之上
調(diào)用方法:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // 啟用SMTP
$mail->Host = "smtp1.example.com"; //SMTP服務(wù)器
$mail->SMTPAuth = true; //開(kāi)啟SMTP認(rèn)證
$mail->Username = "name@example.com"; // SMTP用戶名
$mail->Password = "password"; // SMTP密碼
$mail->Port = 587; // set the SMTP server port 如果端口號(hào)不是默認(rèn)請(qǐng)注明
$mail->From = "from@example.com"; //發(fā)件人地址
$mail->FromName = "Mailer"; //發(fā)件人
$mail->AddAddress("josh@example.net", "Josh Adams"); //添加收件人
$mail->AddAddress("ellen@example.com");
$mail->AddReplyTo("info@example.com", "Information"); //回復(fù)地址
$mail->WordWrap = 50; //設(shè)置每行字符長(zhǎng)度
/** 附件設(shè)置
$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // 添加附件,并指定名稱
*/
$mail->IsHTML(true); // 是否HTML格式郵件
$mail->Subject = "Here is the subject"; //郵件主題
$mail->Body = "This is the HTML message body <b>in bold!</b>"; //郵件內(nèi)容
$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //郵件正文不支持HTML的備用顯示
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
關(guān)于郵件亂碼的解決辦法:
郵件的中文會(huì)出現(xiàn)亂碼主要是編碼沒(méi)有設(shè)置好。
設(shè)置方法如下:
$mail->IsHTML(true); // 是否HTML格式郵件
$mail->CharSet = "utf-8"; // 這里指定字符集!$mail->Encoding = "base64";
但是請(qǐng)注意,這并不能完全保證你收到的郵件是正確的編碼。在發(fā)送html郵件時(shí),我們需要發(fā)送一個(gè)完整的html文檔。
如:
<html><head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>含中文的內(nèi)容</body>
</html>
附件下載:
PHPMailer.zip 114.94KB
更多下載請(qǐng)?jiān)L問(wèn): http://sourceforge.net/projects/phpmailer/