3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for Smtp Server
40 class CRM_Admin_Form_Setting_Smtp
extends CRM_Admin_Form_Setting
{
41 protected $_testButtonName;
44 * Function to build the form
49 public function buildQuickForm() {
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_Mailing_Config
::OUTBOUND_OPTION_REDIRECT_TO_DB
=> ts('Redirect to Database'),
58 $this->addRadio('outBound_option', ts('Select Mailer'), $outBoundOption);
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'));
69 $this->_testButtonName
= $this->getButtonName('refresh', 'test');
71 $this->add('submit', $this->_testButtonName
, ts('Save & Send Test Email'));
73 $this->addFormRule(array('CRM_Admin_Form_Setting_Smtp', 'formRule'));
74 parent
::buildQuickForm();
78 * Function to process the form
84 public function postProcess() {
85 // flush caches so we reload details for future requests
87 CRM_Utils_System
::flushCache();
89 $formValues = $this->controller
->exportValues($this->_name
);
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");
100 $session = CRM_Core_Session
::singleton();
101 $userID = $session->get('userID');
102 list($toDisplayName, $toEmail, $toDoNotEmail) = CRM_Contact_BAO_Contact
::getContactDetails($userID);
104 //get the default domain email address.CRM-4250
105 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
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 » 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)));
113 CRM_Core_Error
::statusBounce(ts('Cannot send a test email because your user record does not have a valid email address.'));
116 if (!trim($toDisplayName)) {
117 $toDisplayName = $toEmail;
120 $to = '"' . $toDisplayName . '"' . "<$toEmail>";
121 $from = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
122 $testMailStatusMsg = ts('Sending test email. FROM: %1 TO: %2.<br />', array(1 => $domainEmailAddress, 2 => $toEmail));
125 if ($formValues['outBound_option'] == CRM_Mailing_Config
::OUTBOUND_OPTION_SMTP
) {
126 $subject = "Test for SMTP settings";
127 $message = "SMTP settings are correct.";
129 $params['host'] = $formValues['smtpServer'];
130 $params['port'] = $formValues['smtpPort'];
132 if ($formValues['smtpAuth']) {
133 $params['username'] = $formValues['smtpUsername'];
134 $params['password'] = $formValues['smtpPassword'];
135 $params['auth'] = TRUE;
138 $params['auth'] = FALSE;
141 // set the localhost value, CRM-3153, CRM-9332
142 $params['localhost'] = $_SERVER['SERVER_NAME'];
144 // also set the timeout value, lets set it to 30 seconds
145 // CRM-7510, CRM-9332
146 $params['timeout'] = 30;
148 $mailerName = 'smtp';
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';
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';
166 'Subject' => $subject,
169 $mailer = Mail
::factory($mailerName, $params);
171 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
172 $result = $mailer->send($toEmail, $headers, $message);
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");
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");
184 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
188 if (!empty($mailingBackend)) {
189 CRM_Core_BAO_ConfigSetting
::formatParams($formValues, $mailingBackend);
192 // if password is present, encrypt it
193 if (!empty($formValues['smtpPassword'])) {
194 $formValues['smtpPassword'] = CRM_Utils_Crypt
::encrypt($formValues['smtpPassword']);
197 CRM_Core_BAO_Setting
::setItem($formValues,
198 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
204 * global validation rules for the form
206 * @param array $fields posted values of the form
208 * @return array list of errors to be posted back to the form
212 static function formRule($fields) {
213 if ($fields['outBound_option'] == CRM_Mailing_Config
::OUTBOUND_OPTION_SMTP
) {
214 if (empty($fields['smtpServer'])) {
215 $errors['smtpServer'] = 'SMTP Server name is a required field.';
217 if (empty($fields['smtpPort'])) {
218 $errors['smtpPort'] = 'SMTP Port is a required field.';
220 if (!empty($fields['smtpAuth'])) {
221 if (empty($fields['smtpUsername'])) {
222 $errors['smtpUsername'] = 'If your SMTP server requires authentication please provide a valid user name.';
224 if (empty($fields['smtpPassword'])) {
225 $errors['smtpPassword'] = 'If your SMTP server requires authentication, please provide a password.';
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.';
233 if (!$fields['sendmail_args']) {
234 $errors['sendmail_args'] = 'Sendmail Argument is a required field.';
238 return empty($errors) ?
TRUE : $errors;
242 * This function sets the default values for the form.
243 * default values are retrieved from the database
249 function setDefaultValues() {
250 if (!$this->_defaults
) {
251 $this->_defaults
= array();
253 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
256 if (!empty($mailingBackend)) {
257 $this->_defaults
= $mailingBackend;
259 if (!empty($this->_defaults
['smtpPassword'])) {
260 $this->_defaults
['smtpPassword'] = CRM_Utils_Crypt
::decrypt($this->_defaults
['smtpPassword']);
264 if (!isset($this->_defaults
['smtpServer'])) {
265 $this->_defaults
['smtpServer'] = 'localhost';
266 $this->_defaults
['smtpPort'] = 25;
267 $this->_defaults
['smtpAuth'] = 0;
270 if (!isset($this->_defaults
['sendmail_path'])) {
271 $this->_defaults
['sendmail_path'] = '/usr/sbin/sendmail';
272 $this->_defaults
['sendmail_args'] = '-i';
276 return $this->_defaults
;