大家都知道很多网站带有「邮件订阅网站」的功能,不过大部分访客即使想订阅也会因繁琐的步骤望而却步,不过今天畅想资源就来教大家如何于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
)名访客来发送「最新文章已发布」邮件啦!