[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Mail adapter that uses Mailgun's web API to deliver email. 5 */ 6 final class PhabricatorMailImplementationMailgunAdapter 7 extends PhabricatorMailImplementationAdapter { 8 9 private $params = array(); 10 private $attachments = array(); 11 12 public function setFrom($email, $name = '') { 13 $this->params['from'] = $email; 14 $this->params['from-name'] = $name; 15 return $this; 16 } 17 18 public function addReplyTo($email, $name = '') { 19 if (empty($this->params['reply-to'])) { 20 $this->params['reply-to'] = array(); 21 } 22 $this->params['reply-to'][] = "{$name} <{$email}>"; 23 return $this; 24 } 25 26 public function addTos(array $emails) { 27 foreach ($emails as $email) { 28 $this->params['tos'][] = $email; 29 } 30 return $this; 31 } 32 33 public function addCCs(array $emails) { 34 foreach ($emails as $email) { 35 $this->params['ccs'][] = $email; 36 } 37 return $this; 38 } 39 40 public function addAttachment($data, $filename, $mimetype) { 41 $this->attachments[] = array( 42 'data' => $data, 43 'name' => $filename, 44 'type' => $mimetype, 45 ); 46 47 return $this; 48 } 49 50 public function addHeader($header_name, $header_value) { 51 $this->params['headers'][] = array($header_name, $header_value); 52 return $this; 53 } 54 55 public function setBody($body) { 56 $this->params['body'] = $body; 57 return $this; 58 } 59 60 public function setHTMLBody($html_body) { 61 $this->params['html-body'] = $html_body; 62 return $this; 63 } 64 65 public function setSubject($subject) { 66 $this->params['subject'] = $subject; 67 return $this; 68 } 69 70 public function supportsMessageIDHeader() { 71 return true; 72 } 73 74 public function send() { 75 $key = PhabricatorEnv::getEnvConfig('mailgun.api-key'); 76 $domain = PhabricatorEnv::getEnvConfig('mailgun.domain'); 77 $params = array(); 78 79 $params['to'] = implode(', ', idx($this->params, 'tos', array())); 80 $params['subject'] = idx($this->params, 'subject'); 81 $params['text'] = idx($this->params, 'body'); 82 83 if (idx($this->params, 'html-body')) { 84 $params['html'] = idx($this->params, 'html-body'); 85 } 86 87 $from = idx($this->params, 'from'); 88 if (idx($this->params, 'from-name')) { 89 $params['from'] = "{$this->params['from-name']} <{$from}>"; 90 } else { 91 $params['from'] = $from; 92 } 93 94 if (idx($this->params, 'reply-to')) { 95 $replyto = $this->params['reply-to']; 96 $params['h:reply-to'] = implode(', ', $replyto); 97 } 98 99 if (idx($this->params, 'ccs')) { 100 $params['cc'] = implode(', ', $this->params['ccs']); 101 } 102 103 foreach (idx($this->params, 'headers', array()) as $header) { 104 list($name, $value) = $header; 105 $params['h:'.$name] = $value; 106 } 107 108 $future = new HTTPSFuture( 109 "https://api:{$key}@api.mailgun.net/v2/{$domain}/messages", 110 $params); 111 $future->setMethod('POST'); 112 113 foreach ($this->attachments as $attachment) { 114 $future->attachFileData( 115 'attachment', 116 $attachment['data'], 117 $attachment['name'], 118 $attachment['type']); 119 } 120 121 list($body) = $future->resolvex(); 122 123 $response = json_decode($body, true); 124 if (!is_array($response)) { 125 throw new Exception("Failed to JSON decode response: {$body}"); 126 } 127 128 if (!idx($response, 'id')) { 129 $message = $response['message']; 130 throw new Exception("Request failed with errors: {$message}."); 131 } 132 133 return true; 134 } 135 136 }
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 |