/** * Sends an email based on the posted params. * * @throws Exception */ public function actionSendMessage() { $this->requirePostRequest(); $settings = craft()->plugins->getPlugin('contactform')->getSettings(); $message = new ContactFormModel(); $savedBody = false; $message->fromEmail = craft()->request->getPost('fromEmail'); $message->fromName = craft()->request->getPost('fromName'); $message->subject = craft()->request->getPost('subject'); if ($settings->allowAttachments) { if (isset($_FILES['attachment']) && isset($_FILES['attachment']['name'])) { if (is_array($_FILES['attachment']['name'])) { $message->attachment = \CUploadedFile::getInstancesByName('attachment'); } else { $message->attachment = array(\CUploadedFile::getInstanceByName('attachment')); } } } // Set the message body $postedMessage = craft()->request->getPost('message'); if ($postedMessage) { if (is_array($postedMessage)) { $savedBody = false; if (isset($postedMessage['body'])) { // Save the message body in case we need to reassign it in the event there's a validation error $savedBody = $postedMessage['body']; } // If it's false, then there was no messages[body] input submitted. If it's '', then validation needs to fail. if ($savedBody === false || $savedBody !== '') { // Compile the message from each of the individual values $compiledMessage = ''; foreach ($postedMessage as $key => $value) { if ($key != 'body') { if ($compiledMessage) { $compiledMessage .= "\n\n"; } $compiledMessage .= $key . ': '; if (is_array($value)) { $compiledMessage .= implode(', ', $value); } else { $compiledMessage .= $value; } } } if (!empty($postedMessage['body'])) { if ($compiledMessage) { $compiledMessage .= "\n\n"; } $compiledMessage .= $postedMessage['body']; } $message->message = $compiledMessage; } } else { $message->message = $postedMessage; } } if ($message->validate()) { // Only actually send it if the honeypot test was valid if (!$this->validateHoneypot($settings->honeypotField) || craft()->contactForm->sendMessage($message)) { if (craft()->request->isAjaxRequest()) { $this->returnJson(array('success' => true)); } else { // Deprecated. Use 'redirect' instead. $successRedirectUrl = craft()->request->getPost('successRedirectUrl'); if ($successRedirectUrl) { $_POST['redirect'] = $successRedirectUrl; } craft()->userSession->setNotice('Your message has been sent.'); $this->redirectToPostedUrl($message); } } } // Something has gone horribly wrong. if (craft()->request->isAjaxRequest()) { return $this->returnErrorJson($message->getErrors()); } else { craft()->userSession->setError('There was a problem with your submission, please check the form and try again!'); if ($savedBody !== false) { $message->message = $savedBody; } craft()->urlManager->setRouteVariables(array('message' => $message)); } }
/** * Sends an email based on the posted params. * * @throws Exception */ public function actionSendMessage() { $this->requirePostRequest(); $settings = craft()->plugins->getPlugin('contactform')->getSettings(); // Check whether the form passes the 'honeypot' test. If not, we pretend everything worked just fine, // so the evil spammer is none the wiser. if (!$this->validateHoneypot($settings->honeypotField)) { $this->returnSuccess(); } $message = new ContactFormModel(); $savedBody = false; $message->fromEmail = craft()->request->getPost('fromEmail'); $message->fromName = craft()->request->getPost('fromName'); $message->subject = craft()->request->getPost('subject'); $message->attachment = \CUploadedFile::getInstanceByName('attachment'); // Set the message body $postedMessage = craft()->request->getPost('message'); if ($postedMessage) { if (is_array($postedMessage)) { $savedBody = false; if (isset($postedMessage['body'])) { // Save the message body in case we need to reassign it in the event there's a validation error $savedBody = $postedMessage['body']; } // If it's false, then there was no messages[body] input submitted. If it's '', then validation needs to fail. if ($savedBody === false || $savedBody !== '') { // Compile the message from each of the individual values $compiledMessage = ''; foreach ($postedMessage as $key => $value) { if ($key != 'body') { if ($compiledMessage) { $compiledMessage .= "\n\n"; } $compiledMessage .= $key . ': '; if (is_array($value)) { $compiledMessage .= implode(', ', $value); } else { $compiledMessage .= $value; } } } if (!empty($postedMessage['body'])) { if ($compiledMessage) { $compiledMessage .= "\n\n"; } $compiledMessage .= $postedMessage['body']; } $message->message = $compiledMessage; } } else { $message->message = $postedMessage; } } if ($message->validate()) { // Send if (craft()->contactForm->sendMessage($message)) { $this->returnSuccess(); } } // Something has gone horribly wrong. if (craft()->request->isAjaxRequest()) { return $this->returnErrorJson($message->getErrors()); } else { craft()->userSession->setError('There was a problem with your submission, please check the form and try again!'); if ($savedBody !== false) { $message->message = $savedBody; } craft()->urlManager->setRouteVariables(array('message' => $message)); } }