public function send(Email $email)
    {
        // Load SparkPost configuration settings
        $apiKey = $this->config('apiKey');

        // Set up HTTP request adapter
        $adapter = new CakeHttpAdapter(new Client());

        // Create SparkPost API accessor
        $sparkpost = new SparkPost($adapter, [ 'key' => $apiKey ]);

        // Pre-process CakePHP email object fields
        $from = (array) $email->from();
        $sender = sprintf('%s <%s>', mb_encode_mimeheader(array_values($from)[0]), array_keys($from)[0]);
        $to = (array) $email->to();
        $replyTo = $email->getReplyTo() ? array_values($email->getReplyTo())[0] : null;
        
        foreach ($to as $toEmail => $toName) {
            $recipients[] = ['address' => [ 'name' => mb_encode_mimeheader($toName), 'email' => $toEmail]];
        }

        // Build message to send
        $message = [
            'from' => $sender,
            'html' => empty($email->message('html')) ? $email->message('text') : $email->message('html'),
            'text' => $email->message('text'),
            'subject' => mb_decode_mimeheader($email->subject()),
            'recipients' => $recipients
        ];
        
        if ($replyTo) {
	    $message['replyTo'] = $replyTo;
	}

        // Send message
        try {
            $sparkpost->transmission->send($message);
        } catch(APIResponseException $e) {
            // TODO: Determine if BRE is the best exception type
            throw new BadRequestException(sprintf('SparkPost API error %d (%d): %s (%s)',
                $e->getAPICode(), $e->getCode(), ucfirst($e->getAPIMessage()), $e->getAPIDescription()));
        }
    }
public function send(Email $email)
    {
        // Load SparkPost configuration settings
        $apiKey = $this->config('apiKey');

        // Set up HTTP request adapter
        $adapter = new CakeHttpAdapter(new Client());

        // Create SparkPost API accessor
        $sparkpost = new SparkPost($adapter, [ 'key' => $apiKey ]);

        // Pre-process CakePHP email object fields
        $from = (array) $email->from();
        $sender = sprintf('%s <%s>', mb_encode_mimeheader(array_values($from)[0]), array_keys($from)[0]);
        $to = (array) $email->to();
        $replyTo = $email->getReplyTo() ? array_values($email->getReplyTo())[0] : null;
        
        foreach ($to as $toEmail => $toName) {
            $recipients[] = ['address' => [ 'name' => mb_encode_mimeheader($toName), 'email' => $toEmail]];
        }

        // Build message to send
        $message = [
            'from' => $sender,
            'html' => empty($email->message('html')) ? $email->message('text') : $email->message('html'),
            'text' => $email->message('text'),
            'subject' => mb_decode_mimeheader($email->subject()),
            'recipients' => $recipients
        ];
        
        if ($replyTo) {
	    $message['replyTo'] = $replyTo;
	}

        // Send message
        try {
            $sparkpost->transmission->send($message);
        } catch(APIResponseException $e) {
            // TODO: Determine if BRE is the best exception type
            throw new BadRequestException(sprintf('SparkPost API error %d (%d): %s (%s)',
                $e->getAPICode(), $e->getCode(), ucfirst($e->getAPIMessage()), $e->getAPIDescription()));
        }
    }
public function send ( Email $ email ) { // Load SparkPost configuration settings $ apiKey = $ this -> config ( 'apiKey' ) ; // Set up HTTP request adapter $ adapter = new CakeHttpAdapter ( new Client ( ) ) ; // Create SparkPost API accessor $ sparkpost = new SparkPost ( $ adapter , [ 'key' => $ apiKey ] ) ; // Pre-process CakePHP email object fields $ from = ( array ) $ email -> from ( ) ; $ sender = sprintf ( '%s <%s>' , mb_encode_mimeheader ( array_values ( $ from ) [ 0 ] ) , array_keys ( $ from ) [ 0 ] ) ; $ to = ( array ) $ email -> to ( ) ; $ replyTo = $ email -> getReplyTo ( ) ? array_values ( $ email -> getReplyTo ( ) ) [ 0 ] : null ; foreach ( $ to as $ toEmail => $ toName ) { $ recipients [ ] = [ 'address' => [ 'name' => mb_encode_mimeheader ( $ toName ) , 'email' => $ toEmail ] ] ; } // Build message to send $ message = [ 'from' => $ sender , 'html' => empty ( $ email -> message ( 'html' ) ) ? $ email -> message ( 'text' ) : $ email -> message ( 'html' ) , 'text' => $ email -> message ( 'text' ) , 'subject' => mb_decode_mimeheader ( $ email -> subject ( ) ) , 'recipients' => $ recipients ] ; if ( $ replyTo ) { $ message [ 'replyTo' ] = $ replyTo ; } // Send message try { $ sparkpost -> transmission -> send ( $ message ) ; } catch ( APIResponseException $ e ) { // TODO: Determine if BRE is the best exception type throw new BadRequestException ( sprintf ( 'SparkPost API error %d (%d): %s (%s)' , $ e -> getAPICode ( ) , $ e -> getCode ( ) , ucfirst ( $ e -> getAPIMessage ( ) ) , $ e -> getAPIDescription ( ) ) ) ; } }
Send mail via SparkPost REST API

@param \Cake\Mailer\Email $email Email message
@return array
Send mail via SparkPost REST API