01. 依據您的 PHP 版本來下載適合的版本 PHPMailer
// 下載解壓縮後把檔案放到跟下面範例程式同一層, 如果不同層的話請更改下面位置 require_once('./class.phpmailer.php');
// 上傳的話只要傳 class.pop3.php, class.smtp.php, class.phpmailer.php 即可, 其它檔案用不到。
eg : require_once('./class.phpmailer.php');
./mail/mail.php
./mail/class.pop3.php
./mail/class.smtp.php
./mail/class.phpmailer.php
02. 把下面的 code, 另存成 mail.php, 編碼請選 UTF-8, 可以讓信的 Title 正常顯示中文。
// 另存 UTF-8 時, 要選擇不要加一個 Unicode BOM 標籤, 不然會有問題喔
// 如果用記事本存 UTF-8 會自動加 Unicode BOM 標籤喔, 所以千萬不要用記事本
// 免費的推薦使用 PSPad or Notepad++
// PSPad : 格式 -> UTF-8 -> 存檔
// Notepad++ : 格式 -> 轉換至 UTF-8 碼格式 (檔首無 BOM) -> 存檔
最後, 瀏覽 http://your.domain/mail/mail.php 就會看到信已寄出了
<html>
<head>
<title>PHPMailer - SMTP (Gmail)</title>
</head>
<body>
<?php
require_once('./class.phpmailer.php');
// include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
// Smtp Setting
$mail->Host = "smtp.gmail.com"; // SMTP server (sets GMAIL as the SMTP server)
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->CharSet = "utf-8"; // Sets the CharSet of the message
$mail->Username = "yourusername@gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
// Smtp Setting
// Setting the Sender, Receicer, ...
$mail->SetFrom('From@who-know.com', 'Amy'); // Adds a "From" address.
$mail->AddAddress('To@who-know.com', 'Eric'); // Adds a "To" address.
$mail->AddReplyTo('Reply-To@who-know.com', 'Eric'); // Adds a "Reply-to" address.
// Setting the Sender, Receicer, ...
// Setting the mail title, content ...
$mail->Subject = 'PHPMailer 測試中文標題';
$mail->Body = "Hi 中文內容,<br>This is the HTML BODY<br>"; // HTML Body
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true);
// $mail->AddAttachment('images/phpmailer.gif'); // attachment
// Setting the mail title, content ...
$mail->Send();
echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
</body>
</html>
03. 更多設定可以參考 PHPMailer Property, PHPMailer Methods, PHPMailer Tip&Tool
04. 如果有問題請先確定下面幾件事
a. 是否啟用了 Gmail 的設定。
. 英 : Gmail -> Settings -> Forwarding and POP/IMAP -> POP Download -> Enable POP -> Save Changes
. 繁 : Gmail -> 設定 -> 轉寄和 POP/IMAP -> POP 下載 -> 啟用 POP 功能 -> 儲存變更
. 簡 : Gmail -> 设置 -> 转发和 POP/IMAP -> POP 下载 -> 件启用 POP -> 保存變更
. 日 : Gmail -> 設定 -> メール転送と POP/IMAP -> POP ダウンロード -> POP を有効 -> 変更を保存
b. 您用的虛擬主機是否有支援 openssl。
September 28th, 2009 at 5:33 PM
我的wordpress还不能发邮件呢。
September 28th, 2009 at 9:18 PM
您可以試試下面這幾款 WordPress Plugin, 如 WP-Mail-SMTP, Configure SMTP, ShiftThis | Swift SMTP, XPertMailer。