last day (15 days later) » 

7:56 PM
0
Q: PHP - Get return value from mail() function when using token to detect multiple form submissions

ZippyBackground I have a website with a simple contact form which uses the POST method to send data to the server where it is sanitised and validated. The user will then be re-directed back to the form page with some URL parameters, and a message shown ('success' if the email was sent, or an error if...

 
you need to submit your form with ajax
 
Hi @Sysix, thanks but as I stated above, I am submitting the form with AJAX... when the user has Javascript enabled :-) This is a fallback specifically for when Javascript is disabled.
 
alright :) it was not clear for me. Then response with a status code and check in js for it. Doont forget to update the (CSRF)-Token on the Frontend side too :)
 
@Sysix, the token gets updated automatically when the page is reloaded. What do you mean by 'respond with a status code'? I can't check anything in JS... again JS is disabled in this situation, besides I don't really want to rely on the client, this should be a server-side solution :-)
 
Okay, you cant avoid redirect when not submiting with ajax. BUT When you submit with ajax the current token is located in the html, validate the token and generate a new one. The Problem: Your form has still the old, now invalid, token. And you try to submit again with the old token
PS: you can avoid, sorry. but you need to refill the values in the form again. (the $_POST one)
 
7:56 PM
I really don't understand what you mean. I don't want to avoid re-directing. That's the whole problem. I can't obtain a response from the mail() function in order to know how to redirect (ie, which URL parameters to add). I know the form has the old/invalid token, it's supposed to haveā€”that's what allows me to detect multiple button presses. Also I am already repopulating the form when re-directing back if validation fails. There's no problem with that, it's not related to the issue in this question.
Hi
 
hey
im sorry I didnt understood your question at the beginning, It was too long to read it ;')
 
I just want to re-iterate what the problem is. When clicking the button once, the email is sent and the mail() function returns true or false (success or failure), so I can determine which URL parameters to add when re-directing back to the form page.
But....
 
mail() is always returning true?
 
If the 'submit' button is clicked 2 or more times, the email is sent, but the mail function never returns anything as the 2nd button click is caught and the 'else' condition (as shown in the question) is entered.
No,, mail returns true if emails sends, false if something goes wrong, or nothing at all if the button is clicked twice (although the email does send).
 
wierd... only at the 2nd one
which php version and how you send it? with postfix or smtp server?
 
8:02 PM
Yes, I mean it kind of makes sense because the 2nd button click is detected as it is supposed to be but what I don't understand is how I can get to the value returned by the mail function which is definitively called (from the 1st button click)
 
jeah the problem is the mail function.
 
PHP is 7.4.1
I'm using sendmail on localhost
No problem with the mail being sent.
 
why you want to track with the second click the first mail send?
 
I think it's something to do with how PHP handles a script running multiple times (different processes)
Because I don't want someone clicking the the submit button 100 times while the email sends and spamming me with 100s of emails
 
okay so token should be invalid and no second mail should be send
i dont really know where exactly your problem is there
 
8:07 PM
Yes 2nd mail is not send so that part works OK
 
you want to redirect only with a good error message?
 
A good message if the email sends, an error if the email fails to send
Which I can do if the submit button is only clicked once
But not if it is clicked multiple times
 
your problem is that your token check fails before
you need to redirect their too with a message
 
Nope
 
because of the token?
 
8:12 PM
What do you mean? The token check fails because the submit button was clicked twice. It's supposed to fails
 
jeah and that why the mail failed to send (from the guest side=
 
The mail doesn't fail to send
I said before the mail sends perfectly fine
But when the submit button is clicked multiple times, I can't get a response from the mail() function
even though the mail does send
Because the 2nd click is detected and the else condition is executed
(else condition #1 in the question's code example)
Just to clarify...
When buttons is pressed once, emails sends and I redirect back and success or failure. No problem
 
at the time of the second click is already the first mail sent?
 
When button is clicked 2 or more times, email is sent only once (which it is supposed to), but I can't redirect because I can't get to the response from the call to mail()
 
because mail is not called? the if before is failing already
 
8:20 PM
At the time of the 2nd click, the email function has already been called... the email is in the process of being sent (probably waiting for a server response)
What?
Mail is called. How else is the email being sent??
Here is what happens...
User clicks 'submit'
This calls mail() because the tokens match
mail is sent
As the mail is being sent, the user clicks the button again
This time the token comparison fails (as it is supposed to) and mail is not called again
This is what is supposed to happen
So no 2nd mail is sent
However, I need to get at the response from the first call to mail() (well, the only call to mail() really).
The mail does send
But I'm left with the code in the 'else' condition whereas I want to get into one of the if/else's that are responses to the mail() call
So I know how to re-direct the user back to the form page
Does that clarify things :-)
 

last day (15 days later) »