轉載自:http://www.xgezhang.com/wp_aliyun_host_sendmail.html
最近想給網站搞一個自動回覆郵件評論的功能,最初的時候以爲wordpress回自動默認集成,但後面才發現並沒有,但卻提供了一些類似於wp-mail的函數可以供管理者調用。
於是便去網上搜索,很容易的找到如下一段代碼,非常經典,很多wp網站都在使用:
/* 開始*/
function comment_mail_notify($comment_id) {
$admin_notify = '1'; // admin 要不要收回復通知 ( '1'=要 ; '0'=不要 )
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改爲你指定的 e-mail.
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
global $wpdb;
if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
$wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
$wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
$notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
$spam_confirmed = $comment->comment_approved;
if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
$wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改爲可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回覆';
$message = '
<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
. trim(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 給您的回覆:<br />'
. trim($comment->comment_content) . '<br /></p>
<p>您可以點擊查看回復的完整內容</p>
<p>還要再度光臨 ' . get_option('blogname') . '</p>
<p>(此郵件由系統自動發送,請勿回覆.)</p>
</div>';
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $to, $subject, $message, $headers );
}
}
add_action('comment_post', 'comment_mail_notify');
/* 自動加勾選欄 */
function add_checkbox() {
echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">有人回覆時郵件通知我</label>';
}
add_action('comment_form', 'add_checkbox');
更改完裏面的一些配置參數之後,本來以爲能夠開始使用了,結果發現事情沒有想象的那麼簡單,測試了很多次都沒法收到郵件。
又檢查了很多遍看是不是代碼出現了問題,發現也不是,無奈之下只能求助與插件,嘗試了好多款插件比如wp SMTP等,在發送測試郵件的時候都會出現類似於”無法訪問SMTP服務器“等錯誤信息,這個問題也就暫時擱淺了。
直到幾天之後我猛然反應過來,會不會是服務器主機提供商本身的問題,在網上查閱了相關資料之後,確實是不同的主機上提供SMTP服務的渠道不一樣,有的需要打開某些接口才能使用。由於我使用的是阿里雲x3型服務器,想實現SMTP服務器必須調用它內置的一些函數。於是我開始求助於萬能的阿里雲人工服務(不得不讚一下,工單回覆神速,而且一針見血,已經幫助我解決好多問題了)經過一番交流之後,客服回覆了我這麼一段話:
鏈接如下:http://www.net.cn/service/faq/xuni/zujian/200811/3572.html?spm=0.0.0.0.TRX6rw
於是我按照它所說的在控制面板上打開了fsockopen,參照鏈接裏給的函數,進行簡單修改後放到了網站服務器中,結果竟然出奇的好用,於是乎問題終於解決了。
以下是我結合上面的提取文字的代碼以及阿里雲服務器內部接口代碼綜合修改之後一段程序,放到網站functions.php裏就可以直接使用了:
function send_mail($comment_id) {
//獲取評論者郵箱內容等
$comment = get_comment($comment_id); //獲取評論的編號
$parent_id = $comment->comment_parent ? $comment->comment_parent : ''; //找到回覆的父節點,及原評論者
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam')) {
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回覆';
$message = '
' . trim(get_comment($parent_id)->comment_author) . ', 您好!
您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:'
. trim(get_comment($parent_id)->comment_content) . '
' . trim($comment->comment_author) . ' 給您的回覆:'
.'
'
.' '.trim($comment->comment_content).
'
您可以點擊 '.'http://www.xgezhang.com/?p='.trim($comment->comment_post_ID).' 查看回復完整內容'.
'
歡迎再度光臨 ' .get_option('blogname') . ' www.xgezhang.com'.'
(此郵件由系統自動發送,請勿回覆.)
';
# $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
//阿里主機服務器函數
$loc_host = "XXX"; //發信計算機名,可隨意
$smtp_acc = "[email protected]"; //Smtp認證的用戶名,類似[email protected],或者fuweng
$smtp_pass="填寫郵箱的的密碼"; //Smtp認證的密碼,一般等同pop3密碼
$smtp_host="填寫smtp的地址"; //SMTP服務器地址,類似 smtp.tom.com
$from="填寫你的郵箱地址"; //發信人Email地址,你的發信信箱地址
$headers = "Content-Type: text/plain; charset=\"utf-8\"\r\nContent-Transfer-Encoding: base64";
$lb="\r\n"; //linebreak
$hdr = explode($lb,$headers); //解析後的hdr
if($message) {$bdy = preg_replace("/^\./","..",explode($lb,$message));}//解析後的message
$smtp = array(
//1、EHLO,期待返回220或者250
array("EHLO ".$loc_host.$lb,"220,250","HELO error: "),
//2、發送Auth Login,期待返回334
array("AUTH LOGIN".$lb,"334","AUTH error:"),
//3、發送經過Base64編碼的用戶名,期待返回334
array(base64_encode($smtp_acc).$lb,"334","AUTHENTIFICATION error : "),
//4、發送經過Base64編碼的密碼,期待返回235
array(base64_encode($smtp_pass).$lb,"235","AUTHENTIFICATION error : "));
//5、發送Mail From,期待返回250
$smtp[] = array("MAIL FROM: <".$from.">".$lb,"250","MAIL FROM error: ");
//6、發送Rcpt To。期待返回250
$smtp[] = array("RCPT TO: <".$to.">".$lb,"250","RCPT TO error: ");
//7、發送DATA,期待返回354
$smtp[] = array("DATA".$lb,"354","DATA error: ");
//8.0、發送From
$smtp[] = array("From: ".$from.$lb,"","");
//8.2、發送To
$smtp[] = array("To: ".$to.$lb,"","");
//8.1、發送標題
$smtp[] = array("Subject: ".$subject.$lb,"","");
//8.3、發送其他Header內容
foreach($hdr as $h) {$smtp[] = array($h.$lb,"","");}
//8.4、發送一個空行,結束Header發送
$smtp[] = array($lb,"","");
//8.5、發送信件主體
if($bdy) {foreach($bdy as $b) {$smtp[] = array(base64_encode($b.$lb).$lb,"","");}}
//9、發送“.”表示信件結束,期待返回250
$smtp[] = array(".".$lb,"250","DATA(end)error: ");
//10、發送Quit,退出,期待返回221
$smtp[] = array("QUIT".$lb,"221","QUIT error: ");
//打開smtp服務器端口
$fp = @fsockopen($smtp_host, 25);
if (!$fp) echo "Error: Cannot conect to ".$smtp_host."
";
while($result = @fgets($fp, 1024)){if(substr($result,3,1) == " ") { break; }}
$result_str="";
//發送smtp數組中的命令/數據
foreach($smtp as $req){
//發送信息
@fputs($fp, $req[0]);
//如果需要接收服務器返回信息,則
if($req[1]){
//接收信息
while($result = @fgets($fp, 1024)){
if(substr($result,3,1) == " ") { break; }
};
if (!strstr($req[1],substr($result,0,3))){
$result_str.=$req[2].$result."
";
}
}
}
//關閉連接
@fclose($fp);
return $result_str;
}
}
add_action('comment_post', 'send_mail');
最後效果如圖:
歡迎轉載,請註明出處