几天前,我在openshift上的wordpress博客突然停止向我发送邮件和通知。到目前为止,我从未想过wordpress是如何处理邮件发送的,因为它们总是有效的。现在,在阅读了codex等之后,我知道wordpress通过调用wp\\u mail(),然后调用@mail()php函数来处理这个问题,该函数最终调用unix sendmail。由于我不想依赖sendmail/openshift,我决定重写wp\\u邮件pluggable 函数,以便它调用我自己的sendgrid函数,而不是使用核心函数。以下是我的插件代码:
<?php
/**
* Plugin Name: Sendgrid Plugin
* Plugin URI: http://www.prahladyeri.com
* Description: Mail sending using Sendgrid Web API
* Version: 0.1
* Author: Prahlad Yeri
* Author URI: http://www.prahladyeri.com
* Text Domain:
* Domain Path:
* Network:
* License: GPLv2
*/
namespace MailDemo;
require_once(\'sendgrid.php\');
add_action( \'init\', __NAMESPACE__ . \'\\plugin_init\' );
/**
* Plugin Name: Prahlad\'s mail
* Description: Alternative way to send a mail
*/
if (!function_exists(\'wp_mail\'))
{
file("http://".$_SERVER[\'SERVER_NAME\']."/logme.php?" . \'Iwill_Override\');
function wp_mail($to, $subject, $message, $headers = \'\', $attachments = array())
{
sendgridmail($to, $subject, $message, $headers);
}
file("http://".$_SERVER[\'SERVER_NAME\']."/logme.php?" . \'Iwas_Overridden\');
}
else
{
file("http://".$_SERVER[\'SERVER_NAME\']."/logme.php?" . \'Iwas_Not_Overridden\');
}
function plugin_init()
{
file("http://".$_SERVER[\'SERVER_NAME\']."/logme.php?" . \'Maildemo_Plugin_Init\');
}
//echo __NAMESPACE__ . "\\n";
这里是include文件sendgrid。php(我在CLI上单独测试了它,效果很好):
<?php
function sendgridmail($to, $subject, $message, $headers)
{
$url = \'https://api.sendgrid.com/\';
$user=\'myapikey\';
$pass=\'myapipassword\';
$params = array(
\'api_user\' => $user,
\'api_key\' => $pass,
\'to\' => $to,
\'subject\' => $subject,
\'html\' => \'\',
\'text\' => $message,
\'from\' => \'[email protected]\',
);
$request = $url.\'api/mail.send.json\';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
}
//only for testing:
/*$to = \'[email protected]\';
$subject = \'Testemail\';
$message = \'It works!!\';
echo \'To is: \' + $to;
//wp_mail( $to, $subject, $message, array() );
sendgridmail($to, $subject, $message, $headers);
print_r(\'Just sent!\');*/
问题是,wordpress似乎并没有调用此功能。昨天我刚开始测试时,它确实工作了一两次,但之后就不再工作了。wordpress似乎仍在调用核心wp\\U邮件函数,而不是调用此函数。各位有什么想法吗?