-
Notifications
You must be signed in to change notification settings - Fork 624
/
Web.php
140 lines (116 loc) · 3.65 KB
/
Web.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace SendGrid;
class Web extends Api implements MailInterface
{
protected $domain = "http://sendgrid.com/";
protected $endpoint = "api/mail.send.json";
/**
* __construct
* Create a new Web instance
*/
public function __construct($username, $password)
{
call_user_func_array("parent::__construct", func_get_args());
}
/**
* _prepMessageData
* Takes the mail message and returns a url friendly querystring
* @param Mail $mail [description]
* @return String - the data query string to be posted
*/
protected function _prepMessageData(Mail $mail)
{
/* the api expects a 'to' parameter, but this parameter will be ignored
* since we're sending the recipients through the header. The from
* address will be used as a placeholder.
*/
$params =
array(
'api_user' => $this->username,
'api_key' => $this->password,
'subject' => $mail->getSubject(),
'html' => $mail->getHtml(),
'text' => $mail->getText(),
'from' => $mail->getFrom(),
'to' => $mail->getFrom(),
'x-smtpapi' => $mail->getHeadersJson()
);
if(($fromname = $mail->getFromName())) {
$params['fromname'] = $fromname;
}
if(($replyto = $mail->getReplyTo())) {
$params['replyto'] = $replyto;
}
// determine if we should send our recipients through our headers,
// and set the properties accordingly
if($mail->useHeaders())
{
// workaround for posting recipients through SendGrid headers
$headers = $mail->getHeaders();
$headers['to'] = $mail->getTos();
$mail->setHeaders($headers);
$params['x-smtpapi'] = $mail->getHeadersJson();
}
else
{
$params['to'] = $mail->getTos();
}
if($mail->getAttachments())
{
foreach($mail->getAttachments() as $attachment)
{
$params['files['.$attachment['filename'].'.'.$attachment['extension'].']'] = '@'.$attachment['file'];
}
}
return $params;
}
/**
* _arrayToUrlPart
* Converts an array to a url friendly string
* @param array $array - the array to convert
* @param String $token - the name of parameter
* @return String - a url part that can be concatenated to a url request
*/
protected function _arrayToUrlPart($array, $token)
{
$string = "";
if ($array)
{
foreach ($array as $value)
{
$string.= "&" . $token . "[]=" . urlencode($value);
}
}
return $string;
}
/**
* send
* Send an email
* @param Mail $mail - The message to send
* @return String the json response
*/
public function send(Mail $mail)
{
$data = $this->_prepMessageData($mail);
//if we're not using headers, we need to send a url friendly post
if(!$mail->useHeaders())
{
$data = http_build_query($data);
}
$request = $this->domain . $this->endpoint;
// we'll append the Bcc and Cc recipients to the url endpoint (GET)
// so that we can still post attachments (via cURL array).
$request.= "?" .
substr($this->_arrayToUrlPart($mail->getBccs(), "bcc"), 1) .
$this->_arrayToUrlPart($mail->getCcs(), "cc");
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $data);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
return $response;
}
}