3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class handles mail account settings.
22 class CRM_Admin_Form_MailSettings
extends CRM_Admin_Form
{
24 protected $_testButtonName;
29 public $submitOnce = TRUE;
32 * Build the form object.
34 public function buildQuickForm() {
35 parent
::buildQuickForm();
36 $this->setPageTitle(ts('Mail Account'));
38 if ($this->_action
& CRM_Core_Action
::DELETE
) {
42 $this->_testButtonName
= $this->getButtonName('upload', 'test');
43 $buttons = $this->getElement('buttons')->getElements();
44 $buttons[] = $this->createElement(
46 $this->_testButtonName
,
47 CRM_Core_Page
::crmIcon('fa-chain') . ' ' . ts('Save & Test'),
48 ['type' => 'submit', 'class' => 'crm-button']
50 $this->getElement('buttons')->setElements($buttons);
52 $this->applyFilter('__ALL__', 'trim');
55 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_MailSettings');
58 $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
60 $this->add('text', 'domain', ts('Email Domain'), $attributes['domain'], TRUE);
61 $this->addRule('domain', ts('Email domain must use a valid internet domain format (e.g. \'example.org\').'), 'domain');
63 $this->add('text', 'localpart', ts('Localpart'), $attributes['localpart']);
65 $this->add('text', 'return_path', ts('Return-Path'), $attributes['return_path']);
66 $this->addRule('return_path', ts('Return-Path must use a valid email address format.'), 'email');
68 $this->add('select', 'protocol',
70 ['' => ts('- select -')] + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_MailSettings', 'protocol'),
74 $this->add('text', 'server', ts('Server'), $attributes['server']);
76 $this->add('text', 'username', ts('Username'), ['autocomplete' => 'off']);
78 $this->add('password', 'password', ts('Password'), ['autocomplete' => 'off']);
80 $this->add('text', 'source', ts('Source'), $attributes['source']);
82 $this->add('checkbox', 'is_ssl', ts('Use SSL?'));
85 1 => ts('Bounce Processing'),
86 0 => ts('Email-to-Activity Processing'),
88 $this->add('select', 'is_default', ts('Used For?'), $usedfor);
89 $this->addField('activity_status', ['placeholder' => FALSE]);
91 $this->add('checkbox', 'is_non_case_email_skipped', ts('Skip emails which do not have a Case ID or Case hash'));
92 $this->add('checkbox', 'is_contact_creation_disabled_if_no_match', ts('Do not create new contacts when filing emails'));
96 * Add local and global form rules.
98 public function addRules() {
99 $this->addFormRule(['CRM_Admin_Form_MailSettings', 'formRule'], $this);
102 public function getDefaultEntity() {
103 return 'MailSettings';
107 * Add local and global form rules.
109 public function setDefaultValues() {
110 $defaults = parent
::setDefaultValues();
112 // Set activity status to "Completed" by default.
113 if ($this->_action
!= CRM_Core_Action
::DELETE
&&
114 (!$this->_id ||
!CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_MailSettings', $this->_id
, 'activity_status'))
116 $defaults['activity_status'] = 'Completed';
123 * Global validation rules for the form.
125 * @param array $fields
126 * Posted values of the form.
127 * @param array $files
129 * @param CRM_Core_Form $form
133 * list of errors to be posted back to the form
135 public static function formRule($fields, $files, $form) {
137 if ($form->_action
!= CRM_Core_Action
::DELETE
) {
138 // Check for default from email address and organization (domain) name. Force them to change it.
139 if ($fields['domain'] == 'EXAMPLE.ORG') {
140 $errors['domain'] = ts('Please enter a valid domain for this mailbox account (the part after @).');
144 return empty($errors) ?
TRUE : $errors;
148 * Process the form submission.
150 public function postProcess() {
151 if ($this->_action
& CRM_Core_Action
::DELETE
) {
152 CRM_Core_BAO_MailSettings
::deleteMailSettings($this->_id
);
153 CRM_Core_Session
::setStatus("", ts('Mail Setting Deleted.'), "success");
157 //get the submitted form values.
158 $formValues = $this->controller
->exportValues($this->_name
);
175 'is_non_case_email_skipped',
176 'is_contact_creation_disabled_if_no_match',
180 foreach ($fields as $f) {
184 'is_non_case_email_skipped',
185 'is_contact_creation_disabled_if_no_match',
187 $params[$f] = CRM_Utils_Array
::value($f, $formValues, FALSE);
190 $params[$f] = $formValues[$f] ??
NULL;
194 $params['domain_id'] = CRM_Core_Config
::domainID();
196 // assign id only in update mode
197 $status = ts('Your New Email Settings have been saved.');
198 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
199 $params['id'] = $this->_id
;
200 $status = ts('Your Email Settings have been updated.');
203 $mailSettings = CRM_Core_BAO_MailSettings
::create($params);
205 if ($mailSettings->id
) {
206 CRM_Core_Session
::setStatus($status, ts("Saved"), "success");
209 CRM_Core_Session
::setStatus("", ts('Changes Not Saved.'), "info");
212 if ($this->controller
->getButtonName() == $this->_testButtonName
) {
213 $test = civicrm_api4('MailSettings', 'testConnection', [
214 'where' => [['id', '=', $mailSettings->id
]],
216 CRM_Core_Session
::setStatus($test['details'], $test['title'],
217 $test['error'] ?
'error' : 'success');