最近朋友採用WordPress做了個部落格,但卻被郵件發送的問題煩惱死了.WP在預設無sendmail等UNIX下的郵件伺服器時,怎麼也不能發送郵件.忙活了兩天,基本把這個問題給解決了.在社區閑逛時,有位老兄提到可以使用phpmailer,後來查看了一下WP2.2的所有檔案,發現它原來就內建這個東東.但是得小小的修改一下才能讓它工作,在此感謝這位兄弟,所有代碼均來自它的小站^_^(小菜不懂PHP語言555...),下面開始動手拉
1.開啟/wp-includes/目錄下的class-phpmailer.php,尋找class.smtp.php將其替換成class-smtp.php(官方的phpmailer兩個檔案名稱分別是class.phpmailer.php和class.smtp.php,放在WP以後,可能是為了統一檔案命名方式就改成了class-phpmailer.php和class-smtp.php,但忘了將裡面調用的檔案名稱一起修改了,呵呵)
2.在/wp-includes/目錄下建立立mail.inc.php(設定發送郵件需要使用的smtp),代碼如下
<?php
require("class.phpmailer.php");
class MyMailer extends PHPMailer {
// Set default variables for all new objects
var $Mailer = "smtp"; // Alternative to IsSMTP()
var $CharSet = "utf-8";
var $From = "你的郵件地址";
var $FromName = "name,你想起什麼名字都可以";
var $Host = "smtp伺服器位址";
var $Port = 25; //smtp server port
var $SMTPAuth = true;
var $Username = "你郵件的帳號";
var $Password = "你郵件的密碼";
//var $SMTPDebug = true;
var $WordWrap = 75;
}
?>
3.開啟/wp-includes/pluggable.php,尋找function wp_mail($to, $subject, $message, $headers = '') {
global $phpmailer;在global $phpmailer;其前面添加如下代碼
require("mail.inc.php");
$mail = new MyMailer;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $message;
return $mail->Send();
4.在此檔案中尋找wp_new_user_notification函數,修改其中的一行代碼:
把
wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_settings('blogname')), $message);
修改成
@wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_settings('blogname')), $message);
5.在檔案結尾?>前添加如下代碼
if ( !function_exists('wp_mail_attachment') ) :
function wp_mail_attachment($to, $subject, $message, $string, $filename, $encoding, $type) {
require("mail.inc.php");
$mail = new MyMailer;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddStringAttachment($string, $filename, $encoding, $type);
return $mail->Send();
}
endif;
OK,到此只需要在mail.inc.php中設定好smtp伺服器位址,連接埠,使用者名稱和密碼就可以使用非SSL SMTP Server(比如163)發送郵件了.
PS:PHP似乎採用配置版的比較好;添加以上代碼以後,非得在後台先啟用使用者註冊,不然怎麼也不能發送郵件,真是奇異^_^