Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 189 lines (168 sloc) 5.396 kb
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
1 <?php
2 // $Id: messaging_method_mail.class.inc,v 1.1.2.1 2010/06/04 17:57:11 jareyero Exp $
3 /**
4 * @file
5 * Drupal Messaging Framework - Send_Method class file
6 */
7
8 /**
d5807d9 Added Mailsystem, missing headers...
Jose Reyero authored
9 * Messaging Send Method: Sendgrid mass mailing
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
10 */
b6d06a5 Changed sendgrid mail to use messaging_htmlmail
Jose Reyero authored
11 class Messaging_Sendgrid_Method extends Messaging_HTML_Mail_Method {
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
12 // Default group and address type
13 public $method = 'sendgrid';
14
15 /**
16 * Send message to multiple destinations
a495d5a Fixed sendgrid tokens for single destination. Formatting.
Jose Reyero authored
17 *
18 * @todo For single destination, things could go faster
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
19 */
20 function send_multiple($destinations, $message) {
21 $results = array();
a495d5a Fixed sendgrid tokens for single destination. Formatting.
Jose Reyero authored
22 // In this case we use bulk sending features and fill in the results with the same value
23 $mail = $this->bulk_build($destinations, $message);
24 $result = $this->mail_send($mail, $message);
25 $results = array_combine(array_keys($destinations), array_fill(0, count($destinations), $result));
ae2371b Added support for bulk mailing headers
Jose Reyero authored
26 return $results;
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
27 }
28
29 /**
ae2371b Added support for bulk mailing headers
Jose Reyero authored
30 * Build bulk mail for multiple addresses
31 *
32 * Add the Sendgrid custom header which holds multiple recipient info and token replacement information
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
33 */
ae2371b Added support for bulk mailing headers
Jose Reyero authored
34 function bulk_build($destinations, $message) {
b6d06a5 Changed sendgrid mail to use messaging_htmlmail
Jose Reyero authored
35 // Disable token replacement for message
36 $template = $message->get_template();
925d5bc Replacing available tokens before sending email, postponing only user…
Jose Reyero authored
37 // We do token replacement with tokens for available objects (no user or destination)
38 // But avoid to clear the ones that don't have a value yet
b6d06a5 Changed sendgrid mail to use messaging_htmlmail
Jose Reyero authored
39 $template->set_option('clear', FALSE);
925d5bc Replacing available tokens before sending email, postponing only user…
Jose Reyero authored
40 //$template->set_option('replace', FALSE);
b6d06a5 Changed sendgrid mail to use messaging_htmlmail
Jose Reyero authored
41
ae2371b Added support for bulk mailing headers
Jose Reyero authored
42 // First build a message using the default mail as 'to' address
43 $defaults = $this->default_params();
44 $mail = $this->mail_build($defaults['default_from'], $message);
dad29fd Using proper token names for sendgrid replacement and hook to add tokens
Jose Reyero authored
45 // Add recipients and tokens for replacement using Sendgrid headers
46 $recipients = $replaces = array();
ae2371b Added support for bulk mailing headers
Jose Reyero authored
47 foreach ($destinations as $destination) {
48 $recipients[] = $destination->get_address();
49 }
dad29fd Using proper token names for sendgrid replacement and hook to add tokens
Jose Reyero authored
50 $header = new Messaging_Sendgrid_SmtpApiHeader();
ae2371b Added support for bulk mailing headers
Jose Reyero authored
51 $header->addTo($recipients);
b6d06a5 Changed sendgrid mail to use messaging_htmlmail
Jose Reyero authored
52 $tokens = $this->bulk_tokens($destinations, $template->render('body'), $message->get_language());
4d75a63 Dropped hook_messaging_destination('tokens'), better way to get tokens
Jose Reyero authored
53 foreach ($tokens as $key => $values) {
50137cb Pre-render the message with other tokens
Jose Reyero authored
54 $header->addSubVal($key, array_values($values));
dad29fd Using proper token names for sendgrid replacement and hook to add tokens
Jose Reyero authored
55 }
a70386f Fixed heaer length
Jose Reyero authored
56 $mail['headers']['X-SMTPAPI'] = $header->as_string();
ae2371b Added support for bulk mailing headers
Jose Reyero authored
57 return $mail;
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
58 }
59
60 /**
4d75a63 Dropped hook_messaging_destination('tokens'), better way to get tokens
Jose Reyero authored
61 * Build tokens for replacement
62 *
63 * @return array
64 * Tokens indexed by token name and destination index
65 */
5b0cdd5 Added proper token parameters to get sanitized values
Jose Reyero authored
66 function bulk_tokens($destinations, $text, $language = NULL) {
4d75a63 Dropped hook_messaging_destination('tokens'), better way to get tokens
Jose Reyero authored
67 $text_tokens = token_scan($text);
68 $user_tokens = array();
5b0cdd5 Added proper token parameters to get sanitized values
Jose Reyero authored
69 $options = array(
70 'language' => $language ? $language : language_default(),
71 'sanitize' => TRUE,
72 );
4d75a63 Dropped hook_messaging_destination('tokens'), better way to get tokens
Jose Reyero authored
73 foreach ($destinations as $index => $destination) {
74 $objects = array(
75 'destination' => $destination,
76 'user' => $destination->get_user()
77 );
5b0cdd5 Added proper token parameters to get sanitized values
Jose Reyero authored
78
4d75a63 Dropped hook_messaging_destination('tokens'), better way to get tokens
Jose Reyero authored
79 foreach ($text_tokens as $type => $tokens) {
5b0cdd5 Added proper token parameters to get sanitized values
Jose Reyero authored
80 $type_tokens = token_generate($type, $tokens, $objects, $options);
4d75a63 Dropped hook_messaging_destination('tokens'), better way to get tokens
Jose Reyero authored
81 $type_tokens += array_combine($tokens, $tokens);
82 foreach ($type_tokens as $name => $value) {
83 $user_tokens[$name][$index] = $value;
84 }
85 }
86 }
87 return $user_tokens;
88 }
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
89
d5807d9 Added Mailsystem, missing headers...
Jose Reyero authored
90 /**
91 * Get mail headers. Helper function for mail methods
92 *
93 * Just add some headers, but reuse most of default headers from parent
94 */
95 static function mail_headers($message, $params = array()) {
96 $params += array('headers' => array());
97 $params['headers'] += array(
98 'MIME-Version' => '1.0',
99 'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
100 );
101 return parent::mail_headers($message, $params);
102 }
ae2371b Added support for bulk mailing headers
Jose Reyero authored
103 }
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
104
ae2371b Added support for bulk mailing headers
Jose Reyero authored
105 /**
106 * SmtpApiHeader from http://wiki.sendgrid.com/doku.php?id=smtpapiheader.php
107 */
108 # Version 1.0
109 # Last Updated 6/22/2009
110
111 class Messaging_Sendgrid_SmtpApiHeader
112 {
113 var $data;
114
115 function addTo($tos)
116 {
117 if (!isset($this->data['to']))
118 {
119 $this->data['to'] = array();
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
120 }
ae2371b Added support for bulk mailing headers
Jose Reyero authored
121 $this->data['to'] = array_merge($this->data['to'], (array)$tos);
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
122 }
ae2371b Added support for bulk mailing headers
Jose Reyero authored
123
124 function addSubVal($var, $val)
125 {
126 if (!isset($this->data['sub']))
127 {
128 $this->data['sub'] = array();
129 }
130
131 if (!isset($this->data['sub'][$var]))
132 {
133 $this->data['sub'][$var] = array();
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
134 }
ae2371b Added support for bulk mailing headers
Jose Reyero authored
135 $this->data['sub'][$var] = array_merge($this->data['sub'][$var], (array)$val);
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
136 }
ae2371b Added support for bulk mailing headers
Jose Reyero authored
137
138
139 function setUniqueArgs($val)
140 {
141 if (!is_array($val)) return;
142 // checking for associative array
143 $diff = array_diff_assoc($val, array_values($val));
144 if(((empty($diff)) ? false : true))
145 {
146 $this->data['unique_args'] = $val;
147 }
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
148 }
ae2371b Added support for bulk mailing headers
Jose Reyero authored
149
150 function setCategory($cat)
151 {
152 $this->data['category'] = $cat;
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
153 }
ae2371b Added support for bulk mailing headers
Jose Reyero authored
154
155 function addFilterSetting($filter, $setting, $value)
156 {
157 if (!isset($this->data['filters']))
158 {
159 $this->data['filters'] = array();
160 }
161
162 if (!isset($this->data['filters'][$filter]))
163 {
164 $this->data['filters'][$filter] = array();
165 }
166
167 if (!isset($this->data['filters'][$filter]['settings']))
168 {
169 $this->data['filters'][$filter]['settings'] = array();
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
170 }
ae2371b Added support for bulk mailing headers
Jose Reyero authored
171 $this->data['filters'][$filter]['settings'][$setting] = $value;
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
172 }
ae2371b Added support for bulk mailing headers
Jose Reyero authored
173
174 function asJSON()
175 {
176 $json = json_encode($this->data);
177 // Add spaces so that the field can be foldd
178 $json = preg_replace('/(["\]}])([,:])(["\[{])/', '$1$2 $3', $json);
179 return $json;
180 }
181
182 function as_string()
183 {
184 $json = $this->asJSON();
a70386f Fixed heaer length
Jose Reyero authored
185 $str = wordwrap($json, 76, "\n ");
ae2371b Added support for bulk mailing headers
Jose Reyero authored
186 return $str;
416fa58 Skeleton for Messaging sendgrid
Jose Reyero authored
187 }
ae2371b Added support for bulk mailing headers
Jose Reyero authored
188 }
Something went wrong with that request. Please try again.