a2design-inc/sendgrid-webapi-cakephp-plugin

handle cc

Opened this issue · 3 comments

is it correct that SendgridTransport does not handle cc ?

It does, just not in the x-smtpapi parameter. See SendGrid API. See also my version which supports Cc and Bcc.

_cakeEmail = $email;

    $this->_config = $this->_cakeEmail->config();

    if (empty($this->_config['count']) || $this->_config['count'] > 500) {
        $this->_config['count'] = 500;
    }

    $this->_headers = $this->_cakeEmail->getHeaders();
    $this->_recipients = $email->to();
    $this->_to = $email->to();
    $this->_cc = $email->cc();
    $this->_bcc = $email->bcc();
    $this->_replyTo = $email->replyTo();

    return $this->_sendPart();

}

private function _sendPart() {

    $json = array(
        'category' => !empty($this->_headers['X-Category']) ? $this->_headers['X-Category'] : $this->_config['category'],
    );

    //Sendgrid Substitution Tags
    if (!empty($this->_headers['X-Sub'])) {
        foreach ($this->_headers['X-Sub'] as $key => $value) {
            $json['sub'][$key] = array_splice($value, 0, $this->_config['count']);
        }
    }

    $params = array(
        'api_user'  => $this->_config['username'],
        'api_key'   => $this->_config['password'],
        'x-smtpapi' => json_encode($json),
        'to'        => $this->_getAddress(array_splice($this->_to, 0, $this->_config['count'])),
        'cc'        => $this->_getAddress(array_splice($this->_cc, 0, $this->_config['count'])),
        'bcc'       => $this->_getAddress(array_splice($this->_bcc, 0, $this->_config['count'])),
        'subject'   => $this->_cakeEmail->subject(),
        'html'      => $this->_cakeEmail->message('html'),
        'text'      => $this->_cakeEmail->message('text'),
        'from'      => $this->_config['from'],
        'fromname'  => $this->_config['fromName'],
        // 'replyto'   => array_keys($this->_replyTo)[0],
        'replyto'   => 'email@email.com',
    );

    $attachments = $this->_cakeEmail->attachments();
    if (!empty($attachments)) {
        foreach ($attachments as $key => $value) {
            $params['files[' . $key . ']'] = '@' . $value['file'];
        }
    }

      json_decode($this->_exec($params));
}

private function _getAddress($addresses = array(), $asString = false) {

    $output = array();

    foreach($addresses as $key => $value) {
        $output[] = "$value <{$key}>";
    }

    if ($asString) {
        return implode(', ', $output);
    }
    else {
        return $output;
    }
}

private function _exec($params) {
    $request =  'https://api.sendgrid.com/api/mail.send.json';
    $email = new HttpSocket(array('ssl_verify_host' => false));
    $response = $email->post($request, $params);
    return $response->body;
}
```

}

above script works for me
tested in cakephp version 2.5.1