public function SettingsForm::buildForm in PHPMailer 8.3
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides ConfigFormBase::buildForm
File
- src/
Form/ SettingsForm.php, line 72
Class
- SettingsForm
- Defines a form that configures devel settings.
Namespace
Drupal\phpmailer\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('phpmailer.settings');
$form['smtp_on'] = [
'#type' => 'checkbox',
'#title' => t('Activate PHPMailer to send e-mails'),
'#default_value' => $config
->get('smtp_on'),
'#description' => t('When enabled, PHPMailer will be used to deliver all site e-mails.'),
];
/**
* @todo This part needs to be figured out.
*/
// Only allow to send all e-mails if Mime Mail is not configured the same
// (mimemail_alter is the counterpart to smtp_on).
// if (\Drupal::moduleHandler()->moduleExists('mimemail') && variable_get('mimemail_alter', 0)) {
// $form['smtp_on']['#disabled'] = TRUE;
// $form['smtp_on']['#default_value'] = 0;
// $form['smtp_on']['#description'] = t('MimeMail has been detected. To enable PHPMailer for mail transport, go to the <a href="@url">MimeMail settings page</a> and select PHPMailer from the available e-mail engines.', ['@url' => url('admin/config/system/mimemail')]);
// }
// elseif (!$config->get('smtp_on') && empty($form_state['input'])) {
if (!$config
->get('smtp_on') && empty($form_state->input)) {
drupal_set_message(t('PHPMailer is currently disabled.'), 'warning');
}
$form['server']['smtp_host'] = [
'#type' => 'textfield',
'#title' => t('Primary SMTP server'),
'#default_value' => $config
->get('smtp_host'),
'#description' => t('The host name or IP address of your primary SMTP server. Use %gmail-smtp for Google Mail.', [
'%gmail-smtp' => 'smtp.gmail.com',
]),
'#required' => TRUE,
];
$form['server']['smtp_hostbackup'] = [
'#type' => 'textfield',
'#title' => t('Backup SMTP server'),
'#default_value' => $config
->get('smtp_hostbackup'),
'#description' => t('Optional host name or IP address of a backup server, if the primary server fails. You may override the default port below by appending it to the host name separated by a colon. Example: %hostname', [
'%hostname' => 'localhost:465',
]),
];
$form['server']['smtp_port'] = [
'#type' => 'textfield',
'#title' => t('SMTP port'),
'#size' => 5,
'#maxlength' => 5,
'#default_value' => $config
->get('smtp_port'),
'#description' => t('The standard SMTP port is 25. Secure connections (including Google Mail), typically use 465.'),
'#required' => TRUE,
];
$form['server']['smtp_protocol'] = [
'#type' => 'select',
'#title' => t('Use secure protocol'),
'#default_value' => $config
->get('smtp_protocol'),
'#options' => [
'' => t('No'),
'ssl' => t('SSL'),
'tls' => t('TLS'),
],
'#description' => t('Whether to use an encrypted connection to communicate with the SMTP server. Google Mail requires SSL.'),
];
if (!function_exists('openssl_open')) {
$form['server']['smtp_protocol']['#default_value'] = '';
$form['server']['smtp_protocol']['#disabled'] = TRUE;
$form['server']['smtp_protocol']['#description'] .= ' ' . t('Note: This option has been disabled since your PHP installation does not seem to have support for OpenSSL.');
$config
->set('smtp_protocol', '')
->save();
}
$form['auth'] = [
'#type' => 'details',
'#title' => t('SMTP authentication'),
'#description' => t('Leave both fields empty, if your SMTP server does not require authentication.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['auth']['smtp_username'] = [
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => $config
->get('smtp_username'),
'#description' => t('For Google Mail, enter your username, including "@gmail.com".'),
];
if (!$config
->get('smtp_hide_password')) {
$form['auth']['smtp_password'] = [
'#type' => 'textfield',
'#title' => t('Password'),
'#default_value' => $config
->get('smtp_password'),
];
$form['auth']['smtp_hide_password'] = [
'#type' => 'checkbox',
'#title' => t('Hide password'),
'#default_value' => 0,
'#description' => t("Check this option to permanently hide the plaintext password from peeking eyes. You may still change the password afterwards, but it won't be displayed anymore."),
];
}
else {
$have_password = $config
->get('smtp_password') != '';
$form['auth']['smtp_password'] = [
'#type' => 'password',
'#title' => $have_password ? t('Change password') : t('Password'),
'#description' => $have_password ? t('Leave empty, if you do not intend to change the current password.') : '',
];
}
$form['advanced'] = [
'#type' => 'details',
'#title' => t('Advanced SMTP settings'),
];
$form['advanced']['smtp_fromname'] = [
'#type' => 'textfield',
'#title' => t('"From" name'),
'#default_value' => $config
->get('smtp_fromname'),
'#description' => t('Enter a name that should appear as the sender for all messages. If left blank the site name will be used instead: %sitename.', [
'%sitename' => $config
->get('site_name'),
]),
];
$form['advanced']['smtp_always_replyto'] = [
'#type' => 'checkbox',
'#title' => t('Always set "Reply-To" address'),
'#default_value' => $config
->get('smtp_always_replyto'),
'#description' => t('Enables setting the "Reply-To" address to the original sender of the message, if unset. This is required when using Google Mail, which would otherwise overwrite the original sender.'),
];
$form['advanced']['smtp_keepalive'] = [
'#type' => 'checkbox',
'#title' => t('Keep connection alive'),
'#default_value' => $config
->get('smtp_keepalive'),
'#description' => t('Whether to reuse an existing connection during a request. Improves performance when sending a lot of e-mails at once.'),
];
$form['advanced']['smtp_debug'] = [
'#type' => 'select',
'#title' => t('Debug level'),
'#default_value' => $config
->get('smtp_debug'),
'#options' => [
0 => t('Disabled'),
1 => t('Errors only'),
2 => t('Server responses'),
4 => t('Full communication'),
],
'#description' => t("Debug the communication with the SMTP server. You normally shouldn't enable this unless you're trying to debug e-mail sending problems."),
];
$form['advanced']['smtp_debug_log'] = [
'#type' => 'checkbox',
'#title' => t("Record debugging output in Drupal's log"),
'#default_value' => $config
->get('smtp_debug_log'),
'#description' => t("If this is checked, the debugging out put that is produced will also be recorded in Drupal's database log."),
'#states' => [
'visible' => [
':input[name=smtp_debug]' => [
'!value' => 0,
],
],
],
];
$form['advanced']['ssl_settings'] = [
'#type' => 'fieldset',
'#title' => t('Advanced SSL settings'),
'#description' => t('If you are attempting to connect to a broken or malconfigured secure mail server, you can try toggling one or more of these options. <strong>If changing any of these options allows connection to the server, you should consider either fixing the SSL certifcate, or using a different SMTP server, as the connection may be insecure.</strong>'),
];
$ssl_verify_peer = $config
->get('smtp_ssl_verify_peer');
$form['advanced']['ssl_settings']['smtp_ssl_verify_peer'] = [
'#type' => 'checkbox',
'#title' => t('Verfy peer'),
'#default_value' => isset($ssl_verify_peer) ? $ssl_verify_peer : 1,
'#description' => t('If this is checked, it will require verification of the SSL certificate being used on the mail server.'),
];
$ssl_verify_peer_name = $config
->get('smtp_ssl_verify_peer_name');
$form['advanced']['ssl_settings']['smtp_ssl_verify_peer_name'] = [
'#type' => 'checkbox',
'#title' => t('Verfy peer name'),
'#default_value' => isset($ssl_verify_peer_name) ? $ssl_verify_peer_name : 1,
'#description' => t("If this is checked, it will require verification of the mail server's name in the SSL certificate."),
];
$ssl_allow_self_signed = $config
->get('smtp_ssl_allow_self_signed');
$form['advanced']['ssl_settings']['smtp_ssl_allow_self_signed'] = [
'#type' => 'checkbox',
'#title' => t('Allow self signed'),
'#default_value' => isset($ssl_allow_self_signed) ? $ssl_allow_self_signed : 0,
'#description' => t('If this is checked, it will allow conecting to a mail server with a self-signed SSL certificate. (This requires "Verfy peer" to be enabled.)'),
'#states' => [
'enabled' => [
':input[name="ssl_verify_peer"]' => [
'checked' => TRUE,
],
],
],
];
$form['test'] = [
'#type' => 'details',
'#title' => t('Test configuration'),
];
$form['test']['phpmailer_test'] = [
'#type' => 'textfield',
'#title' => t('Recipient'),
'#default_value' => '',
'#description' => t('Type in an address to have a test e-mail sent there.'),
];
return parent::buildForm($form, $form_state);
}