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