forked from Nikeev/drupal_sendgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendgrid.module
176 lines (158 loc) · 4.63 KB
/
sendgrid.module
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
<?php
/**
* @file
* Enables Drupal to send email directly through Sendgrid.
*/
define('SENDGRID_TEST_API_KEY', 'undefined');
define('SENDGRID_QUEUE', 'sendgrid_queue');
define('SENDGRID_EMAIL_REGEX', '/^\s*(.+?)\s*<\s*([^>]+)\s*>$/');
/**
* Implements hook_help().
*/
function sendgrid_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/help#sendgrid':
$output = t('Allow for site emails to be sent through Sendgrid.');
}
return $output;
}
/**
* Access callback for sending test email.
*
* @return bool
* True if current user has access to send test messages
*/
function sendgrid_test_access() {
$has_permission = \Drupal::currentUser()->hasPermission('administer sendgrid');
$key = \Drupal::config('sendgrid.settings')->get('sendgrid_api_key');
return $has_permission & !empty($key);
}
/**
* Implements hook_mail().
*/
function sendgrid_mail($key, &$message, $params) {
if ($key == 'test') {
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
if ($params['include_attachment']) {
$message['attachments'][] = \Drupal::service('file_system')->realpath('core/misc/druplicon.png');
$message['body'] .= ' ' . t('The Drupal icon is included as an attachment to test the attachment functionality.');
}
}
}
/**
* Calls the API send message.
*
* This is the default function used by sendgrid_mailsend().
*
* @param array $message
* Associative array containing message data.
*
* @return array
* Results of sending the message.
*
* @throws Sendgrid_Error
*
*/
function sendgrid_sender_plain($message) {
$api = \Drupal::service('sendgrid.api');
return $api->send($message);
}
/**
* Display the names of the modules that are using Mailsystem.
*
* This is consistent with with Mailsystem's display. In the future, if
* Mailsystem were to provide an API for their labeling, that should be used.
*
* @return array
* Array of all module names indexing to their "display" names,
* and some special items for non-module values like null, default-system,
* and some clarification talked onto the end of the Sendgrid module's name.
*/
function sendgrid_get_module_key_names() {
$name_array = array(
'' => '--none--',
'default-system' => "Site-wide default",
);
$descriptions = array();
foreach (system_rebuild_module_data() as $item) {
if ($item->status && !empty($item->info['name'])) {
$descriptions[strtolower($item->info['name'])] = (empty($item->info['package']) ? '' : $item->info['package']) . ' » ' . t(':module module', array(':module' => $item->info['name']));
}
}
asort($descriptions);
$mailsystem_config = \Drupal::service('config.factory')->getEditable('mailsystem.settings');
$modules = $mailsystem_config->get('modules');
foreach (array_keys($modules) as $module_name) {
foreach ($modules[$module_name] as $key => $options) {
$id = $module_name . '_' . $key;
$title = preg_replace('/^.* » /', '', $descriptions[$module_name]);
$title .= " ($key key)";
$name_array[$id] = $title;
}
}
return $name_array;
}
/**
* Helper to return a comma delimited list of mail keys to not log content for.
*
* @return string
* a comma delimited list of mail keys
*/
function sendgrid_mail_key_blacklist() {
return \Drupal::config('sendgrid.settings')->get('sendgrid_mail_key_blacklist');
}
/**
* Helper to generate an array of recipients.
*
* @param mixed $to
* a comma delimited list of email addresses in 1 of 2 forms:
* any number of names <[email protected]>
*
* @return array
* array of email addresses
*/
function sendgrid_get_to($to) {
$recipients = array();
$to_array = explode(',', $to);
foreach ($to_array as $email) {
if (preg_match(SENDGRID_EMAIL_REGEX, $email, $matches)) {
$recipients[] = array(
'email' => $matches[2],
'name' => $matches[1],
);
}
else {
$recipients[] = array('email' => $email);
}
}
return $recipients;
}
/**
* Determine if mail should be processed asynchronously.
*
* @return bool
* True if asyncronous processing is enabled
*/
function sendgrid_process_async() {
return \Drupal::config('sendgrid.settings')->get('sendgrid_process_async');
}
/**
* Returns an array containing the from information for a Sendgrid message.
*
* @return array
* array(
* 'email' => '[email protected]',
* 'name' => 'My Site',
* )
*/
function sendgrid_from() {
$email = \Drupal::config('sendgrid.settings')->get('sendgrid_from_email');
$name = \Drupal::config('sendgrid.settings')->get('sendgrid_from_name');
return array(
'email' => $email,
'name' => $name,
);
}