My external form is sending the email twice
Permalink 1 user found helpful
Hi Guys, I don't have the best knowledge in php but i have managed to put this external form together that is a simple contact form and sends an e-mail containing the details.
However it works too well. It sends the e-mail twice!
I have spent a week trawling forums and prying my eyes over the code but i just cannot workout why.
I know it is calling the SendMail function twice for some reason but cannot work out why. PLEASE HELP!!
CONTROLLER FILE:
AND THE FORM FILE
However it works too well. It sends the e-mail twice!
I have spent a week trawling forums and prying my eyes over the code but i just cannot workout why.
I know it is calling the SendMail function twice for some reason but cannot work out why. PLEASE HELP!!
CONTROLLER FILE:
<?php namespace Concrete\Block\ExternalForm\Form\Controller; use Loader; use Concrete\Core\Validation\CSRF\Token; class customcontact extends \Concrete\Core\Controller\AbstractController { /** * Sends the email. * * @return boolean */ public function action_send() { $errors = array(); // Load helpers
Viewing 15 lines of 68 lines. View entire code block.
AND THE FORM FILE
<?php defined('C5_EXECUTE') or die("Access Denied."); use Concrete\Core\Validation\CSRF\Token; ?> <?php if(isset($response) && $response): ?> <div class="alert alert-info">Thanks, your email was successfully sent.</div> <?php endif; ?> <?php if(isset($errors) && !empty($errors)): ?> <div class="alert alert-error"> <p>Sorry, there was a problem with your submission and your message could not be sent.</p> <ul> <?php foreach($errors as $field => $error): ?> <li><?php echo $error; ?></li> <?php endforeach; ?> </ul>
Viewing 15 lines of 38 lines. View entire code block.
I was building an external form of my own when I ran into this same issue, and now I realize what's causing it. I believe the action_send() gets called twice: first for the form post and then as the view method, because the URL includes it after the form is submitted.
To prevent duplicate emails, you need to set a redirect after the $mail->sendMail(), like this:
And then add another action method:
Another solution might be to set a session variable and check that.
To prevent duplicate emails, you need to set a redirect after the $mail->sendMail(), like this:
$mail->sendMail(); $this->redirect( Page::getCurrentPage()->getCollectionPath(), 'thank_you' );
And then add another action method:
public function action_thank_you { // Uncomment this if you need to prepare the view // $this->view(); $this->set( 'response', true ); }
Another solution might be to set a session variable and check that.
nvm
I'm getting double emails too—but only on my production (Linux) environment. On Windows XAMPP, it's fine.
The form action function is indeed being called twice (on Linux). I don't understand why it should be called a second time as the 'view method'. Can anyone explain?
The form action function is indeed being called twice (on Linux). I don't understand why it should be called a second time as the 'view method'. Can anyone explain?
I kludged around this issue using a global variable in controller.php. It's very ugly. The fact that the problem occurs in some environments and not others suggests that c5, php or apache/litespeed are implicated. The versions of c5 and php are virtually identical between my two servers, but one uses apache while the other uses litespeed. It's the latter that stutters.
Otherwise everything looks ok. I would probably start debugging this by inserting some echo commands into the action_send() method just to make sure it doesn't run twice. Also, var_dump() is your friend when performing quick and dirty object autopsies:
With the above you can verify the $mail object doesn't contain any unintended cc fields or something.