3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * Build the form object
48 public function buildQuickForm() {
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'),
57 $this->addRadio('outBound_option', ts('Select Mailer'), $outBoundOption);
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'));
68 $this->_testButtonName
= $this->getButtonName('refresh', 'test');
70 $this->add('submit', $this->_testButtonName
, ts('Save & Send Test Email'));
72 $this->addFormRule(array('CRM_Admin_Form_Setting_Smtp', 'formRule'));
73 parent
::buildQuickForm();
77 * Process the form submission
82 public function postProcess() {
83 // flush caches so we reload details for future requests
85 CRM_Utils_System
::flushCache();
87 $formValues = $this->controller
->exportValues($this->_name
);
89 $buttonName = $this->controller
->getButtonName();
90 // check if test button
91 if ($buttonName == $this->_testButtonName
) {
92 if ($formValues['outBound_option'] == CRM_Mailing_Config
::OUTBOUND_OPTION_DISABLED
) {
93 CRM_Core_Session
::setStatus(ts('You have selected "Disable Outbound Email". A test email can not be sent.'), ts("Email Disabled"), "error");
94 } elseif ( $formValues['outBound_option'] == CRM_Mailing_Config
::OUTBOUND_OPTION_REDIRECT_TO_DB
) {
95 CRM_Core_Session
::setStatus(ts('You have selected "Redirect to Database". A test email can not be sent.'), ts("Email Disabled"), "error");
98 $session = CRM_Core_Session
::singleton();
99 $userID = $session->get('userID');
100 list($toDisplayName, $toEmail, $toDoNotEmail) = CRM_Contact_BAO_Contact
::getContactDetails($userID);
102 //get the default domain email address.CRM-4250
103 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
105 if (!$domainEmailAddress ||
$domainEmailAddress == 'info@EXAMPLE.ORG') {
106 $fixUrl = CRM_Utils_System
::url("civicrm/admin/domain", 'action=update&reset=1');
107 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)));
111 CRM_Core_Error
::statusBounce(ts('Cannot send a test email because your user record does not have a valid email address.'));
114 if (!trim($toDisplayName)) {
115 $toDisplayName = $toEmail;
118 $to = '"' . $toDisplayName . '"' . "<$toEmail>";
119 $from = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
120 $testMailStatusMsg = ts('Sending test email. FROM: %1 TO: %2.<br />', array(1 => $domainEmailAddress, 2 => $toEmail));
123 if ($formValues['outBound_option'] == CRM_Mailing_Config
::OUTBOUND_OPTION_SMTP
) {
124 $subject = "Test for SMTP settings";
125 $message = "SMTP settings are correct.";
127 $params['host'] = $formValues['smtpServer'];
128 $params['port'] = $formValues['smtpPort'];
130 if ($formValues['smtpAuth']) {
131 $params['username'] = $formValues['smtpUsername'];
132 $params['password'] = $formValues['smtpPassword'];
133 $params['auth'] = TRUE;
136 $params['auth'] = FALSE;
139 // set the localhost value, CRM-3153, CRM-9332
140 $params['localhost'] = $_SERVER['SERVER_NAME'];
142 // also set the timeout value, lets set it to 30 seconds
143 // CRM-7510, CRM-9332
144 $params['timeout'] = 30;
146 $mailerName = 'smtp';
148 elseif ($formValues['outBound_option'] == CRM_Mailing_Config
::OUTBOUND_OPTION_SENDMAIL
) {
149 $subject = "Test for Sendmail settings";
150 $message = "Sendmail settings are correct.";
151 $params['sendmail_path'] = $formValues['sendmail_path'];
152 $params['sendmail_args'] = $formValues['sendmail_args'];
153 $mailerName = 'sendmail';
155 elseif ($formValues['outBound_option'] == CRM_Mailing_Config
::OUTBOUND_OPTION_MAIL
) {
156 $subject = "Test for PHP mail settings";
157 $message = "mail settings are correct.";
158 $mailerName = 'mail';
164 'Subject' => $subject,
167 $mailer = Mail
::factory($mailerName, $params);
169 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
170 $result = $mailer->send($toEmail, $headers, $message);
172 if (!is_a($result, 'PEAR_Error')) {
173 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 $message = CRM_Utils_Mail
::errorMessage($mailer, $result);
177 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");
182 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
186 if (!empty($mailingBackend)) {
187 CRM_Core_BAO_ConfigSetting
::formatParams($formValues, $mailingBackend);
190 // if password is present, encrypt it
191 if (!empty($formValues['smtpPassword'])) {
192 $formValues['smtpPassword'] = CRM_Utils_Crypt
::encrypt($formValues['smtpPassword']);
195 CRM_Core_BAO_Setting
::setItem($formValues,
196 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
202 * Global validation rules for the form
204 * @param array $fields posted values of the form
206 * @return array list of errors to be posted back to the form
209 public static function formRule($fields) {
210 if ($fields['outBound_option'] == CRM_Mailing_Config
::OUTBOUND_OPTION_SMTP
) {
211 if (empty($fields['smtpServer'])) {
212 $errors['smtpServer'] = 'SMTP Server name is a required field.';
214 if (empty($fields['smtpPort'])) {
215 $errors['smtpPort'] = 'SMTP Port is a required field.';
217 if (!empty($fields['smtpAuth'])) {
218 if (empty($fields['smtpUsername'])) {
219 $errors['smtpUsername'] = 'If your SMTP server requires authentication please provide a valid user name.';
221 if (empty($fields['smtpPassword'])) {
222 $errors['smtpPassword'] = 'If your SMTP server requires authentication, please provide a password.';
226 if ($fields['outBound_option'] == CRM_Mailing_Config
::OUTBOUND_OPTION_SENDMAIL
) {
227 if (!$fields['sendmail_path']) {
228 $errors['sendmail_path'] = 'Sendmail Path is a required field.';
230 if (!$fields['sendmail_args']) {
231 $errors['sendmail_args'] = 'Sendmail Argument is a required field.';
235 return empty($errors) ?
TRUE : $errors;
239 * Set default values for the form.
240 * default values are retrieved from the database
245 public function setDefaultValues() {
246 if (!$this->_defaults
) {
247 $this->_defaults
= array();
249 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
252 if (!empty($mailingBackend)) {
253 $this->_defaults
= $mailingBackend;
255 if (!empty($this->_defaults
['smtpPassword'])) {
256 $this->_defaults
['smtpPassword'] = CRM_Utils_Crypt
::decrypt($this->_defaults
['smtpPassword']);
260 if (!isset($this->_defaults
['smtpServer'])) {
261 $this->_defaults
['smtpServer'] = 'localhost';
262 $this->_defaults
['smtpPort'] = 25;
263 $this->_defaults
['smtpAuth'] = 0;
266 if (!isset($this->_defaults
['sendmail_path'])) {
267 $this->_defaults
['sendmail_path'] = '/usr/sbin/sendmail';
268 $this->_defaults
['sendmail_args'] = '-i';
272 return $this->_defaults
;