Unable to override pluggable function wp_mail

Discussion in 'WordPress' started by Prahlad Yeri, May 31, 2014.

  1. #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');
    }
    PHP:
    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!');*/
    PHP:

    Trouble is 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. Can you help me with this ?
     
    Prahlad Yeri, May 31, 2014 IP
  2. TIEro

    TIEro Active Member

    Messages:
    741
    Likes Received:
    177
    Best Answers:
    5
    Trophy Points:
    70
    #2
    I'm no php genius, but won't starting the definition with "if (!function_exists('wp_mail'))" cause it never to fire (since wp_mail exists in core)?
     
    TIEro, May 31, 2014 IP
  3. Prahlad Yeri

    Prahlad Yeri Active Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    This is indeed the way you override the pluggable functions. See this codex link at the end: http://codex.wordpress.org/Pluggable_Functions
     
    Prahlad Yeri, May 31, 2014 IP