commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Admin / Form / Setting / Smtp.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * $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 * Build the form object.
45 *
46 * @return void
47 */
48 public function buildQuickForm() {
49
50 $outBoundOption = array(
51 CRM_Mailing_Config::OUTBOUND_OPTION_MAIL => ts('mail()'),
52 CRM_Mailing_Config::OUTBOUND_OPTION_SMTP => ts('SMTP'),
53 CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL => ts('Sendmail'),
54 CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED => ts('Disable Outbound Email'),
55 CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB => ts('Redirect to Database'),
56 );
57 $this->addRadio('outBound_option', ts('Select Mailer'), $outBoundOption);
58
59 CRM_Utils_System::setTitle(ts('Settings - Outbound Mail'));
60 $this->add('text', 'sendmail_path', ts('Sendmail Path'));
61 $this->add('text', 'sendmail_args', ts('Sendmail Argument'));
62 $this->add('text', 'smtpServer', ts('SMTP Server'));
63 $this->add('text', 'smtpPort', ts('SMTP Port'));
64 $this->addYesNo('smtpAuth', ts('Authentication?'));
65 $this->addElement('text', 'smtpUsername', ts('SMTP Username'));
66 $this->addElement('password', 'smtpPassword', ts('SMTP Password'));
67
68 $this->_testButtonName = $this->getButtonName('refresh', 'test');
69
70 $this->addFormRule(array('CRM_Admin_Form_Setting_Smtp', 'formRule'));
71 parent::buildQuickForm();
72 $buttons = $this->getElement('buttons')->getElements();
73 $buttons[] = $this->createElement('submit', $this->_testButtonName, ts('Save & Send Test Email'), array('crm-icon' => 'mail-closed'));
74 $this->getElement('buttons')->setElements($buttons);
75 }
76
77 /**
78 * Process the form submission.
79 *
80 *
81 * @return void
82 */
83 public function postProcess() {
84 // flush caches so we reload details for future requests
85 // CRM-11967
86 CRM_Utils_System::flushCache();
87
88 $formValues = $this->controller->exportValues($this->_name);
89
90 $buttonName = $this->controller->getButtonName();
91 // check if test button
92 if ($buttonName == $this->_testButtonName) {
93 if ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) {
94 CRM_Core_Session::setStatus(ts('You have selected "Disable Outbound Email". A test email can not be sent.'), ts("Email Disabled"), "error");
95 }
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(
123 1 => $domainEmailAddress,
124 2 => $toEmail,
125 ));
126
127 $params = array();
128 if ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) {
129 $subject = "Test for SMTP settings";
130 $message = "SMTP settings are correct.";
131
132 $params['host'] = $formValues['smtpServer'];
133 $params['port'] = $formValues['smtpPort'];
134
135 if ($formValues['smtpAuth']) {
136 $params['username'] = $formValues['smtpUsername'];
137 $params['password'] = $formValues['smtpPassword'];
138 $params['auth'] = TRUE;
139 }
140 else {
141 $params['auth'] = FALSE;
142 }
143
144 // set the localhost value, CRM-3153, CRM-9332
145 $params['localhost'] = $_SERVER['SERVER_NAME'];
146
147 // also set the timeout value, lets set it to 30 seconds
148 // CRM-7510, CRM-9332
149 $params['timeout'] = 30;
150
151 $mailerName = 'smtp';
152 }
153 elseif ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) {
154 $subject = "Test for Sendmail settings";
155 $message = "Sendmail settings are correct.";
156 $params['sendmail_path'] = $formValues['sendmail_path'];
157 $params['sendmail_args'] = $formValues['sendmail_args'];
158 $mailerName = 'sendmail';
159 }
160 elseif ($formValues['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) {
161 $subject = "Test for PHP mail settings";
162 $message = "mail settings are correct.";
163 $mailerName = 'mail';
164 }
165
166 $headers = array(
167 'From' => $from,
168 'To' => $to,
169 'Subject' => $subject,
170 );
171
172 $mailer = Mail::factory($mailerName, $params);
173
174 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
175 $result = $mailer->send($toEmail, $headers, $message);
176 unset($errorScope);
177 if (!is_a($result, 'PEAR_Error')) {
178 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");
179 }
180 else {
181 $message = CRM_Utils_Mail::errorMessage($mailer, $result);
182 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");
183 }
184 }
185 }
186
187 $mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
188 'mailing_backend'
189 );
190
191 if (!empty($mailingBackend)) {
192 CRM_Core_BAO_ConfigSetting::formatParams($formValues, $mailingBackend);
193 }
194
195 // if password is present, encrypt it
196 if (!empty($formValues['smtpPassword'])) {
197 $formValues['smtpPassword'] = CRM_Utils_Crypt::encrypt($formValues['smtpPassword']);
198 }
199
200 CRM_Core_BAO_Setting::setItem($formValues,
201 CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
202 'mailing_backend'
203 );
204 }
205
206 /**
207 * Global validation rules for the form.
208 *
209 * @param array $fields
210 * Posted values of the form.
211 *
212 * @return array
213 * list of errors to be posted back to the form
214 */
215 public static function formRule($fields) {
216 if ($fields['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) {
217 if (empty($fields['smtpServer'])) {
218 $errors['smtpServer'] = 'SMTP Server name is a required field.';
219 }
220 if (empty($fields['smtpPort'])) {
221 $errors['smtpPort'] = 'SMTP Port is a required field.';
222 }
223 if (!empty($fields['smtpAuth'])) {
224 if (empty($fields['smtpUsername'])) {
225 $errors['smtpUsername'] = 'If your SMTP server requires authentication please provide a valid user name.';
226 }
227 if (empty($fields['smtpPassword'])) {
228 $errors['smtpPassword'] = 'If your SMTP server requires authentication, please provide a password.';
229 }
230 }
231 }
232 if ($fields['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) {
233 if (!$fields['sendmail_path']) {
234 $errors['sendmail_path'] = 'Sendmail Path is a required field.';
235 }
236 if (!$fields['sendmail_args']) {
237 $errors['sendmail_args'] = 'Sendmail Argument is a required field.';
238 }
239 }
240
241 return empty($errors) ? TRUE : $errors;
242 }
243
244 /**
245 * Set default values for the form.
246 * default values are retrieved from the database
247 *
248 *
249 * @return void
250 */
251 public function setDefaultValues() {
252 if (!$this->_defaults) {
253 $this->_defaults = array();
254
255 $mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
256 'mailing_backend'
257 );
258 if (!empty($mailingBackend)) {
259 $this->_defaults = $mailingBackend;
260
261 if (!empty($this->_defaults['smtpPassword'])) {
262 $this->_defaults['smtpPassword'] = CRM_Utils_Crypt::decrypt($this->_defaults['smtpPassword']);
263 }
264 }
265 else {
266 if (!isset($this->_defaults['smtpServer'])) {
267 $this->_defaults['smtpServer'] = 'localhost';
268 $this->_defaults['smtpPort'] = 25;
269 $this->_defaults['smtpAuth'] = 0;
270 }
271
272 if (!isset($this->_defaults['sendmail_path'])) {
273 $this->_defaults['sendmail_path'] = '/usr/sbin/sendmail';
274 $this->_defaults['sendmail_args'] = '-i';
275 }
276 }
277 }
278 return $this->_defaults;
279 }
280
281 }