1

A couple of days ago, my wordpress blog hosted on openshift suddenly stopped sending me mails and notifications. Until now I'd never bothered how wordpress handled mail sending since they always worked. Now, after going through codex, etc I came to know that wordpress handles this by calling the wp_mail(), then @mail() php function which ultimately calls the unix sendmail. Since I don't want to depend on sendmail/openshift, I decided to override the wp_mail pluggable function in my custom plugin, so that it calls my own sendgrid function instead of using the core one. Here is my code for plugin:

<?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";

And here is the include file sendgrid.php (I tested it individually on CLI and it works fine):

<?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'      => 'myself@yahoo.com',
  );


    $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      = 'myself@yahoo.com';
$subject = 'Testemail';
$message = 'It works!!';
echo 'To is: ' + $to;
//wp_mail( $to, $subject, $message, array() );
sendgridmail($to, $subject, $message, $headers);
print_r('Just sent!');*/

Trouble is that this doesn't seem to be getting called by wordpress. It did work once or twice when I initially tested it yesterday, but after that its no longer working. Looks like wordpress is still calling the core wp_mail function instead of calling this one. Any idea folks ?

1

I think this line:

namespace MailDemo;

is the reason for you problems.

You define your custom wp_mail() function within this namespace, so it looks like you're trying to override the function:

\MailDemo\wp_mail() 

but not the pluggable function:

\wp_mail()

Try to remove the namespace setup and see what happens.

Another way around this would be to include the override code part from a file, for example:

require_once plugin_dir_path( __FILE__ ) . 'override_wp_mail.php';

to have it in the global scope for the WordPress core.

  • 1
    as alternative, mantain the namespace and somewhere add a non-namespaced function like function wp_mail() { return call_user_func_array( '\MailDemo\wp_mail', func_get_args() ); } – gmazzap May 31 '14 at 9:10
  • @birgire Removing the namespace didn't work. Regarding the second "require_once" statement, in which file should I include that ? – Prahlad Yeri May 31 '14 at 9:11
  • 1
    Finally got solved after I removed the namespace and the "NAMESPACE ." expression. The thing is that I'm on PHP 5.4 and the openshift site is still using 5.3.3. Looks like the live php doesn't like namespaces! – Prahlad Yeri May 31 '14 at 11:49
  • You can add the wp_mail() definition in a file or like @G.M. nicely suggested, ... aha, you just commented before be ;-) Ok, glad to hear the mystery is solved ;-) yes by namespace setup, I meant everyting related to it, I should have expressed that in more detail ;-) – birgire May 31 '14 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.