sendgrid / wordpress

SendGrid plugin for WordPress
51 stars 64 forks source link

FEATURE REQUEST: Add ability to enable sandbox mode #45

Open QWp6t opened 7 years ago

QWp6t commented 7 years ago

In order to maintain dev/prod parity, I keep this plugin enabled in development environments and test all of my forms based on the return value of wp_mail(). It would be helpful, however, if this plugin could give me the option to enable sandbox mode, preferably enabled via a constant. I can set a constant like define('SENDGRID_SANDBOX', true); or some such (this way I can set the value in a .env file and retrieve it per environment), in which case I would expect wp_mail to still return true or false depending on whether the email would successfully send, but similar to WordPress core API, I could retrieve the error via the wp_mail_failed action hook (or any other implementation will do, obviously).

Does this sound feasible?

wunderdojo commented 5 years ago

This is but I just came across the same thing so it appears it was never implemented in the plugin. In case anyone else needs to use Sandbox mode with the SendGrid WP plugin a quick hack to do so. In class-sendrid-api.php I just threw in a quick check for a locally defined constant:


if( defined( 'ENV_DEVELOPMENT' ) ){

      $body = json_decode( $data['body'] );

      $body->mail_settings = [
        'sandbox_mode' => [ 
          'enable' => true
        ]
      ];

      $data['body'] = json_encode( $body );

    }

That goes in at line 38 right before the call to wp_remote_post:

public function send(SendGrid\Email $email) {
    $data = array(
      'headers' => array(
        'Content-Type' => 'application/json',
        'User-Agent' => 'sendgrid/wordpress;php',
        'Authorization' => 'Bearer ' . $this->apikey
      ),
      'body' => Sendgrid_Translator::to_api_v3( $email ),
      'decompress' => false,
      'timeout' => Sendgrid_Tools::get_request_timeout()
    );

    //- quick hack to add in support for sandbox mode
    if( defined( 'ENV_DEVELOPMENT' ) ){

      $body = json_decode( $data['body'] );

      $body->mail_settings = [
        'sandbox_mode' => [ 
          'enable' => true
        ]
      ];

      $data['body'] = json_encode( $body );

    }

    // Send the request
    $response = wp_remote_post( self::URL, $data );
wolffe commented 5 years ago

This repository is not checked for bug reports or feature reports. Use the official site for this.

wunderdojo commented 5 years ago

@wolffe Which site are you referring to? Thanks.

wolffe commented 5 years ago

SendGrid, obviously. It's been like that for years.

I'm using the plugin myself and never had an issue, but I agree it's unmaintained and the codebase is a bit dated.