further comment fixes
[civicrm-core.git] / CRM / Admin / Form / Setting / Smtp.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33
34 /**
35 * This class generates form components for Smtp Server.
36 */
37 class CRM_Admin_Form_Setting_Smtp extends CRM_Admin_Form_Setting {
38 protected $_testButtonName;
39
40 /**
41 * Build the form object.
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'),
50 CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB => ts('Redirect to Database'),
51 );
52 $this->addRadio('outBound_option', ts('Select Mailer'), $outBoundOption);
53
54 CRM_Utils_System::setTitle(ts('Settings - Outbound Mail'));
55 $this->add('text', 'sendmail_path', ts('Sendmail Path'));
56 $this->add('text', 'sendmail_args', ts('Sendmail Argument'));
57 $this->add('text', 'smtpServer', ts('SMTP Server'));
58 $this->add('text', 'smtpPort', ts('SMTP Port'));
59 $this->addYesNo('smtpAuth', ts('Authentication?'));
60 $this->addElement('text', 'smtpUsername', ts('SMTP Username'));
61 $this->addElement('password', 'smtpPassword', ts('SMTP Password'));
62
63 $this->_testButtonName = $this->getButtonName('refresh', 'test');
64
65 $this->addFormRule(array('CRM_Admin_Form_Setting_Smtp', 'formRule'));
66 parent::buildQuickForm();
67 $buttons = $this->getElement('buttons')->getElements();
68 $buttons[] = $this->createElement('submit', $this->_testButtonName, ts('Save & Send Test Email'), array('crm-icon' => 'mail-closed'));
69 $this->getElement('buttons')->setElements($buttons);
70 }
71
72 /**
73 * Process the form submission.
74 */
75 public function postProcess() {
76 // flush caches so we reload details for future requests
77 // CRM-11967
78 CRM_Utils_System::flushCache();
79
80 $formValues = $this->controller->exportValues($this->_name);
81
82 $buttonName = $this->controller->getButtonName();
83 // check if test button
84 if ($buttonName == $this->_testButtonName) {
85 if ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) {
86 CRM_Core_Session::setStatus(ts('You have selected "Disable Outbound Email". A test email can not be sent.'), ts("Email Disabled"), "error");
87 }
88 elseif ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB) {
89 CRM_Core_Session::setStatus(ts('You have selected "Redirect to Database". A test email can not be sent.'), ts("Email Disabled"), "error");
90 }
91 else {
92 $session = CRM_Core_Session::singleton();
93 $userID = $session->get('userID');
94 list($toDisplayName, $toEmail, $toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($userID);
95
96 //get the default domain email address.CRM-4250
97 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
98
99 if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
100 $fixUrl = CRM_Utils_System::url("civicrm/admin/domain", 'action=update&reset=1');
101 CRM_Core_Error::fatal(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM &raquo; Communications &raquo; FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', array(1 => $fixUrl)));
102 }
103
104 if (!$toEmail) {
105 CRM_Core_Error::statusBounce(ts('Cannot send a test email because your user record does not have a valid email address.'));
106 }
107
108 if (!trim($toDisplayName)) {
109 $toDisplayName = $toEmail;
110 }
111
112 $to = '"' . $toDisplayName . '"' . "<$toEmail>";
113 $from = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
114 $testMailStatusMsg = ts('Sending test email. FROM: %1 TO: %2.<br />', array(
115 1 => $domainEmailAddress,
116 2 => $toEmail,
117 ));
118
119 $params = array();
120 if ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) {
121 $subject = "Test for SMTP settings";
122 $message = "SMTP settings are correct.";
123
124 $params['host'] = $formValues['smtpServer'];
125 $params['port'] = $formValues['smtpPort'];
126
127 if ($formValues['smtpAuth']) {
128 $params['username'] = $formValues['smtpUsername'];
129 $params['password'] = $formValues['smtpPassword'];
130 $params['auth'] = TRUE;
131 }
132 else {
133 $params['auth'] = FALSE;
134 }
135
136 // set the localhost value, CRM-3153, CRM-9332
137 $params['localhost'] = $_SERVER['SERVER_NAME'];
138
139 // also set the timeout value, lets set it to 30 seconds
140 // CRM-7510, CRM-9332
141 $params['timeout'] = 30;
142
143 $mailerName = 'smtp';
144 }
145 elseif ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) {
146 $subject = "Test for Sendmail settings";
147 $message = "Sendmail settings are correct.";
148 $params['sendmail_path'] = $formValues['sendmail_path'];
149 $params['sendmail_args'] = $formValues['sendmail_args'];
150 $mailerName = 'sendmail';
151 }
152 elseif ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) {
153 $subject = "Test for PHP mail settings";
154 $message = "mail settings are correct.";
155 $mailerName = 'mail';
156 }
157
158 $headers = array(
159 'From' => $from,
160 'To' => $to,
161 'Subject' => $subject,
162 );
163
164 $mailer = Mail::factory($mailerName, $params);
165
166 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
167 $result = $mailer->send($toEmail, $headers, $message);
168 unset($errorScope);
169 if (!is_a($result, 'PEAR_Error')) {
170 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");
171 }
172 else {
173 $message = CRM_Utils_Mail::errorMessage($mailer, $result);
174 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");
175 }
176 }
177 }
178
179 $mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
180 'mailing_backend'
181 );
182
183 if (!empty($mailingBackend)) {
184 CRM_Core_BAO_ConfigSetting::formatParams($formValues, $mailingBackend);
185 }
186
187 // if password is present, encrypt it
188 if (!empty($formValues['smtpPassword'])) {
189 $formValues['smtpPassword'] = CRM_Utils_Crypt::encrypt($formValues['smtpPassword']);
190 }
191
192 CRM_Core_BAO_Setting::setItem($formValues,
193 CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
194 'mailing_backend'
195 );
196 }
197
198 /**
199 * Global validation rules for the form.
200 *
201 * @param array $fields
202 * Posted values of the form.
203 *
204 * @return array
205 * list of errors to be posted back to the form
206 */
207 public static function formRule($fields) {
208 if ($fields['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) {
209 if (empty($fields['smtpServer'])) {
210 $errors['smtpServer'] = 'SMTP Server name is a required field.';
211 }
212 if (empty($fields['smtpPort'])) {
213 $errors['smtpPort'] = 'SMTP Port is a required field.';
214 }
215 if (!empty($fields['smtpAuth'])) {
216 if (empty($fields['smtpUsername'])) {
217 $errors['smtpUsername'] = 'If your SMTP server requires authentication please provide a valid user name.';
218 }
219 if (empty($fields['smtpPassword'])) {
220 $errors['smtpPassword'] = 'If your SMTP server requires authentication, please provide a password.';
221 }
222 }
223 }
224 if ($fields['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) {
225 if (!$fields['sendmail_path']) {
226 $errors['sendmail_path'] = 'Sendmail Path is a required field.';
227 }
228 if (!$fields['sendmail_args']) {
229 $errors['sendmail_args'] = 'Sendmail Argument is a required field.';
230 }
231 }
232
233 return empty($errors) ? TRUE : $errors;
234 }
235
236 /**
237 * Set default values for the form.
238 */
239 public function setDefaultValues() {
240 if (!$this->_defaults) {
241 $this->_defaults = array();
242
243 $mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
244 'mailing_backend'
245 );
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 }
266 return $this->_defaults;
267 }
268
269 }