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