[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Mail adapter that uses SendGrid's web API to deliver email. 5 */ 6 final class PhabricatorMailImplementationSendGridAdapter 7 extends PhabricatorMailImplementationAdapter { 8 9 private $params = array(); 10 11 public function setFrom($email, $name = '') { 12 $this->params['from'] = $email; 13 $this->params['from-name'] = $name; 14 return $this; 15 } 16 17 public function addReplyTo($email, $name = '') { 18 if (empty($this->params['reply-to'])) { 19 $this->params['reply-to'] = array(); 20 } 21 $this->params['reply-to'][] = array( 22 'email' => $email, 23 'name' => $name, 24 ); 25 return $this; 26 } 27 28 public function addTos(array $emails) { 29 foreach ($emails as $email) { 30 $this->params['tos'][] = $email; 31 } 32 return $this; 33 } 34 35 public function addCCs(array $emails) { 36 foreach ($emails as $email) { 37 $this->params['ccs'][] = $email; 38 } 39 return $this; 40 } 41 42 public function addAttachment($data, $filename, $mimetype) { 43 if (empty($this->params['files'])) { 44 $this->params['files'] = array(); 45 } 46 $this->params['files'][$filename] = $data; 47 } 48 49 public function addHeader($header_name, $header_value) { 50 $this->params['headers'][] = array($header_name, $header_value); 51 return $this; 52 } 53 54 public function setBody($body) { 55 $this->params['body'] = $body; 56 return $this; 57 } 58 59 public function setHTMLBody($body) { 60 $this->params['html-body'] = $body; 61 return $this; 62 } 63 64 65 public function setSubject($subject) { 66 $this->params['subject'] = $subject; 67 return $this; 68 } 69 70 public function supportsMessageIDHeader() { 71 return false; 72 } 73 74 public function send() { 75 76 $user = PhabricatorEnv::getEnvConfig('sendgrid.api-user'); 77 $key = PhabricatorEnv::getEnvConfig('sendgrid.api-key'); 78 79 if (!$user || !$key) { 80 throw new Exception( 81 "Configure 'sendgrid.api-user' and 'sendgrid.api-key' to use ". 82 "SendGrid for mail delivery."); 83 } 84 85 $params = array(); 86 87 $ii = 0; 88 foreach (idx($this->params, 'tos', array()) as $to) { 89 $params['to['.($ii++).']'] = $to; 90 } 91 92 $params['subject'] = idx($this->params, 'subject'); 93 $params['text'] = idx($this->params, 'body'); 94 95 if (idx($this->params, 'html-body')) { 96 $params['html'] = idx($this->params, 'html-body'); 97 } 98 99 $params['from'] = idx($this->params, 'from'); 100 if (idx($this->params, 'from-name')) { 101 $params['fromname'] = $this->params['from-name']; 102 } 103 104 if (idx($this->params, 'reply-to')) { 105 $replyto = $this->params['reply-to']; 106 107 // Pick off the email part, no support for the name part in this API. 108 $params['replyto'] = $replyto[0]['email']; 109 } 110 111 foreach (idx($this->params, 'files', array()) as $name => $data) { 112 $params['files['.$name.']'] = $data; 113 } 114 115 $headers = idx($this->params, 'headers', array()); 116 117 // See SendGrid Support Ticket #29390; there's no explicit REST API support 118 // for CC right now but it works if you add a generic "Cc" header. 119 // 120 // SendGrid said this is supported: 121 // "You can use CC as you are trying to do there [by adding a generic 122 // header]. It is supported despite our limited documentation to this 123 // effect, I am glad you were able to figure it out regardless. ..." 124 if (idx($this->params, 'ccs')) { 125 $headers[] = array('Cc', implode(', ', $this->params['ccs'])); 126 } 127 128 if ($headers) { 129 // Convert to dictionary. 130 $headers = ipull($headers, 1, 0); 131 $headers = json_encode($headers); 132 $params['headers'] = $headers; 133 } 134 135 $params['api_user'] = $user; 136 $params['api_key'] = $key; 137 138 $future = new HTTPSFuture( 139 'https://sendgrid.com/api/mail.send.json', 140 $params); 141 $future->setMethod('POST'); 142 143 list($body) = $future->resolvex(); 144 145 $response = json_decode($body, true); 146 if (!is_array($response)) { 147 throw new Exception("Failed to JSON decode response: {$body}"); 148 } 149 150 if ($response['message'] !== 'success') { 151 $errors = implode(';', $response['errors']); 152 throw new Exception("Request failed with errors: {$errors}."); 153 } 154 155 return true; 156 } 157 158 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |