1

I am trying to build an inquiry form and send the data to my email using PHPMailer. I am receiving the data which is submitted through the form but I am not able to send a confirmation to the customer who filled the form. So far this is my form:

<form class="myForm" action="" id="iForm" method="POST">
        <input type="text"  id="Name" placeholder="Name" required> 
        <input type="email" id="Email" placeholder="Email" required>
        <input type="tel"   id="Phone" placeholder="Phone Number" required>
        <input type="text"  id="Date" placeholder="Schedule a call" required>
        <textarea id="Message" rows="5" placeholder="Your Message"  required></textarea>
        <div class="form-group">
             <button type="submit" class="btn cbtn">SUBMIT</button>
        </div>
</form>

Passing the data from the submitted form

$("#iForm").on('submit', function(e) {
e.preventDefault();
var data = {
    name: $("#Name").val(),
    email: $("#Email").val(),
    phone: $("#Phone").val(),
    date: $("#Date").val(),
    message: $("#Message").val()
};

if ( isValidEmail(data['email']) && (data['name'].length > 1) && (data['date'].length > 1) && (data['message'].length > 1) && isValidPhoneNumber(data['phone']) ) {
    $.ajax({
        type: "POST",
        url: "php/appointment.php",
        data: data,
        success: function() {
            $('.success.df').delay(500).fadeIn(1000);
            $('.failed.df').fadeOut(500);
        }
    });
} else {
    $('.failed.df').delay(500).fadeIn(1000);
    $('.success.df').fadeOut(500);
}

return false;
});

Checking for valid email address

function isValidEmail(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

Checking for a valid phone number

function isValidPhoneNumber(phoneNumber) {
    return phoneNumber.match(/[0-9-()+]{3,20}/);
}

This is the code which I am using from PHPMailer:

$_name      = $_REQUEST['name'];
$_email     = $_REQUEST['email'];
$_phone     = $_REQUEST['phone'];
$_date      = $_REQUEST['date'];
$_message   = $_REQUEST['message'];

$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->CharSet    = 'UTF-8';
$mail->SMTPDebug  = 0;
$mail->SMTPAuth   = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port       = 587;
$mail->Username   = "office@myhost.co.uk";
$mail->Password   = "********";
$mail->Host       = "myhost.co.uk";

$mail->setFrom('office@myhost.co.uk', 'Alex');
$mail->addAddress('me@gmail.com', 'Alex');

$mail->isHTML(true);
$mail->Subject     = 'New inquiry from CC';
$mail->Body        = <<<EOD
     <strong>Name:</strong> $_name <br>
     <strong>Email:</strong> <a href="mailto:$_email?subject=feedback" "email me">$_email</a> <br> <br>
     <strong>Phone:</strong> $_phone <br>
     <strong>Booking Date:</strong> $_date <br>
     <strong>Message:</strong> $_message <br>

I've tried using this, making another instance of PHPMailer and Send the email to the customer with the email they provided.

if($mail->Send()) {
$autoRespond = new PHPMailer();
$autoRespond->setFrom('office@myhost.co.uk', 'Alex');
$autoRespond->AddAddress($_email); 
$autoRespond->Subject = "Autorepsonse: We received your submission"; 
$autoRespond->Body = "We received your submission. We will contact you";

$autoRespond->Send(); 
}

I've tried a few online "solutions" to this with no success. Any ideas ?

h3k
  • 178
  • 3
  • 13
  • Did you check what issue I have ? I do not have issue with the attributes, I am receiving the email with the data submitted perfectly. I am not able to send auto respond to the customer who submitted the form with the second part I've shown. – h3k Feb 25 '19 at 23:56
  • If you showed us the actual form you have written, then there is no way that data from those fields is being sent from the browser to the PHP script unless the `` fields have a `name="name"` etc attribute. Becase the `$_email` field will not be set to anything – RiggsFolly Feb 26 '19 at 00:00
  • Change this `` to this `` Then tell me if it works !!! – RiggsFolly Feb 26 '19 at 00:06
  • Of course I can get the information from the form and send it to my PHP, I am using javascript for that purpose. My first email is using all of the parameters and I am receiving it on my email, so why the heck you mark my thread as duplicate when obviously you have no idea what my issue is ? – h3k Feb 26 '19 at 00:09
  • Well **you dont show us any javascript so how are we supposed to know** If you show us **ALL** the code that you are actually using **MAYBE** we can help you – RiggsFolly Feb 26 '19 at 00:13
  • The other option would be to close this because you dont show a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) – RiggsFolly Feb 26 '19 at 00:14
  • to improve your experience on SO please read [how to ask](https://stackoverflow.com/help/how-to-ask) an [On Topic question](https://stackoverflow.com/help/on-topic), and the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and [RETAKE the tour](http://stackoverflow.com/tour) – RiggsFolly Feb 26 '19 at 00:15
  • Ok, so now we are still missing the code for `isValidEmail()` and `isValidPhoneNumber()` – RiggsFolly Feb 26 '19 at 00:20
  • Now where is `$("#Message")` – RiggsFolly Feb 26 '19 at 00:24
  • Why bother with your own email validator when PHPMailer provides one? You will be able to see what is going on with your second submission if you enable debug output with `SMTPDebug = 2`. – Synchro Feb 26 '19 at 07:05
  • I've tried what you've suggested however there is no error listed when debugging and the second email still not received :/ – h3k Feb 26 '19 at 19:21
  • "I am not able to send a confirmation to the customer who filled the form" - what does that mean? What **exactly** is not working with the given code? Also, please remove all unneccessary parts - or is this problem directly related to the JS parts? – Nico Haase Aug 17 '23 at 09:36

1 Answers1

0

I finally found the solution of my issue. Basically everything above is correct the only thing which I missed was pass the SMTP parameters again to the new instance of PHPMailer. So now the last piece of code which caused the issue looks like that:

    if($mail->Send()) {
       $autoRespond = new PHPMailer();
 
       $autoRespond->IsSMTP();
       $autoRespond->CharSet    = 'UTF-8';
       $autoRespond->SMTPDebug  = 0;
       $autoRespond->SMTPAuth   = TRUE;
       $autoRespond->SMTPSecure = "tls";
       $autoRespond->Port       = 587;
       $autoRespond->Username   = "office@myhost.co.uk";
       $autoRespond->Password   = "********";
       $autoRespond->Host       = "myhost.co.uk";

       $autoRespond->setFrom('office@myhost.co.uk', 'Alex');
       $autoRespond->addAddress($_email);
       $autoRespond->Subject = "Autorepsonse: We received your submission"; 
       $autoRespond->Body = "We received your submission. We will contact you";

       $autoRespond->Send(); 
}

Thank you for the help everyone.

HDP
  • 4,005
  • 2
  • 36
  • 58
h3k
  • 178
  • 3
  • 13