Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
ce064e4f | 35 | * This class generates form components for Smtp Server. |
6a488035 TO |
36 | */ |
37 | class CRM_Admin_Form_Setting_Smtp extends CRM_Admin_Form_Setting { | |
38 | protected $_testButtonName; | |
39 | ||
40 | /** | |
eceb18cc | 41 | * Build the form object. |
6a488035 TO |
42 | */ |
43 | public function buildQuickForm() { | |
44 | ||
45 | $outBoundOption = array( | |
46 | CRM_Mailing_Config::OUTBOUND_OPTION_MAIL => ts('mail()'), | |
47 | CRM_Mailing_Config::OUTBOUND_OPTION_SMTP => ts('SMTP'), | |
48 | CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL => ts('Sendmail'), | |
49 | CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED => ts('Disable Outbound Email'), | |
2a7b2166 | 50 | CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB => ts('Redirect to Database'), |
6a488035 TO |
51 | ); |
52 | $this->addRadio('outBound_option', ts('Select Mailer'), $outBoundOption); | |
53 | ||
54 | CRM_Utils_System::setTitle(ts('Settings - Outbound Mail')); | |
beac1417 | 55 | $this->add('checkbox', 'allow_mail_from_logged_in_contact', ts('Allow Mail to be sent from logged in contact\'s email address')); |
6a488035 TO |
56 | $this->add('text', 'sendmail_path', ts('Sendmail Path')); |
57 | $this->add('text', 'sendmail_args', ts('Sendmail Argument')); | |
58 | $this->add('text', 'smtpServer', ts('SMTP Server')); | |
59 | $this->add('text', 'smtpPort', ts('SMTP Port')); | |
60 | $this->addYesNo('smtpAuth', ts('Authentication?')); | |
61 | $this->addElement('text', 'smtpUsername', ts('SMTP Username')); | |
62 | $this->addElement('password', 'smtpPassword', ts('SMTP Password')); | |
63 | ||
64 | $this->_testButtonName = $this->getButtonName('refresh', 'test'); | |
65 | ||
6a488035 TO |
66 | $this->addFormRule(array('CRM_Admin_Form_Setting_Smtp', 'formRule')); |
67 | parent::buildQuickForm(); | |
aeb97cc1 | 68 | $buttons = $this->getElement('buttons')->getElements(); |
2fb51250 | 69 | $buttons[] = $this->createElement('submit', $this->_testButtonName, ts('Save & Send Test Email'), array('crm-icon' => 'fa-envelope-o')); |
aeb97cc1 | 70 | $this->getElement('buttons')->setElements($buttons); |
6a488035 TO |
71 | } |
72 | ||
73 | /** | |
eceb18cc | 74 | * Process the form submission. |
6a488035 TO |
75 | */ |
76 | public function postProcess() { | |
77 | // flush caches so we reload details for future requests | |
78 | // CRM-11967 | |
79 | CRM_Utils_System::flushCache(); | |
80 | ||
81 | $formValues = $this->controller->exportValues($this->_name); | |
82 | ||
beac1417 MW |
83 | Civi::settings()->set('allow_mail_from_logged_in_contact', (!empty($formValues['allow_mail_from_logged_in_contact']))); |
84 | unset($formValues['allow_mail_from_logged_in_contact']); | |
85 | ||
6a488035 TO |
86 | $buttonName = $this->controller->getButtonName(); |
87 | // check if test button | |
88 | if ($buttonName == $this->_testButtonName) { | |
89 | if ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) { | |
90 | CRM_Core_Session::setStatus(ts('You have selected "Disable Outbound Email". A test email can not be sent.'), ts("Email Disabled"), "error"); | |
0db6c3e1 | 91 | } |
481a74f4 | 92 | elseif ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB) { |
6a488035 TO |
93 | CRM_Core_Session::setStatus(ts('You have selected "Redirect to Database". A test email can not be sent.'), ts("Email Disabled"), "error"); |
94 | } | |
95 | else { | |
96 | $session = CRM_Core_Session::singleton(); | |
97 | $userID = $session->get('userID'); | |
98 | list($toDisplayName, $toEmail, $toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($userID); | |
99 | ||
100 | //get the default domain email address.CRM-4250 | |
101 | list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
102 | ||
103 | if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') { | |
104 | $fixUrl = CRM_Utils_System::url("civicrm/admin/domain", 'action=update&reset=1'); | |
105 | CRM_Core_Error::fatal(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM » Communications » FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', array(1 => $fixUrl))); | |
106 | } | |
107 | ||
108 | if (!$toEmail) { | |
109 | CRM_Core_Error::statusBounce(ts('Cannot send a test email because your user record does not have a valid email address.')); | |
110 | } | |
111 | ||
112 | if (!trim($toDisplayName)) { | |
113 | $toDisplayName = $toEmail; | |
114 | } | |
115 | ||
353ffa53 TO |
116 | $to = '"' . $toDisplayName . '"' . "<$toEmail>"; |
117 | $from = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>'; | |
118 | $testMailStatusMsg = ts('Sending test email. FROM: %1 TO: %2.<br />', array( | |
119 | 1 => $domainEmailAddress, | |
389bcebf | 120 | 2 => $toEmail, |
353ffa53 | 121 | )); |
6a488035 TO |
122 | |
123 | $params = array(); | |
124 | if ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { | |
125 | $subject = "Test for SMTP settings"; | |
126 | $message = "SMTP settings are correct."; | |
127 | ||
128 | $params['host'] = $formValues['smtpServer']; | |
129 | $params['port'] = $formValues['smtpPort']; | |
130 | ||
131 | if ($formValues['smtpAuth']) { | |
132 | $params['username'] = $formValues['smtpUsername']; | |
133 | $params['password'] = $formValues['smtpPassword']; | |
353ffa53 | 134 | $params['auth'] = TRUE; |
6a488035 TO |
135 | } |
136 | else { | |
137 | $params['auth'] = FALSE; | |
138 | } | |
139 | ||
140 | // set the localhost value, CRM-3153, CRM-9332 | |
141 | $params['localhost'] = $_SERVER['SERVER_NAME']; | |
142 | ||
143 | // also set the timeout value, lets set it to 30 seconds | |
144 | // CRM-7510, CRM-9332 | |
145 | $params['timeout'] = 30; | |
146 | ||
147 | $mailerName = 'smtp'; | |
148 | } | |
149 | elseif ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { | |
150 | $subject = "Test for Sendmail settings"; | |
151 | $message = "Sendmail settings are correct."; | |
152 | $params['sendmail_path'] = $formValues['sendmail_path']; | |
153 | $params['sendmail_args'] = $formValues['sendmail_args']; | |
154 | $mailerName = 'sendmail'; | |
155 | } | |
156 | elseif ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { | |
353ffa53 TO |
157 | $subject = "Test for PHP mail settings"; |
158 | $message = "mail settings are correct."; | |
6a488035 TO |
159 | $mailerName = 'mail'; |
160 | } | |
161 | ||
162 | $headers = array( | |
163 | 'From' => $from, | |
164 | 'To' => $to, | |
165 | 'Subject' => $subject, | |
166 | ); | |
167 | ||
168 | $mailer = Mail::factory($mailerName, $params); | |
169 | ||
6a4257d4 | 170 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 171 | $result = $mailer->send($toEmail, $headers, $message); |
6a4257d4 | 172 | unset($errorScope); |
df151738 MW |
173 | if (defined('CIVICRM_MAIL_LOG')) { |
174 | CRM_Core_Session::setStatus($testMailStatusMsg . ts('You have defined CIVICRM_MAIL_LOG - no mail will be sent. Your %1 settings have not been tested.', array(1 => strtoupper($mailerName))), ts("Mail not sent"), "warning"); | |
175 | } | |
176 | elseif (!is_a($result, 'PEAR_Error')) { | |
6a488035 TO |
177 | CRM_Core_Session::setStatus($testMailStatusMsg . ts('Your %1 settings are correct. A test email has been sent to your email address.', array(1 => strtoupper($mailerName))), ts("Mail Sent"), "success"); |
178 | } | |
179 | else { | |
180 | $message = CRM_Utils_Mail::errorMessage($mailer, $result); | |
181 | CRM_Core_Session::setStatus($testMailStatusMsg . ts('Oops. Your %1 settings are incorrect. No test mail has been sent.', array(1 => strtoupper($mailerName))) . $message, ts("Mail Not Sent"), "error"); | |
182 | } | |
183 | } | |
184 | } | |
185 | ||
aaffa79f | 186 | $mailingBackend = Civi::settings()->get('mailing_backend'); |
6a488035 TO |
187 | |
188 | if (!empty($mailingBackend)) { | |
6e5b5e59 | 189 | $formValues = array_merge($mailingBackend, $formValues); |
6a488035 TO |
190 | } |
191 | ||
192 | // if password is present, encrypt it | |
193 | if (!empty($formValues['smtpPassword'])) { | |
194 | $formValues['smtpPassword'] = CRM_Utils_Crypt::encrypt($formValues['smtpPassword']); | |
195 | } | |
196 | ||
08ef4ddd | 197 | Civi::settings()->set('mailing_backend', $formValues); |
6a488035 TO |
198 | } |
199 | ||
200 | /** | |
eceb18cc | 201 | * Global validation rules for the form. |
6a488035 | 202 | * |
5173bd95 TO |
203 | * @param array $fields |
204 | * Posted values of the form. | |
6a488035 | 205 | * |
a6c01b45 CW |
206 | * @return array |
207 | * list of errors to be posted back to the form | |
6a488035 | 208 | */ |
00be9182 | 209 | public static function formRule($fields) { |
6a488035 | 210 | if ($fields['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { |
a7488080 | 211 | if (empty($fields['smtpServer'])) { |
6a488035 TO |
212 | $errors['smtpServer'] = 'SMTP Server name is a required field.'; |
213 | } | |
a7488080 | 214 | if (empty($fields['smtpPort'])) { |
6a488035 TO |
215 | $errors['smtpPort'] = 'SMTP Port is a required field.'; |
216 | } | |
a7488080 CW |
217 | if (!empty($fields['smtpAuth'])) { |
218 | if (empty($fields['smtpUsername'])) { | |
6a488035 TO |
219 | $errors['smtpUsername'] = 'If your SMTP server requires authentication please provide a valid user name.'; |
220 | } | |
a7488080 | 221 | if (empty($fields['smtpPassword'])) { |
6a488035 TO |
222 | $errors['smtpPassword'] = 'If your SMTP server requires authentication, please provide a password.'; |
223 | } | |
224 | } | |
225 | } | |
226 | if ($fields['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { | |
227 | if (!$fields['sendmail_path']) { | |
228 | $errors['sendmail_path'] = 'Sendmail Path is a required field.'; | |
229 | } | |
230 | if (!$fields['sendmail_args']) { | |
231 | $errors['sendmail_args'] = 'Sendmail Argument is a required field.'; | |
232 | } | |
233 | } | |
234 | ||
235 | return empty($errors) ? TRUE : $errors; | |
236 | } | |
237 | ||
238 | /** | |
c490a46a | 239 | * Set default values for the form. |
6a488035 | 240 | */ |
00be9182 | 241 | public function setDefaultValues() { |
6a488035 TO |
242 | if (!$this->_defaults) { |
243 | $this->_defaults = array(); | |
244 | ||
aaffa79f | 245 | $mailingBackend = Civi::settings()->get('mailing_backend'); |
6a488035 TO |
246 | if (!empty($mailingBackend)) { |
247 | $this->_defaults = $mailingBackend; | |
248 | ||
249 | if (!empty($this->_defaults['smtpPassword'])) { | |
250 | $this->_defaults['smtpPassword'] = CRM_Utils_Crypt::decrypt($this->_defaults['smtpPassword']); | |
251 | } | |
252 | } | |
253 | else { | |
254 | if (!isset($this->_defaults['smtpServer'])) { | |
255 | $this->_defaults['smtpServer'] = 'localhost'; | |
256 | $this->_defaults['smtpPort'] = 25; | |
257 | $this->_defaults['smtpAuth'] = 0; | |
258 | } | |
259 | ||
260 | if (!isset($this->_defaults['sendmail_path'])) { | |
261 | $this->_defaults['sendmail_path'] = '/usr/sbin/sendmail'; | |
262 | $this->_defaults['sendmail_args'] = '-i'; | |
263 | } | |
264 | } | |
265 | } | |
beac1417 | 266 | $this->_defaults['allow_mail_from_logged_in_contact'] = Civi::settings()->get('allow_mail_from_logged_in_contact'); |
6a488035 TO |
267 | return $this->_defaults; |
268 | } | |
96025800 | 269 | |
6a488035 | 270 | } |