forked from pantheon-systems/smtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp.admin.inc
260 lines (238 loc) · 11.2 KB
/
smtp.admin.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
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
258
259
260
<?php
/**
* @file
* Administrative page code for the smtp module.
*
*/
/**
* Administrative settings.
*
* @return
* An array containing form items to place on the module settings page.
*/
function smtp_admin_settings() {
// Override the smtp_library variable.
if (module_exists('mimemail') &&
strpos(variable_get('smtp_library', ''), 'mimemail')) {
// don't touch smtp_library
}
else {
if (variable_get('smtp_on', 0)) {
$smtp_path = drupal_get_filename('module', 'smtp');
if ($smtp_path) {
variable_set('smtp_library', $smtp_path);
drupal_set_message(t('SMTP.module is active.'));
}
// If drupal can't find the path to the module, display an error.
else {
drupal_set_message(t("SMTP.module error: Can't find file."), 'error');
}
}
// If this module is turned off, delete the variable.
else {
variable_del('smtp_library');
drupal_set_message(t('SMTP.module is INACTIVE.'));
}
}
// Give info to user that this is a custom module for Pantheon sites with PEG.
drupal_set_message(t('This module has been customized for Pantheon Site to work with Pantheon Enterprise, using host 127.0.0.1 to route through the firewall to Client\'s external SMTP server IPs. The port should be set to the constant PANTHEON_SOIP_NATIONWIDE . The host 127.0.0.1:PANTHEON_SOIP_NATIONWIDE resolves to 216.71.144.16:25'), 'warning');
$form['onoff'] = array(
'#type' => 'fieldset',
'#title' => t('Install options'),
);
$form['onoff']['smtp_on'] = array(
'#type' => 'radios',
'#title' => t('Turn this module on or off'),
'#default_value' => variable_get('smtp_on', 0),
'#options' => array(1 => t('On'), 0 => t('Off')),
'#description' => t('To uninstall this module you must turn it off here first.'),
);
$form['onoff']['smtp_queue'] = array(
'#type' => 'checkbox',
'#title' => t('Send mail by queue'),
'#default_value' => variable_get('smtp_queue', FALSE),
'#description' => t('Mails will be sent by drupal queue api.'),
);
$form['server'] = array(
'#type' => 'fieldset',
'#title' => t('SMTP server settings'),
);
$form['server']['smtp_host'] = array(
'#type' => 'textfield',
'#title' => t('SMTP server'),
'#default_value' => variable_get('smtp_host', ''),
'#description' => t('The address of your outgoing SMTP server.'),
);
$form['server']['smtp_hostbackup'] = array(
'#type' => 'textfield',
'#title' => t('SMTP backup server'),
'#default_value' => variable_get('smtp_hostbackup', ''),
'#description' => t('The address of your outgoing SMTP backup server. If the primary server can\'t be found this one will be tried. This is optional.'),
);
$form['server']['smtp_port'] = array(
'#type' => 'textfield',
'#title' => t('SMTP port'),
'#size' => 100,
'#maxlength' => 100,
'#default_value' => variable_get('smtp_port', '25'),
'#description' => t('The default SMTP port is 25, if that is being blocked try 80. Gmail uses 465. See !url for more information on configuring for use with Gmail. You may provide a string PHP constant, which must contain the port number.', array('!url' => l(t('this page'), 'http://gmail.google.com/support/bin/answer.py?answer=13287'))),
);
$form['server']['smtp_backupport'] = array(
'#type' => 'textfield',
'#title' => t('SMTP backup port'),
'#size' => 100,
'#maxlength' => 100,
'#default_value' => variable_get('smtp_backupport', '25'),
'#description' => t('The default SMTP port is 25, if that is being blocked try 80. Gmail uses 465. See !url for more information on configuring for use with Gmail. You may provide a string PHP constant, which must contain the port number.', array('!url' => l(t('this page'), 'http://gmail.google.com/support/bin/answer.py?answer=13287'))),
);
// Only display the option if openssl is installed.
if (function_exists('openssl_open')) {
$encryption_options = array(
'standard' => t('No'),
'ssl' => t('Use SSL'),
'tls' => t('Use TLS'),
);
$encryption_description = t('This allows connection to an SMTP server that requires SSL encryption such as Gmail.');
}
// If openssl is not installed, use normal protocol.
else {
variable_set('smtp_protocol', 'standard');
$encryption_options = array('standard' => t('No'));
$encryption_description = t('Your PHP installation does not have SSL enabled. See the !url page on php.net for more information. Gmail requires SSL.', array('!url' => l(t('OpenSSL Functions'), 'http://php.net/openssl')));
}
$form['server']['smtp_protocol'] = array(
'#type' => 'select',
'#title' => t('Use encrypted protocol'),
'#default_value' => variable_get('smtp_protocol', 'standard'),
'#options' => $encryption_options,
'#description' => $encryption_description,
);
$form['auth'] = array(
'#type' => 'fieldset',
'#title' => t('SMTP Authentication'),
'#description' => t('Leave blank if your SMTP server does not require authentication.'),
);
$form['auth']['smtp_username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('smtp_username', ''),
'#description' => t('SMTP Username.'),
);
$form['auth']['smtp_password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#default_value' => variable_get('smtp_password', ''),
'#description' => t('SMTP password. If you have already entered your password before, you should leave this field blank, unless you want to change the stored password.'),
);
$form['email_options'] = array(
'#type' => 'fieldset',
'#title' => t('E-mail options'),
);
$form['email_options']['smtp_from'] = array(
'#type' => 'textfield',
'#title' => t('E-mail from address'),
'#default_value' => variable_get('smtp_from', ''),
'#description' => t('The e-mail address that all e-mails will be from.'),
);
$form['email_options']['smtp_fromname'] = array(
'#type' => 'textfield',
'#title' => t('E-mail from name'),
'#default_value' => variable_get('smtp_fromname', ''),
'#description' => t('The name that all e-mails will be from. If left blank will use the site name of:') . ' ' . variable_get('site_name', 'Drupal powered site'),
);
$form['email_options']['smtp_allowhtml'] = array(
'#type' => 'checkbox',
'#title' => t('Allow to send e-mails formated as Html'),
'#default_value' => variable_get('smtp_allowhtml', 0),
'#description' => t('Checking this box will allow Html formated e-mails to be sent with the SMTP protocol.'),
);
// If an address was given, send a test e-mail message.
$test_address = variable_get('smtp_test_address', '');
if ($test_address != '') {
// Clear the variable so only one message is sent.
variable_del('smtp_test_address');
global $language;
$params['subject'] = t('Drupal SMTP test e-mail');
$params['body'] = array(t('If you receive this message it means your site is capable of using SMTP to send e-mail.'));
drupal_mail('smtp', 'smtp-test', $test_address, $language, $params);
drupal_set_message(t('A test e-mail has been sent to @email. You may want to !check for any error messages.', array('@email' => $test_address, '!check' => l(t('check the logs'), 'admin/reports/dblog'))));
}
$form['email_test'] = array(
'#type' => 'fieldset',
'#title' => t('Send test e-mail'),
);
$form['email_test']['smtp_test_address'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address to send a test e-mail to'),
'#default_value' => '',
'#description' => t('Type in an address to have a test e-mail sent there.'),
);
$form['smtp_debugging'] = array(
'#type' => 'checkbox',
'#title' => t('Enable debugging'),
'#default_value' => variable_get('smtp_debugging', 0),
'#description' => t('Checking this box will print SMTP messages from the server for every e-mail that is sent.'),
);
$form['#submit'][] = 'smtp_admin_settings_form_submit';
return system_settings_form($form);
} // End of smtp_admin_settings().
/**
* Validation for the administrative settings form.
*
* @param $form
* An associative array containing the structure of the form.
* @param $form_state
* A keyed array containing the current state of the form.
*/
function smtp_admin_settings_validate($form, &$form_state) {
if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_host'] == '') {
form_set_error('smtp_host', t('You must enter an SMTP server address.'));
}
if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_port'] == '') {
form_set_error('smtp_port', t('You must enter an SMTP port number.'));
}
// If the port is a constant instead of a number, validate the constant
// exists.
if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_port'] != '' && !is_numeric($form_state['values']['smtp_port'])) {
$resolved_port = constant($form_state['values']['smtp_port']);
if($resolved_port == NULL ) {
form_set_error('smtp_port', t('SMTP port constant %port_constant is set to NULL or does not exist. Make sure the PHP constant exists.', array('%port_constant' => $form_state['values']['smtp_port'])));
} else {
drupal_set_message(t('SMTP port constant %port_constant has been resolved to %resolved_port', array('%port_constant' => $form_state['values']['smtp_port'], '%resolved_port' => $resolved_port)));
}
}
if ($form_state['values']['smtp_from'] && !valid_email_address($form_state['values']['smtp_from'])) {
form_set_error('smtp_from', t('The provided from e-mail address is not valid.'));
}
// If username is set empty, we must set both username/password empty as well.
if (empty($form_state['values']['smtp_username'])) {
$form_state['values']['smtp_password'] = '';
}
// A little hack. When form is presentend, the password is not shown (Drupal way of doing).
// So, if user submits the form without changing the password, we must prevent it from being reset.
elseif (empty($form_state['values']['smtp_password'])) {
unset($form_state['values']['smtp_password']);
}
} // End of smtp_admin_settings_validate().
/**
* Submit handler().
*/
function smtp_admin_settings_form_submit($form, &$form_state) {
// Check if SMTP status has been changed.
if (
(!variable_get('smtp_on', FALSE) && $form_state['values']['smtp_on']) ||
(variable_get('smtp_on', FALSE) && !$form_state['values']['smtp_on'])
) {
$mail_modes = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
// Turning on.
if ($form_state['values']['smtp_on']) {
variable_set('smtp_previous_mail_system', $mail_modes['default-system']);
$mail_modes['default-system'] = 'SmtpMailSystem';
}
// Turning off.
else {
$mail_modes['default-system'] = variable_get('smtp_previous_mail_system', 'DefaultMailSystem');
}
variable_set('mail_system', $mail_modes);
}
}