Problem:
The goal is: my website’s user completes a set of fields (not a form), then hits “send e-mail” (some fields are verified for completeness), then the user’s e-mail platform is activated through “mailTo”, the e-mail is pre-populated and all the user has to do is send it. Here is the problem: Is there any way to detect that the user actually sent the e-mail? Right now, after the e-mail is supposedly sent, a little div pops up asking the user if the e-mail was sent, which is fine by me, but, I think, not solid enough. I do not want to use any external, type of “mail sender” app, or .ajax, due to spam issues, want to keep the communication as simple as possible, and, knowing that there is no complete way to verify an e-mail, prefer to leave it up to the user. This is why I also require a phone number to verify their booking (it is a hotel’s reservation website). Here is my code:
$("#btnDone").click(function(){
//variables, for verification purposes (check input of "input type: hidden" fields - not in a form):
var ctxx = "", ce = $("#yce").val().length, ct = $("#yct").val().length, cnl = $("#cnc").val().length;
//verification functions return error = 1, if error:
if(ce > 0){isEmail();} //contains basic regex
else{isPhone();} //one or the other is required
if(error==0){iscnp();} if(error==0){isyn();} if(error==0){if(ct > 0){iscnt();}}
//if verifications OK:
if(error==0){
//variables to be included in e-mail:
ynx = $("#yn").val(), cex = $("#yce").val(), ctx = $("#yct").val(), cnx = $("#cnt").val(),
npx = $("#cnp").val(), ncx = $("#cnc").val();
//structuring, before populating e-mail variables:
if(cnl > 0){npx = npx + ", " + ncx}
ctxx = "Phone: " + ctx + " " + "(" + cnx + ")"; temp = "E-mail: " + cex;
if(ce==0){temp = ctxx;}else{if(ct > 0){temp = temp + " - " + " " + ctxx;}}
//e-mail variables:
var esubject = encodeURIComponent("Subject: Reservation at (hotel name)");
var ebody = encodeURIComponent("Number of persons: " + npx
+ "\n\n" + iv + " - " + ov + "\n" + stay + " " + noni + " total" + "\n"
+ "Reservation Name: " + ynx + "\n" + temp + "\n\n"
+ "Thank you, we will contact you shortly to confirm your booking..." + "\n"
+ "Please add any notes and special requests below:" + "\n\n\n\n\n");
//send e-mail:
window.location.href = "mailto:myemail@mail.com?subject=" + esubject + "&body=" + ebody;
//asking the user if the e-mail was sent:
$("#text1").text("Did you send your reservation e-mail?"); $("#text2").text("");
}
});
The issue is with that last line. Instead of asking the user if the e-mail was sent, can jquery detect if it was? In other words, if the user hit “send” on his mail platform? Or, if that e-mail message actually reached my server? If not (probably the case), how can the above be done with PHP?
Solution:
No, you can’t detect this client-side (directly), with jQuery or anything else.
You can detect this server-side, by putting a unique identifier in the email (for instance, in the subject) and having your client-side code call your server-side code to check whether the email has been received by your mail server. (You may have to poll [or use a web socket or server-sent events, etc.], it’ll usually take at least a second or two, sometimes longer.) How you do it is almost entirely dependent on the details of your server environment (and would be too broad a question).
Alternatively, you can send the information to your server directly from your script instead of involving the user’s email system, as ADyson pointed out. That would be a fairly standard way to do it. If you need to validate their email address, send them an email with a code in it they then quote back (via clicking a link or replying to the email).