0

I have been digging around to look for suggestions on how to ensure an email address that has been inputted in a web form is valid or not. It seems like the best solution is to send an automated email to the email address the user has submitted, if they receive it then great if not its obviously not valid.

what i want to do is have the user fill in the contact form, when they submit the form it checks all validation and then sends an automated email to the users email address, then depending on whether the email was successfully received send the original query to my email.

I can get the automated email sending ok but is there a way to get php to return a email received confirmation so that i can process the rest of my script?

here is my code

<?php
// if user has submitted the form and there are no errors

if(($_SERVER["REQUEST_METHOD"] == "POST") && !$nameErr && !$emailErr && !$phoneErr && !$messageErr && !$botErr && !$pointsErr)
{
    //Create a new PHPMailer instance
    $mail = new PHPMailer;
    //Tell PHPMailer to use SMTP
    $mail->isSMTP();
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 0;
    //Set the hostname of the mail server
    $mail->Host = 'mailout.one.com';
    //Set the SMTP port number - likely to be 25, 465 or 587
    $mail->Port = 587;
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //Username to use for SMTP authentication
    $mail->Username = 'USERNAME';
    //Password to use for SMTP authentication
    $mail->Password = 'PASSWORD';
    //Set who the message is to be sent from
    $mail->setFrom($from, $name);
    //Set an alternative reply-to address
    $mail->addReplyTo($email, $name);
    //Set who the message is to be sent to
    $mail->addAddress($from, 'PERSON');


    // To send automated reply mail
    $autoemail = new PHPMailer(); 
    $autoemail->From = $from; 
    $autoemail->FromName = "PERSON"; 
    $autoemail->AddAddress($email, $name); 
    $autoemail->Subject = "Autorepsonse: We received your submission"; 
    $autoemail->Body = "We received your submission. We will contact you soon ...";
    $autoemail->Send();



    if(!$autoemail->send()) 
    {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } 
    else 
    {
        //CHECK AUTOMATED EMAIL HAS BEEN RECEIVED BY THE USER THEN SEND THEIR ENQUIRY EMAIL
        //Set the subject line
    #$mail->Subject = $subject;
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    #$mail->Body = $message."<p><strong>Club Enquiry Relates To: </strong>".$club."</p><p><strong>Client Details</strong></p>Name: ".$name."<br/>Email: ".$email."<br />Tel No: ".$phone."<p><strong>Extras</strong></p>How Did you find our website? ".$hearsay."<br/ >Spam Score: ".$points." out of ".$maxPoints."<br />IP Address: ".$_SERVER['REMOTE_ADDR'];
    //Replace the plain text body with one created manually
    #$mail->AltBody = 'This is a plain-text message body';
    //Attach an image file
    #$mail->addAttachment('images/phpmailer_mini.png');

        echo"<h2>Thank You!!!</h2>";
        echo "<p>Your message has been sent successfully, we aim to respond within a couple of days. Please check your Junk/Span folder incase it ends up there.</p>";
    }   

But I'm unsure how to tell if the auto response email was delivered or not?

any help is greatly appreciated

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
BBLJ84
  • 175
  • 1
  • 12
  • 1
    *"is there a way to get php to return a email received confirmation so that i can process the rest of my script?"* - Only if there's a link for them to click on and that will do some type of verification at *your* end, be it write to a file, a database. – Funk Forty Niner Dec 24 '18 at 20:04
  • *"But I'm unsure how to tell if the auto response email was delivered or not?"* - `if(condition){...}` which you appear to be doing now. – Funk Forty Niner Dec 24 '18 at 20:05
  • thanks @FunkFortyNiner i thought that might be the case, but was hoping there was a quicker simpler process :( – BBLJ84 Dec 24 '18 at 20:12
  • 1
    Welcome. Well, it's "kind of" simple. Like I said, you could get them to click on the link, sent as a METHOD with parameters, and then if they click on it, it would then be retrieved through the same method and checking if it is set/not empty. Either to a file, and/or a database. It wouldn't take that much time to setup either method. There's nothing that "quick & dirty", I'm afraid. – Funk Forty Niner Dec 24 '18 at 20:14
  • 1
    You can have a look at [How to check if an email address is real or valid using PHP](https://stackoverflow.com/q/19261987/1415724) also. It *might* or might not work for you though. It's pretty hard nowadays to check for emails that are real though, given what systems/servers supports (or not). – Funk Forty Niner Dec 24 '18 at 20:20
  • ok cool, i will look into that a bit more, my only worry is people might not want to click on a link or it might get lost in the junk folder and therefor never get activated, but only time will see if that's an issue or not :) thanks for the link :) – BBLJ84 Dec 24 '18 at 20:21
  • If your server has proper [SPF records](https://en.wikipedia.org/wiki/Sender_Policy_Framework) and sent through SMTP, it shouldn't be a problem and shouldn't be marked as spam/sent to junkmail. However, we can't control a person's action and can't force their hand, as it were. You can only instruct them in the email that if they wish to continue or any other reason..., that they click the link to be approved and to also show them that they can "trust" you with a message of disclosure & how their data is used. If they feel it sounds legit they'll click it. Good luck :) & happy (safe) holidays. – Funk Forty Niner Dec 24 '18 at 20:26
  • @FunkFortyNiner thank you very much for your help! all the best and well wishes over the holidays :) – BBLJ84 Dec 24 '18 at 20:47

1 Answers1

5

An automatic and instant delivery confirmation is not possible, so if you're thinking about waiting for that confirmation for your script to perform the second part of the signup process, you need to rethink your strategy.

Also, keep in mind that a valid email doesn't necessarily mean that the email belongs to the person signing up. I could in theory sign up to your site using an email of someone I know and be OK just because the email exists.

If you need email validation, you should consider sending an automated email with a unique disposable link which the user clicks to complete the process or containing a unique code which the user must enter in a confirmation step.

My suggestion would be to include a link in the email pointing to your site (i.e., yoursite.com/verification/random_unique_string)

The random_unique_string may be anything as long as it's not reversible by a malicious user. For inatance, it could be a hash of the recipient email, date sent, random salt, etc which is unique and when clicked, leads to the finalization of the sign up process

Javier Larroulet
  • 3,047
  • 3
  • 13
  • 30
  • Which is basically what I told them to do in comments, *but* more. This stands to go much deeper and is too broad for this area of Stack, IMHO. – Funk Forty Niner Dec 24 '18 at 20:31
  • You're right... I didn't notice the comments until I read yours now. I agree this is a subject that could easily become much deeper than stack's regular topic, as it has a plethora of considerations that need to be addressed if one wants to do it right. Happy holidays! – Javier Larroulet Dec 25 '18 at 01:06