1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 
<?php
class Inbound_Mailer_SparkPost extends Inbound_Mail_Daemon {
    
    public static function send_email( $send_now = false) {
        $settings = Inbound_Mailer_Settings::get_settings();
        $sparkpost = new Inbound_SparkPost(  $settings['sparkpost-key'] );
        if ( !$send_now && self::$row->datetime ) {
            $send_at = date( 'c' , strtotime( self::$row->datetime ) );
            if (isset(self::$email_settings['timezone'])) {
                $date_parts = explode('+' , $send_at );
                $timezone_parts = explode('UTC' , self::$email_settings['timezone'] );
                $send_at = $date_parts[0] .$timezone_parts[1].':00';
            }
        } else {
            $send_at = 'now';
        }
        
        if (isset(self::$email['is_test'])) {
            $campaign_id = 'test';
        } else {
            $campaign_id = self::$row->email_id . '_' . self::$row->variation_id;
        }
        $message_args = array(
            'recipients'         => array( 
                array(
                    'address'           => array( 
                        'email'     => self::$email['send_address'], 
                        'name'      => null, 
                        'header_to' => null, 
                    ),
                    'return_path'       => null, 
                    'tags'              => self::$tags[ self::$row->email_id ], 
                    'metadata'          => null, 
                    'substitution_data' => null, 
                ),
            ),
            'content'            => array( 
                'html'          => self::$email['body'], 
                'text'          => null, 
                'subject'       => self::$email['subject'], 
                'from'          => array( 
                    'email' => self::$email['from_email'],
                    'name'  => self::$email['from_name'],
                ),
                'reply_to'      => self::$email['reply_email'], 
                'headers'       => null, 
                'attachments'   => array(), 
                'inline_images' => array(), 
            ),
            
            'options'            => array( 
                'start_time'       => $send_at, 
                'open_tracking'    => true, 
                'click_tracking'   => true, 
                'transactional'    => true, 
                'sandbox'          => false, 
                'skip_suppression' => null, 
                'inline_css'       => null, 
            ),
            'headers'            => array(
                'Content-type'  => 'application/json',
                'Authorization' =>  $settings['sparkpost-key'],
                'User-Agent'    => 'sparkpost-inbound',
            ),
            'description'        => null, 
            'campaign_id'        => $campaign_id , 
            'metadata'           => array(
                'email_id' => self::$row->email_id,
                'lead_id' => self::$row->lead_id,
                'variation_id' => self::$row->variation_id,
                'rule_id' => self::$row->rule_id,
                'job_id' => self::$row->job_id,
                'nature' => self::$email_settings['email_type']
            ), 
            'substitution_data'  => null, 
            'return_path'        => null, 
            'template_id'        => null, 
            'use_draft_template' => null, 
        );
        
        self::$response = $sparkpost->send( $message_args );
        do_action( 'mailer/sparkpost/send' , $message_args  );
    }
}