大家都知道很多網站帶有「郵件訂閱網站」的功能,不過大部分訪客即使想訂閱也會因繁瑣的步驟望而卻步,不過今天暢想資源就來教大家如何於WordPress內向部分(隨機抽取)留言者發送新文章郵件通知,讓網站的精品文章能夠被訪客及時獲取!
預覽
教學
注意:使用該功能有可能(未經證實!)會由於類似群發而被服務商標識為垃圾郵件,一切後果自負!
提示:如果你需要添加嵌套評論回覆郵件通知提示功能,請見「【純代碼】為WordPress添加評論回覆郵件提醒通知功能」!
1、首先將下方代碼添加到「佈景函式庫(functions.php)」的最底部 ?>
之前,然後點擊
提示:你可以將下方代碼各高亮部分修改為你想要的配置!
注意:如果您的伺服器並不支持 mail
函數,請參考「這篇文章」並使用外部SMTP發送郵件即可!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
/* 向留言者發送新文章郵件通知開始(由AREFLY.COM製作) */ function mail_to_commenter($to, $subject, $message){ $from_email = get_option('admin_email'); //發件人=站長郵箱 //$from_email = "no-reply@domain.com"; //去除本行注釋可修改發件人郵箱! $charset = get_option('blog_charset'); $headers = "From: $from_email \n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: text/html;charset=\"$charset\"\n"; $to = strtolower($to); @wp_mail($to, $subject, $message, $headers); } function mail_to_commenters_when_post($post_ID){ global $wpdb; @$wpdb->query('create table if not exists `jiucool_mail_exclude`(`id` int(11) NOT NULL auto_increment,`email` varchar(100) NOT NULL,`pub_date` datetime NOT NULL default \'0000-00-00 00:00:00\', `ip` varchar(100) NOT NULL default \'0.0.0.0\',PRIMARY KEY (`id`))'); $exclude_emails_jiucool = array('my@domain.com'); //發送最新文章通知時需排除之郵箱 $total_mail = '50'; //郵件最大發送篇數 $tablecomments = $wpdb->comments; $a_time = date('Y-m-d H:i:s', strtotime('-3 Month')); $query_emails_from_db = "select id,email from jiucool_mail_exclude "; $emails = $wpdb->get_results($query_emails_from_db); foreach ($emails as $email) { array_push($exclude_emails_jiucool, $email->email); } $query = "SELECT count(comment_ID),comment_author, comment_author_email,comment_author_url FROM $tablecomments where comment_type='' AND comment_approved=1 and comment_date>'".$a_time."' group by comment_author_email order by rand() limit 0,".$total_mail; $comments = $wpdb->get_results($query); $posturl = get_option('home').'/?p='.$post_ID; $post = get_post($post_ID); $blogname = get_option('blogname'); $morestring = "<!--more-->"; $explodemore = explode($morestring, $post->post_content); if(!empty($explodemore[1])){ $excerpt = $explodemore[0]; } else if ($post->post_excerpt) { $excerpt = $post->post_excerpt; } else { if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){ $post_content = $result['1']; } else { $post_content_r = explode("\n",trim(strip_tags($post->post_content))); $post_content = $post_content_r['0']; } $excerpt = utf8Substr($post_content, 0, 220); } //$excerpt = mb_strimwidth($post->post_content, 0, 30000, '....'); $excerpt = ' <style> blockquote{ padding: 5px 10px; margin: 0 5px; border: 1px solid #ddd; /** border-left: 8px solid #ddd; **/ background-color: #e9e9e9; overflow: auto; border-left-width: 4px; font-family: inherit; } blockquote.code, pre{ font-family: "Courier New", sans-serif; padding: 5px 5px; margin: 0; } code{ padding: 2px 4px; color: #8A2908; background: rgba(0, 0, 0, 0.05); border: 1px solid rgba(0, 0, 0, 0.1); -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; font-size: 12px; font-family: "Courier New", sans-serif; word-wrap: break-word; word-break: break-all; } kbd{ padding: 0.1em 0.6em; border: 1px solid #cccccc; font-size: 11px; font-family: Arial, Helvetica, sans-serif; background-color: #f7f7f7; color: #333333; -webkit-box-shadow: rgba(0, 0, 0, 0.2) 0 1px 0, white 0 0 0 2px inset; -moz-box-shadow: rgba(0, 0, 0, 0.2) 0 1px 0, white 0 0 0 2px inset; box-shadow: rgba(0, 0, 0, 0.2) 0 1px 0, white 0 0 0 2px inset; -webkit-border-radius: 3px; -moz-border-radius: 3px; -ms-border-radius: 3px; -o-border-radius: 3px; border-radius: 3px; text-shadow: white 0 1px 0; display: inline-block; margin: 0 0.1em; line-height: 1.4; text-indent: 0em; } p img:not(.wp-smiley){ -webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 6px; max-width: 400px; width: expression(this.width > 400 ? "400px" : true); height: auto; } a{ color: #12addb; } </style> <div style="color: #555; font-size: 12px; font-family: \'Segoe UI\', \'Segoe\', \'Segoe WP\', \'Tahoma\', \'Microsoft YaHei\', sans-serif; -webkit-font-smoothing: antialiased; width: 600px; margin:0 auto; border: 1px solid #e9e9e9; border-top: none;"> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr valign="top" height="2"> <td width="190" bgcolor="#0B9938"></td> <td width="120" bgcolor="#9FCE67"></td> <td width="85" bgcolor="#EDB113"></td> <td width="85" bgcolor="#FFCC02"></td> <td width="130" bgcolor="#5B1301" valign="top"></td> </tr> </tbody> </table> <div style="padding:0 15px 8px"> <h2 style="border-bottom: 1px solid #e9e9e9; font-size: 14px; font-weight: normal; padding: 10px 0 10px;"><span style="color: #12addb;">> </span>「<a style="text-decoration:none;color:#12addb" href="' . get_option("home") . '" title="' . get_option("blogname") . '" target="_blank">' . get_option("blogname") . '</a>」部落格有新文章發表啦!</h2> <div style="font-size:12px;color:#777;padding:0 10px;margin-top:18px"><div class="im"> <p>尊敬的訪客您好!</p> <p>「<a style="text-decoration:none;color:#12addb" href="' . get_option("home") . '" title="' . get_option("blogname") . '" target="_blank">' . get_option("blogname") . '</a>」最新文章:<strong>' . get_the_title($comment->comment_post_ID) . '</strong></p> <p style="background-color:#f5f5f5;padding:10px 15px;margin:18px 0">文章摘要:'.$excerpt.'</p> </div> <p>您可以「<a style="text-decoration:none; color:#12addb" href="'.$posturl .'" target="_blank">查看全文</a>」或「<a style="text-decoration:none; color:#12addb" href="'.$posturl .'#comments" target="_blank">發表評論</a>」</p> <p>歡迎再次光臨「<a style="text-decoration:none; color:#12addb" href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a>」</p> </div> </div> <div style="color:#888;padding:10px;border-top:1px solid #e9e9e9;background:#f5f5f5"> <p style="margin:0;padding:0"><span style="color:#BDBDBD">© <a style="color: #888; text-decoration: none;" href="' . get_option("home") . '" title="' . get_option("blogname") . '" target="_blank">' . get_option("blogname") . '</a> 您收到本郵件是由於您曾於本站留言並被隨機選中,如果您希望退訂本郵件,請「<a href="' . get_option("home") . '/unsubscribe.php?email='.$comments[$i]->comment_author_email.'&code='.$code_jiucool.'" target="_blank">點擊這裏</a>」!</span></p> </div>'; date_default_timezone_set("Asia/Shanghai"); $mo_time = strtotime(date('Y-m-d H:i:s', time())); $pub_time = strtotime(get_the_time('Y-m-d H:i:s', $post_ID)); $mail_title="=?UTF-8?B?".base64_encode(get_option('blogname').'最新文章:'.$post->post_title)."?="; if($mo_time-$pub_time<36){ $content = ''; $count_jiucool=0; $i = 0; do{ if(!in_array($comments[$i]->comment_author_email, $exclude_emails_jiucool)){ $code_jiucool = md5($comments[$i]->comment_author_email); $excerpt_to = $excerpt; mail_to_commenter($comments[$i]->comment_author_email, $mail_title, $excerpt_to); $count_jiucool++; $content .=($count_jiucool).". <a href='".$comments[$i]->comment_author_url."' target='_blank'>".$comments[$i]->comment_author.'</a> : '.$comments[$i]->comment_author_email.",<br />"; } $i++; }while($i<count($comments)); $content = ' <style> blockquote{ padding: 5px 10px; margin: 0 5px; border: 1px solid #ddd; /** border-left: 8px solid #ddd; **/ background-color: #e9e9e9; overflow: auto; border-left-width: 4px; font-family: inherit; } blockquote.code, pre{ font-family: "Courier New", sans-serif; padding: 5px 5px; margin: 0; } code{ padding: 2px 4px; color: #8A2908; background: rgba(0, 0, 0, 0.05); border: 1px solid rgba(0, 0, 0, 0.1); -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; font-size: 12px; font-family: "Courier New", sans-serif; word-wrap: break-word; word-break: break-all; } kbd{ padding: 0.1em 0.6em; border: 1px solid #cccccc; font-size: 11px; font-family: Arial, Helvetica, sans-serif; background-color: #f7f7f7; color: #333333; -webkit-box-shadow: rgba(0, 0, 0, 0.2) 0 1px 0, white 0 0 0 2px inset; -moz-box-shadow: rgba(0, 0, 0, 0.2) 0 1px 0, white 0 0 0 2px inset; box-shadow: rgba(0, 0, 0, 0.2) 0 1px 0, white 0 0 0 2px inset; -webkit-border-radius: 3px; -moz-border-radius: 3px; -ms-border-radius: 3px; -o-border-radius: 3px; border-radius: 3px; text-shadow: white 0 1px 0; display: inline-block; margin: 0 0.1em; line-height: 1.4; text-indent: 0em; } p img:not(.wp-smiley){ -webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 6px; max-width: 400px; width: expression(this.width > 400 ? "400px" : true); height: auto; } a{ color: #12addb; } </style> <div style="color: #555; font-size: 12px; font-family: \'Segoe UI\', \'Segoe\', \'Segoe WP\', \'Tahoma\', \'Microsoft YaHei\', sans-serif; -webkit-font-smoothing: antialiased; width: 600px; margin:0 auto; border: 1px solid #e9e9e9; border-top: none;"> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr valign="top" height="2"> <td width="190" bgcolor="#0B9938"></td> <td width="120" bgcolor="#9FCE67"></td> <td width="85" bgcolor="#EDB113"></td> <td width="85" bgcolor="#FFCC02"></td> <td width="130" bgcolor="#5B1301" valign="top"></td> </tr> </tbody> </table> <div style="padding:0 15px 8px"> <h2 style="border-bottom: 1px solid #e9e9e9; font-size: 14px; font-weight: normal; padding: 10px 0 10px;"><span style="color: #12addb;">> </span>「<a style="text-decoration:none;color:#12addb" href="' . get_option("home") . '" title="' . get_option("blogname") . '" target="_blank">' . get_option("blogname") . '</a>」部落格有新文章發表啦!</h2> <div style="font-size:12px;color:#777;padding:0 10px;margin-top:18px"><div class="im"> <p>「<a style="text-decoration:none; color:#12addb" href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a>」之站長您好!</p> <p>「<a style="text-decoration:none;color:#12addb" href="' . get_option("home") . '" title="' . get_option("blogname") . '" target="_blank">' . get_option("blogname") . '</a>」之文章更新通知已成功發送給'.$count_jiucool.'位訪客:</p> <p style="background-color:#f5f5f5;padding:10px 15px;margin:18px 0">'.$content.'</p> </div> <p>堅持就是勝利,繼續為「<a style="text-decoration:none; color:#12addb" href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a>」加油努力吧!</p> </div> </div> <div style="color:#888;padding:10px;border-top:1px solid #e9e9e9;background:#f5f5f5"> <p style="margin:0;padding:0"><span style="color:#BDBDBD">© <a style="color: #888; text-decoration: none;" href="' . get_option("home") . '" title="' . get_option("blogname") . '" target="_blank">' . get_option("blogname") . '</a> 請添加 <a style="color:#BDBDBD" href="mailto:'.get_option('admin_email').'">'.get_option('admin_email').'</a> 至您的通訊錄,以便接收本站郵件,謝謝!</span></p> </div>'; mail_to_commenter(get_option("admin_email"), '更新文章通知發送報告', $content); } } add_action('publish_post', 'mail_to_commenters_when_post', 5, 1); /* 向留言者發送新文章郵件通知結束(由AREFLY.COM製作) */ |
2、然後我們需要上傳一個「退訂頁面」,將下列代碼儲存為「unsubscribe.php」並上傳至WordPress根目錄:
注意:你需要將 $table_prefix
之值替換為你的WordPress數據庫前綴!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<?php $table_prefix = 'wp_'; //數據庫前綴 require_once('wp-config.php'); ?> <!DOCTYPE html> <html lang="zh-TW"> <head> <meta charset="UTF-8"> <meta name="robots" content="noindex" /> <title>退訂文章訂閱</title> <script> function confirmunsubscribe(){ var msg = "您確定要退訂新文章郵件訂閱麼?"; if (confirm(msg)==true){ var url = window.location.href; window.location.href = url + "&confirm=yes"; }else{ return; } } </script> </head> <body> <?php $unsubscribeEmail =$_GET['email']; echo '您的郵箱地址是:'.$unsubscribeEmail."<p />"; $code = md5($unsubscribeEmail); if($_GET['code']== $code){ if($_GET["confirm"] == "yes"){ $db = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("對不起!數據庫連接錯誤!"); mysql_select_db(DB_NAME); $query = mysql_query("select * from jiucool_mail_exclude where email='$unsubscribeEmail'"); if(mysql_num_rows($query)>0){ echo "<script>alert('您之前已經成功退訂,請不要重復提交!'); </script>"; echo "<script>window.location.href='/' </script>"; exit(); }else{ date_default_timezone_set("Asia/Shanghai"); $pub_date = date('Y-m-d H:i:s', strtotime(time())-60*60); $ip = get_ip(); $query = mysql_query("select * from jiucool_mail_exclude where ip='$ip' and pub_date>'$pub_date' "); if(mysql_num_rows($query)>0){ echo "<script>alert(\"對不起,您同一時間內退訂操作過快,請稍後再試!\"); </script>"; echo "<script>window.location.href='/' </script>"; exit(); }else{ $pub_date = date('Y-m-d H:i:s', time()); $query = mysql_query("insert into `jiucool_mail_exclude`(email,pub_date,ip)values('$unsubscribeEmail','$pub_date','$ip')"); echo "<script>alert('恭喜,您已成功退訂郵件!再次感謝您對本站的支持!'); </script>"; $table = $table_prefix."options"; $query = mysql_query("select * from $table where option_name='admin_email'"); while ($row = mysql_fetch_array($query)) { $subject = "提醒:郵箱「".$unsubscribeEmail . "」已退訂!"; $message .= $subject; toemail($row["option_value"], $subject, $message); } echo "<script>window.location.href='/' </script>"; exit(); } } }else{ echo "<input type='button' onclick='javascript:confirmunsubscribe();' value='確認退訂' />"; } }else{ echo "<script>alert('對不起,您無權進行本操作!'); </script>"; echo "<script>window.location.href='/' </script>"; } function get_ip (){ if ($_SERVER["HTTP_X_FORWARDED_FOR"]==""){ $user_ip=$_SERVER["REMOTE_ADDR"]; }else{ $user_ip=$_SERVER["HTTP_X_FORWARDED_FOR"]; } return $user_ip; } function toemail($to,$subject,$message){ $from_email = get_option('admin_email'); //發件人=站長郵箱 //$from_email = "no-reply@domain.com"; //去除本行注釋可修改發件人郵箱! $charset = "UTF-8"; $headers = "From: $from_email \n" ; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: text/html;charset=\"$charset\"\n"; $to = strtolower($to); @mail($to, $subject, $message, $headers) or die ("郵件發送失敗!"); } ?> </body> </html> |
3、現在當我們發表新文章時,網站就將會自動隨機抽取 $total_mail
(例如 50
)名訪客來發送「最新文章已發佈」郵件啦!