Linux平臺php通過phpmailer郵件類發送郵件代碼詳解及源碼下載
通過phpmailer郵件類發送郵件代碼及下載
PHPMailer是一個用于發送電子郵件的PHP函數包。它提供的功能包括:
*.在發送郵時指定多個收件人,抄送地址,暗送地址和回復地址
*.支持多種郵件編碼包括:8bit,base64,binary和quoted-printable
*.支持SMTP驗證
*.支持冗余SMTP服務器
*.支持帶附件的郵件和Html格式的郵件
*.自定義郵件頭
*.支持在郵件中嵌入圖片
*.調試靈活
*.經測試兼容的SMTP服務器包括:Sendmail,qmail,Postfix,Imail,Exchange等
*.可運行在任何平臺之上
調用方法:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // 啟用SMTP
$mail->Host = "smtp1.example.com"; //SMTP服務器
$mail->SMTPAuth = true; //開啟SMTP認證
$mail->Username = "name@example.com"; // SMTP用戶名
$mail->Password = "password"; // SMTP密碼
$mail->Port = 587; // set the SMTP server port 如果端口號不是默認請注明
$mail->From = "from@example.com"; //發件人地址
$mail->FromName = "Mailer"; //發件人
$mail->AddAddress("josh@example.net", "Josh Adams"); //添加收件人
$mail->AddAddress("ellen@example.com");
$mail->AddReplyTo("info@example.com", "Information"); //回復地址
$mail->WordWrap = 50; //設置每行字符長度
/** 附件設置
$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>"; //郵件內容
$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";
?>
關于郵件亂碼的解決辦法:
郵件的中文會出現亂碼主要是編碼沒有設置好。
設置方法如下:
$mail->IsHTML(true); // 是否HTML格式郵件
$mail->CharSet = "utf-8"; // 這里指定字符集!$mail->Encoding = "base64";
但是請注意,這并不能完全保證你收到的郵件是正確的編碼。在發送html郵件時,我們需要發送一個完整的html文檔。
如:
<html><head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>含中文的內容</body>
</html>
附件下載:
PHPMailer.zip 114.94KB
更多下載請訪問: http://sourceforge.net/projects/phpmailer/