当前位置:首页 > CMS教程 > WordPress > 列表

Wordpress用户注册邮件内容自定义格式美化实例

发布:smiling 来源: PHP粉丝网  添加日期:2015-03-23 17:04:45 浏览: 评论:0 

WordPress站点,新用户注册成功后,系统会分别给网站管理员和新用户发送邮件通知,默认发送的邮件不太美观,现在我们来讲解一下如何自定义邮件内容的html格式.

WordPress提供了一个可供插件重新定义的新用户邮件通知函数 wp_new_user_notification(),现在我们来重新定义这个函数的内容,来美化我们的邮件内容格式.

原函数函数内容如下:

  1. if ( !function_exists('wp_new_user_notification') ) : 
  2. /** 
  3.  * Notify the blog admin of a new user, normally via email. 
  4.  * 
  5.  * @since 2.0 
  6.  * 
  7.  * @param int $user_id User ID 
  8.  * @param string $plaintext_pass Optional. The user's plaintext password 
  9.  */ 
  10. function wp_new_user_notification($user_id$plaintext_pass = '') { 
  11.     $user = get_userdata( $user_id ); 
  12.  
  13.     $user_login = stripslashes($user->user_login); 
  14.     $user_email = stripslashes($user->user_email); 
  15.  
  16.     // The blogname option is escaped with esc_html on the way into the database in sanitize_option 
  17.     // we want to reverse this for the plain text arena of emails. 
  18.     $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 
  19.  
  20.     $message  = sprintf(__('New user registration on your site %s:'), $blogname) . "rnrn"
  21.     $message .= sprintf(__('Username: %s'), $user_login) . "rnrn"
  22.     $message .= sprintf(__('E-mail: %s'), $user_email) . "rn"
  23.  
  24.     @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message); 
  25.  
  26.     if ( emptyempty($plaintext_pass) ) 
  27.         return
  28. //开源软件:phpfensi.com 
  29.     $message  = sprintf(__('Username: %s'), $user_login) . "rn"
  30.     $message .= sprintf(__('Password: %s'), $plaintext_pass) . "rn"
  31.     $message .= wp_login_url() . "rn"
  32.  
  33.     wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message); 
  34.  
  35. endif

自定义邮件内容和格式

我们可以新建一个"插件",重新定义的wp_new_user_notification函数定义的邮件内容即可,我们在wp-content/plugins/目录下,新建一个文本文件命名为new-user-notification.php,插入以下代码,保存,然后在后台启动插件new-user-notification即可,代码如下:

  1. <?php 
  2. /* 
  3.   Plugin Name: new-user-notification 
  4.   Description:重新定义发送邮件的内容和格式 
  5.   Version: 1.0 
  6.  */ 
  7.  
  8. if ( !function_exists('wp_new_user_notification') ) : 
  9. /** 
  10.  * Notify the blog admin of a new user, normally via email. 
  11.  * 
  12.  * @since 2.0 
  13.  * 
  14.  * @param int $user_id User ID 
  15.  * @param string $plaintext_pass Optional. The user's plaintext password 
  16.  */ 
  17. function wp_new_user_notification($user_id$plaintext_pass = '') { 
  18.     $user = get_userdata( $user_id ); 
  19.  
  20.     $user_login = stripslashes($user->user_login); 
  21.     $user_email = stripslashes($user->user_email); 
  22.  
  23.     // 获取博客名称 
  24.     $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 
  25.  
  26.     // 给管理员发送的邮件内容,这里是HTML格式 
  27.     $message = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>新用户注册</title></head><body><div align="center"><table cellpadding="0" cellspacing="1" style="border:3px solid #d9e9f1;background:#7fbddd; text-align:left;"><tr><td style="padding:0;"><table cellpadding="30" cellspacing="0" style="border:1px solid #ffffff;background:#f7f7f7;width:500px;"><tr><td style="line-height:2;font-size:12px;">您的网站 <strong>' . $blogname . '</strong> 有新用户注册。<br />用户名:' . $user_login . '<br />Email:' . $user_email . '<br /><br />如果您不是  <strong>' . $blogname . '</strong> 的管理员,请直接忽略本邮件!</div></td></tr></table></td></tr></table></div></body></html>'
  28.  
  29.     // 给网站管理员发送邮件 
  30.     $message_headers = "Content-Type: text/html; charset="utf-8"n"
  31.     @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message$message_headers); 
  32.  
  33.     if ( emptyempty($plaintext_pass) ) 
  34.         return
  35.     
  36.     // 你可以在此修改发送给新用户的通知Email,这里是HTML格式 
  37.     $message = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>新用户注册</title></head><body><div align="center"><table cellpadding="0" cellspacing="1" style="border:3px solid #d9e9f1;background:#7fbddd; text-align:left;"><tr><td style="padding:0;"><table cellpadding="30" cellspacing="0" style="border:1px solid #ffffff;background:#f7f7f7;width:500px;"><tr><td style="line-height:2;font-size:12px;">您刚刚在网站 <strong>' . $blogname . '</strong> 注册一个帐号。<br />用户名:' . $user_login . '<br />登录密码:' . $plaintext_pass . '<br />登录网址:<a href="' . wp_login_url() . '">' . wp_login_url() . '</a><br /><br />如果您没有在 <strong>'$blogname . '</strong> 注册过任何信息,请直接忽略本邮件!</div></td></tr></table></td></tr></table></div></body></html>'
  38.  
  39.     // sprintf(__('[%s] Your username and password'), $blogname) 为邮件标题 
  40.     wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message$message_headers); 
  41. endif
  42. ?>

Tags: Wordpress用户注册 Wordpress邮件

分享到: