Windows下WordPress郵件發送(外部smtp)解決方案

來源:互聯網
上載者:User

最近朋友採用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似乎採用配置版的比較好;添加以上代碼以後,非得在後台先啟用使用者註冊,不然怎麼也不能發送郵件,真是奇異^_^

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.