Skip to content

Commit

Permalink
fixed crashing of email sending in Joomla 3.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMacej committed Apr 7, 2016
1 parent 83a2c06 commit 8474ee8
Showing 1 changed file with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
* after it.
*
* Created on October, 2013
* Modified on April, 2016
*
* @package plg_system_contactformenhancer
* @author Peter Macej
* @copyright Copyright (c) 2013 Peter Macej. All rights reserved.
* @copyright Copyright (c) 2016 Peter Macej. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

Expand All @@ -24,6 +25,8 @@
defined('_JEXEC') or die;

/**
* Controller for single contact view.
*
* @package Joomla.Site
* @subpackage com_contact
*/
Expand All @@ -41,11 +44,18 @@ public function getModel($name = '', $prefix = '', $config = array('ignore_reque
* used in contactformenhancer plugin.
*/


/**
* Method to submit the contact form and send an email.
*
* @return boolean True on success sending the email. False on failure.
*
* @since 1.5.19
*/
public function submit()
{
// Check for request forgeries.
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

$app = JFactory::getApplication();
$model = $this->getModel('contact');
$params = JComponentHelper::getParams('com_contact');
Expand Down Expand Up @@ -80,6 +90,7 @@ public function submit()

// Validate the posted data.
$form = $model->getForm();

if (!$form)
{
JError::raiseError(500, $model->getError());
Expand All @@ -90,6 +101,7 @@ public function submit()

if ($validate === false)
{

// Get the validation messages.
$errors = $model->getErrors();
// Push up to three validation messages out to the user.
Expand Down Expand Up @@ -129,7 +141,7 @@ public function submit()
$sent = false;
if (!$params->get('custom_reply'))
{
$sent = $this->_sendEmail($data, $contact);
$sent = $this->_sendEmail($data, $contact, $params->get('show_email_copy'));
}

// Set the success message if it was a success
Expand Down Expand Up @@ -168,10 +180,19 @@ public function submit()
}



private function _sendEmail($data, $contact)
/**
*
* @param array $data The data to send in the email.
* @param stdClass $contact The user information to send the email to
* @param boolean $copy_email_activated True to send a copy of the email to the user.
*
* @return boolean True on success sending the email, false on failure.
*
* @since 1.6.4
*/
private function _sendEmail($data, $contact, $copy_email_activated)
{
// get contactformenhancer plugin parameters
// get contactformenhancer plugin parameters
$plugin = JPluginHelper::getPlugin('system', 'contactformenhancer');
$pluginParams = new JRegistry();
$pluginParams->loadString($plugin->params);
Expand All @@ -182,12 +203,12 @@ private function _sendEmail($data, $contact)
$contact_user = JUser::getInstance($contact->user_id);
$contact->email_to = $contact_user->get('email');
}
$mailfrom = $app->getCfg('mailfrom');
$fromname = $app->getCfg('fromname');
$sitename = $app->getCfg('sitename');
$mailfrom = $app->get('mailfrom');
$fromname = $app->get('fromname');
$sitename = $app->get('sitename');

$name = $data['contact_name'];
$email = JstringPunycode::emailToPunycode($data['contact_email']);
$email = JStringPunycode::emailToPunycode($data['contact_email']);
$subject = $data['contact_subject'];
$body = $data['contact_message'];

Expand All @@ -203,7 +224,7 @@ private function _sendEmail($data, $contact)
$body = $prefix."\n".$name.' <'.$email.'>'."\r\n\r\n".stripslashes($body);

$mail->addRecipient($contact->email_to);
$mail->addReplyTo(array($email, $name));
$mail->addReplyTo($email, $name);
$mail->setSender(array($mailfrom, $fromname));
$mail->setSubject($sitename.': '.$subject);
$mail->setBody($body);
Expand All @@ -217,7 +238,9 @@ private function _sendEmail($data, $contact)
$cReplyName = $this->processVariables($cReplyName, $name, $email, $subject, $body);
$cReplyEmail = $pluginParams->get("customReplytoEmail", "");
$cReplyEmail = $this->processVariables($cReplyEmail, $name, $email, $subject, $body);
$mail->addReplyTo(array($cReplyEmail, $cReplyName));
if (trim($cReplyEmail) !== '') {
$mail->addReplyTo($cReplyEmail, $cReplyName);
}

$cFromName = $pluginParams->get("customFromName", "%FORM_NAME%");
$cFromName = $this->processVariables($cFromName, $name, $email, $subject, $body);
Expand All @@ -237,9 +260,8 @@ private function _sendEmail($data, $contact)

//If we are supposed to copy the sender, do so.

// check whether email copy function activated
if ( array_key_exists('contact_email_copy', $data) )
{
// Check whether email copy function activated
if ($copy_email_activated == true && !empty($data['contact_email_copy'])) {
$copytext = JText::sprintf('COM_CONTACT_COPYTEXT_OF', $contact->name, $sitename);
$copytext .= "\r\n\r\n".$body;
$copysubject = JText::sprintf('COM_CONTACT_COPYSUBJECT_OF', $subject);
Expand All @@ -248,14 +270,14 @@ private function _sendEmail($data, $contact)
if ($pluginParams->get('sendCustomCopyEmail', '0') == '0') {
// default original format
$mail->addRecipient($email);
$mail->addReplyTo(array($email, $name));
$mail->addReplyTo($email, $name);
$mail->setSender(array($mailfrom, $fromname));
$mail->setSubject($copysubject);
$mail->setBody($copytext);
} else {
// custom format
$mail->addRecipient($email);
$mail->addReplyTo(array($email, $name));
$mail->addReplyTo($email, $name);
$mail->setSender(array($mailfrom, $fromname));

$cSubject = $pluginParams->get("customCopySubject", "Copy of: %FORM_SUBJECT%");
Expand All @@ -275,9 +297,9 @@ private function _sendEmail($data, $contact)

private function processVariables($text, $name, $email, $subject, $body) {
$app = JFactory::getApplication();
$mailfrom = $app->getCfg('mailfrom');
$fromname = $app->getCfg('fromname');
$sitename = $app->getCfg('sitename');
$mailfrom = $app->get('mailfrom');
$fromname = $app->get('fromname');
$sitename = $app->get('sitename');
$siteurl = JUri::base();

$text = str_replace("%FORM_SUBJECT%", $subject, $text);
Expand Down

0 comments on commit 8474ee8

Please sign in to comment.