-
Notifications
You must be signed in to change notification settings - Fork 2
/
messaging_sendgrid.inc
188 lines (168 loc) · 5.27 KB
/
messaging_sendgrid.inc
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
// $Id: messaging_method_mail.class.inc,v 1.1.2.1 2010/06/04 17:57:11 jareyero Exp $
/**
* @file
* Drupal Messaging Framework - Send_Method class file
*/
/**
* Messaging Send Method: Sendgrid mass mailing
*/
class Messaging_Sendgrid_Method extends Messaging_HTML_Mail_Method {
// Default group and address type
public $method = 'sendgrid';
/**
* Send message to multiple destinations
*
* @todo For single destination, things could go faster
*/
function send_multiple($destinations, $message) {
$results = array();
// In this case we use bulk sending features and fill in the results with the same value
$mail = $this->bulk_build($destinations, $message);
$result = $this->mail_send($mail, $message);
$results = array_combine(array_keys($destinations), array_fill(0, count($destinations), $result));
return $results;
}
/**
* Build bulk mail for multiple addresses
*
* Add the Sendgrid custom header which holds multiple recipient info and token replacement information
*/
function bulk_build($destinations, $message) {
// Disable token replacement for message
$template = $message->get_template();
// We do token replacement with tokens for available objects (no user or destination)
// But avoid to clear the ones that don't have a value yet
$template->set_option('clear', FALSE);
//$template->set_option('replace', FALSE);
// First build a message using the default mail as 'to' address
$defaults = $this->default_params();
$mail = $this->mail_build($defaults['default_from'], $message);
// Add recipients and tokens for replacement using Sendgrid headers
$recipients = $replaces = array();
foreach ($destinations as $destination) {
$recipients[] = $destination->get_address();
}
$header = new Messaging_Sendgrid_SmtpApiHeader();
$header->addTo($recipients);
$tokens = $this->bulk_tokens($destinations, $template->render('body'), $message->get_language());
foreach ($tokens as $key => $values) {
$header->addSubVal($key, array_values($values));
}
$mail['headers']['X-SMTPAPI'] = $header->as_string();
return $mail;
}
/**
* Build tokens for replacement
*
* @return array
* Tokens indexed by token name and destination index
*/
function bulk_tokens($destinations, $text, $language = NULL) {
$text_tokens = token_scan($text);
$user_tokens = array();
$options = array(
'language' => $language ? $language : language_default(),
'sanitize' => TRUE,
);
foreach ($destinations as $index => $destination) {
$objects = array(
'destination' => $destination,
'user' => $destination->get_user()
);
foreach ($text_tokens as $type => $tokens) {
$type_tokens = token_generate($type, $tokens, $objects, $options);
$type_tokens += array_combine($tokens, $tokens);
foreach ($type_tokens as $name => $value) {
$user_tokens[$name][$index] = $value;
}
}
}
return $user_tokens;
}
/**
* Get mail headers. Helper function for mail methods
*
* Just add some headers, but reuse most of default headers from parent
*/
static function mail_headers($message, $params = array()) {
$params += array('headers' => array());
$params['headers'] += array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
);
return parent::mail_headers($message, $params);
}
}
/**
* SmtpApiHeader from http://wiki.sendgrid.com/doku.php?id=smtpapiheader.php
*/
# Version 1.0
# Last Updated 6/22/2009
class Messaging_Sendgrid_SmtpApiHeader
{
var $data;
function addTo($tos)
{
if (!isset($this->data['to']))
{
$this->data['to'] = array();
}
$this->data['to'] = array_merge($this->data['to'], (array)$tos);
}
function addSubVal($var, $val)
{
if (!isset($this->data['sub']))
{
$this->data['sub'] = array();
}
if (!isset($this->data['sub'][$var]))
{
$this->data['sub'][$var] = array();
}
$this->data['sub'][$var] = array_merge($this->data['sub'][$var], (array)$val);
}
function setUniqueArgs($val)
{
if (!is_array($val)) return;
// checking for associative array
$diff = array_diff_assoc($val, array_values($val));
if(((empty($diff)) ? false : true))
{
$this->data['unique_args'] = $val;
}
}
function setCategory($cat)
{
$this->data['category'] = $cat;
}
function addFilterSetting($filter, $setting, $value)
{
if (!isset($this->data['filters']))
{
$this->data['filters'] = array();
}
if (!isset($this->data['filters'][$filter]))
{
$this->data['filters'][$filter] = array();
}
if (!isset($this->data['filters'][$filter]['settings']))
{
$this->data['filters'][$filter]['settings'] = array();
}
$this->data['filters'][$filter]['settings'][$setting] = $value;
}
function asJSON()
{
$json = json_encode($this->data);
// Add spaces so that the field can be foldd
$json = preg_replace('/(["\]}])([,:])(["\[{])/', '$1$2 $3', $json);
return $json;
}
function as_string()
{
$json = $this->asJSON();
$str = wordwrap($json, 76, "\n ");
return $str;
}
}