用BAE搭建WordPress博客小白完整教程(三)—–让WordPress发送邮件

用BAE搭建WordPress博客小白完整教程(三)—–让WordPress发送邮件

在这里我不得不说一下的是,在搭建WordPress完成后,你会获得一个管理员账号,账号绑定了你的邮件,在忘记密码的时候你可以用邮件找回,你也可以对其它人开放注册,是他们成为你博客的会员,但是问题在于,当我在BAE搭建完成后却总是接收不到Wordpress发给我的邮件,多方google,百度终于才知道,原来BAE提供了一个消息队列功能来实现发送邮件功能.

以下代码是从http://www.baiduyunblog.com/?p=1http://www.bjwilly.com/archives/425.html 摘取的                  

创建消息队列

把名称记下来,到BAE官方或者是在网上搜索BCMS-SDK,下载并解压到wp-includes目录中

现在你就可以配置Wordpress了

回到你的原来解压WordPress源码的文件夹下,找到wp-config-sample.php改成wp-config.php,用记事本或者是其它文本工具打开它,这里我喜欢用sublime text2,修改成如下(红色为修改或者是添加的部份)

<?php
 //创建的云数据库名称和申请的bucket和队列名称
$dbname = "xxxxxxxxxxx";
$table_prefix  = 'wp';

$bcs_bukect = 'xxxxxxxxxxxxxx';

$bcms_queue = 'xxxxxxxxxxxxx'

?>
<?php
//从环境变量中获得数据库的主机地址、用户名和密码
$ip = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');
$port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');
$host = $ip.":".$port;
$user = getenv('HTTP_BAE_ENV_AK');
$pass = getenv('HTTP_BAE_ENV_SK');
// ** MySQL 设置 – 具体信息来自您正在使用的主机 ** //
/** WordPress 数据库的名称 */
define('DB_NAME', $dbname);
/** MySQL 数据库用户名 */
define('DB_USER', $user);
/** MySQL 数据库密码 */
define('DB_PASSWORD', $pass);
/** MySQL 主机 */
define('DB_HOST', $host);
/*bucket名字*/
define('BCS_BUCKET', $bcs_bukect);
/*消息队列*/
define('BCMS_QUEUE',$bcms_queue);
/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');
/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');

接下来是要让WordPress使用BAE的Bucket来访问数据库

修改wp-admin/includes/file.php文件
找到$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );

删除下面的代码:
// Move the file to the uploads dir
   $new_file = $uploads['path'] . "/$filename";
   if ( false === @ move_uploaded_file( $file['tmp_name'],$new_file ) )
       return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ),$uploads['path'] ) );
   // Set correct file permissions
   $stat = stat( dirname( $new_file ));
   $perms = $stat['mode'] & 0000666;
   @ chmod( $new_file, $perms );
   // Compute the URL
   $url = $uploads['url'] . "/$filename";
   if ( is_multisite() )
       delete_transient( 'dirsize_cache' );
   return apply_filters( 'wp_handle_upload', array( 'file'=> $new_file,'url' => $url, 'type' => $type ), 'upload' );
———–添加下面的代码——————–
$tmp_file = wp_tempnam($filename);
// Move the file to the uploads dir
if ( false === @ move_uploaded_file( $file['tmp_name'],$tmp_file ) )
       return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ),$uploads['path'] ) );
// If a resize was requested, perform the resize.
$image_resize = isset( $_POST['image_resize'] ) && 'true'== $_POST['image_resize'];
$do_resize = apply_filters( 'wp_upload_resize',$image_resize );
$size = @getimagesize( $tmp_file );
if ( $do_resize && $size ) {
       $old_temp = $tmp_file;
       $tmp_file = image_resize( $tmp_file, (int) get_option('large_size_w'), (int) get_option('large_size_h'), 0, 'resized');
       if ( ! is_wp_error($tmp_file) ) {
           unlink($old_temp);
       } else {
           $tmp_file = $old_temp;
       }
   }
   //新版采用百度云存储
   //上传到云存储
   $bucket = constant('BCS_BUCKET');
   $opt = array();
   $baidu_bcs = new BaiduBCS();
   $object =  "/$filename";
   $fileUpload = $tmp_file;
   if( !file_exists($fileUpload) )
   {
       die('file is not existed!!!!!!!!!!!!!');
   }
   $re = $baidu_bcs->create_object ( $bucket, $object,$fileUpload, $opt);
   trigger_error( print_r($re, true) );
   unlink($tmp_file);
   $url = $baidu_bcs->generate_get_object_url($bucket,$object);
   //echo '??????'; exit;
   if ( is_multisite() )
       delete_transient( 'dirsize_cache' );
   //exit("$new_file     !!</br>      $url");
   return apply_filters( 'wp_handle_upload', array( 'file'=> $new_file,'url' => $url, 'type' => $type ), 'upload' );

添加消息队列的支持

在wp-includes\pluggable.php中查找if ( !function_exists( 'wp_mail' ) )

并在它的上面一行添加以下代码

/**
*Send Mail
*BCMS
*/
if( !function_exists('wp_mail') ) :
function wp_mail( $to, $subject, $message, $headers='', $attachments=array() ) {
   require_once ABSPATH . WPINC .'/Bcms.class.php';
   $bcms=new Bcms () ;
   $ret=$bcms->mail ( BCMS_QUEUE, $message, array($to), array( Bcms::MAIL_SUBJECT=> $subject)) ;
   if( false===$ret ) {
       returnfalse;
   }else{
       returntrue;
   }
}
endif;


所有的修改操作都 要记得保存哦~~

现在你就可以提交代码了,回到之前Checkout的代码的文件夹,右键Commit

不要忘记点All,把所有的文件都上传哦

几分钟后你的代码就上传成功了,上传过程中可能会让你输入账号和密码,就是你百度的账号和密码啦

现在就只差把你的博客网站发布到网上去了,回到版本管理页面

,在要上线版本的前面打上勾

如果看见当前状态是已上线.那恭喜你~你的博客网站已经上线啦,现在赶快在地址栏上输入创建应用时申请的域名,回车就可以安装你的博客啦~~

如果看见当前状态是发布失败,那可能是你在什么地方弄错了,点击查看错误,会列出错误是出在哪个文件的哪一行,看看是否少写或者是多写了什么东东?不行就把出错的文件重新覆盖过,重新修改一遍试试.是不是刚才修改完了忘记了保存就上传了?再次强调,我有时候也会范的错误,就是修改忘记保存已修改的文件,而且保存完文件一定要记得上传在SVN上,要不然BAE服务器也不知道你修改了什么东东

第一次登录WordPress会让你填一些管理员的信息,完成之后就可以开始你的Hello World博客啦~~

Leave a Reply

Your email address will not be published. Required fields are marked *