-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsendgrid_integration.module
More file actions
257 lines (232 loc) · 6.88 KB
/
sendgrid_integration.module
File metadata and controls
257 lines (232 loc) · 6.88 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* @file
* Main module file for SendGrid Integration.
*
* Provides module configuration and help functionality.
*/
define('SENDGRID_INTEGRATION_EMAIL_REGEX', '/^\s*"?(.+?)"?\s*<\s*([^>]+)\s*>$/');
/**
* Implements hook_config_info().
*/
function sendgrid_integration_config_info() {
$prefixes['sendgrid_integration.settings'] = array(
'label' => t('Sendgrid Integration Settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_xautoload().
*/
function sendgrid_integration_xautoload($adapter) {
if (class_exists(SendGrid\Client::class)) {
return;
}
$dir = backdrop_get_path('module', 'sendgrid_integration') . '/vendor/composer';
if (file_exists($dir)) {
$adapter->absolute()->composerDir($dir);
}
}
/**
* Implements hook_menu().
*/
function sendgrid_integration_menu() {
$items = [];
$items['admin/config/services/sendgrid'] = [
'title' => 'SendGrid Settings',
'description' => 'SendGrid Integration Settings',
'page callback' => 'backdrop_get_form',
'page arguments' => ['sendgrid_integration_admin'],
'access callback' => 'user_access',
'access arguments' => ['administer sendgrid settings'],
'file' => 'sendgrid_integration.admin.inc',
'type' => MENU_NORMAL_ITEM,
'weight' => 1,
];
$items['admin/config/services/sendgrid/default'] = [
'title' => 'Administer SendGrid Integration',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
];
$items['admin/config/services/sendgrid/test'] = [
'title' => 'SendGrid Test Email Send',
'description' => 'Send a test email through sendgrid.',
'page callback' => 'backdrop_get_form',
'page arguments' => ['sendgrid_integration_test'],
'access callback' => 'user_access',
'access arguments' => ['administer sendgrid settings'],
'file' => 'sendgrid_integration.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 50,
];
return $items;
}
/**
* Implements hook_permissions().
*/
function sendgrid_integration_permission() {
return [
'administer sendgrid settings' => [
'title' => t('Administer SendGrid settings'),
'description' => t('Configure SendGrid options like username and password'),
],
];
}
/**
* Implements hook_cron_queue_info().
*/
function sendgrid_integration_cron_queue_info() {
$queues = [];
$queues['SendGridResendQueue'] = [
'worker callback' => 'sendgrid_integration_email_resend',
'time' => 60,
];
return $queues;
}
/**
* Cron queue worker callback function.
* Tries to send email again, if it previously failed with good reason.
*/
function sendgrid_integration_email_resend($message) {
// Defining parameters first to improve code readability.
$module = $message['module'];
$key = $message['key'];
$to = $message['to'];
$language = $message['language'];
$params = $message['params'];
$from = $message['from'];
$send = TRUE;
backdrop_mail($module, $key, $to, $language, $params, $from, $send);
}
/**
* Get list of bounced emails. This is unused and may be come deprecated.
*
* @param string $start_date
* Start date in format of YYYY-MM-DD.
*
* @param string $end_date
* End date in format of YYYY-MM-DD.
*
* @param string $type
* Optional type, either 'hard' or 'soft'.
*
* @param string $email
* Optional email address used to searching for.
*
* @return array
* List of bounces.
*/
function sendgrid_integration_get_bounces($start_date, $end_date, $type = NULL, $email = NULL) {
// Connection url for bounces.
// @TODO update to V3.
$config = config('sendgrid_integration.settings');
$server = "https://sendgrid.com/api/bounces.get.json";
$user = $config->get('sendgrid_integration_username');
$key = sendgrid_integration_get_api_key();
if (isset($start_date) == FALSE || isset($end_date) == FALSE) {
watchdog('sendgrid_integration', 'Error on calling get_bounches: Missing date parameter', [], WATCHDOG_NOTICE);
return array();
}
$data = [
'date' => 1,
'start_date' => $start_date,
'end_date' => $end_date,
'api_user' => $user,
'api_key' => $key,
];
if (isset($type)) {
$data['type'] = $type;
}
if (isset($email)) {
$data['email'] = $email;
}
// Options for backdrop_http_request.
$options = [
'method' => 'GET',
'data' => http_build_query($data),
'timeout' => 30,
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
];
$response = backdrop_http_request($server, $options);
$list = backdrop_json_decode($response->data);
// Check if server returned error messages.
if (isset($list['error'])) {
watchdog('sendgrid_integration ', 'Error while retrieving bounces list: %error', ['%error' => $list['error']], WATCHDOG_NOTICE);
}
return $list;
}
/**
* Split an email address into it's name and address components.
*/
function sendgrid_integration_parse_address($email) {
if (preg_match(SENDGRID_INTEGRATION_EMAIL_REGEX, $email, $matches)) {
return [$matches[2], $matches[1]];
}
else {
return array($email, ' ');
}
}
/**
* Implements hook_mail().
*/
function sendgrid_integration_mail($key, &$message, $params) {
$message['module'] = 'sendgrid_integration';
$message['key'] = $key;
$message['subject'] = $params['subject'];
$message['body'] = explode(
MAIL_LINE_ENDINGS . MAIL_LINE_ENDINGS,
$params['body']
);
if ($params['include_test_attachment']) {
$message['attachments'][] = backdrop_realpath('core/misc/favicon.ico');
}
if (isset($params['Reply-To']) && !empty($params['Reply-To'])) {
$message['headers']['Reply-To'] = $params['Reply-To'];
}
}
/**
* Implements hook_autoload_info().
*/
function sendgrid_integration_autoload_info() {
$implements = array(
'SendGridMailSystem' => 'inc/sendgrid.mail.inc',
);
return $implements;
}
/**
* Helper to return the API Key for SendGrid.
*/
function sendgrid_integration_get_api_key() {
$config = config('sendgrid_integration.settings');
if (module_exists('key')) {
$apikey = $config->get('sendgrid_integration_secret_apikey');
$apikey = (empty($apikey)) ? '' : key_get_key_value($apikey);
}
else {
$apikey = $config->get('sendgrid_integration_apikey');
}
return $apikey ?? '';
}
/**
* Helper to check if the SendGrid library is available. First checks for
* Composer autoloading, then falls back to the bundled library if Composer
* isn't being used.
*
* @return bool
* TRUE if the library is available, FALSE otherwise.
*/
function sendgrid_integration_check_library() {
$library_exists = class_exists(SendGrid\Client::class);
if (!$library_exists) {
// If we're not using Composer, try to use the bundled library.
$dir = backdrop_get_path('module', 'sendgrid_integration') . '/vendor/composer';
if (module_exists('xautoload') && file_exists($dir)) {
xautoload()->adapter->composerDir($dir);
}
$library_exists = class_exists(SendGrid\Client::class);
}
return $library_exists;
}