Jump to content

Ajax PHP contact form help


whitt

Recommended Posts

I am attempting to create a php AJAX contact form , however the email never seems to arrive in my outlook.
 

/*
  Jquery Validation using jqBootstrapValidation
   example is taken from jqBootstrapValidation docs 
  */
$(function() {

 $("input,textarea").jqBootstrapValidation(
    {
     preventSubmit: true,
     submitError: function($form, event, errors) {
      // something to have when submit produces an error ?
      // Not decided if I need it yet
     },
     submitSuccess: function($form, event) {
      event.preventDefault(); // prevent default submit behaviour
       // get values from FORM
       var name = $("input#name").val();  
       var email = $("input#email").val();
       var phone = $("input#phone").val();  
       var message = $("textarea#message").val();
        var firstName = name; // For Success/Failure Message
           // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
	   firstName = name.split(' ').slice(0, -1).join(' ');
         }        
	 $.ajax({
                url: "./bin/contact_me.php",
            	type: "POST",
            	data: {name: name,phone:phone, email: email, message: message},
            	cache: false,
            	success: function() {  
            	// Success message
            	   $('#success').html("<div class='alert alert-success'>");
            	   $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
            		.append( "</button>");
            	  $('#success > .alert-success')
            		.append("<strong>Your message has been sent. </strong>");
 		  $('#success > .alert-success')
 			.append('</div>');
 						    
 		  //clear all fields
 		  $('#contactForm').trigger("reset");
 	      },
 	   error: function() {		
 		// Fail message
 		 $('#success').html("<div class='alert alert-danger'>");
            	$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
            	 .append( "</button>");
            	$('#success > .alert-danger').append("<strong>Sorry "+firstName+" it seems that my mail server is not responding...</strong> Could you please email me directly ? Sorry for the inconvenience!");
 	        $('#success > .alert-danger').append('</div>');
 		//clear all fields
 		$('#contactForm').trigger("reset");
 	    },
           })
         },
         filter: function() {
                   return $(this).is(":visible");
         },
       });

      $("a[data-toggle=\"tab\"]").click(function(e) {
                    e.preventDefault();
                    $(this).tab("show");
        });
  });
 

/*When clicking on Full hide fail/success boxes */ 
$('#name').focus(function() {
     $('#success').html('');
  });
<?php


require("../lib/phpmailer/PHPMailerAutoload.php");

if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone']) ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

    $m             = new PHPMailer();
    $m->IsSMTP();
    $m->SMTPAuth   = true;
    $m->SMTPDebug  = 2;                    
    $m->Host       = "smtp-mail.outlook.com";
   
                      
                     
    $m->Username   = ""; 
    $m->Password   = "";
    $m->SMTPSecure = 'tls';
    $m->Port       = 587;  
   
    
    $m->addAddress('EXAMPLE@outlook.com', $name);

    $name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

    
    $m->Subject = "Contact Form has been submitted";
    $m->Body = "You have received a new message. <br><br>".
                  " Here are the details:<br> Name: $name <br> Phone Number: $phone <br>".
                  "Email: $email_address<br> Message <br> $message";
   

if(!$m->Send()) {
      echo "Mailer Error: " . $m->ErrorInfo;
      exit;
    } 
?>
Link to comment
https://forums.phpfreaks.com/topic/295049-ajax-php-contact-form-help/
Share on other sites

How do you know it submits fine? You don't check anything returned from your ajax call like the error messages you are sending if there is a problem. Try changing your ajax success method to this and check the browser console for anything returned.

success: function(data) { 
  console.log(data);
 //..rest of success code

Console doesn't appear to be coming up with anything which is odd as this script seems to work with other php mail scripts i've written it only seems to not like phpmailer.

I have attached my files , im going to keep looking at it but i cant see whats wrong.

test.zipFetching info...

Besides the possible issues with your script, also understand that ANY of the Microsoft email domains (outlook, hotmail, live, msn) are very very hard to have an email arrive in the inbox.  They have very strict rules in place to prevent spam, so make sure to check your spam box before deciding that the email has never arrived.  Try sending it to a yahoo or gmail address too and see if it arrives there.  Those email domains are far less strict.

  On 3/5/2015 at 9:37 PM, fastsol said:

Besides the possible issues with your script, also understand that ANY of the Microsoft email domains (outlook, hotmail, live, msn) are very very hard to have an email arrive in the inbox.  They have very strict rules in place to prevent spam, so make sure to check your spam box before deciding that the email has never arrived.  Try sending it to a yahoo or gmail address too and see if it arrives there.  Those email domains are far less strict.

With Gmail when the form is submitted i get a suspicious sign in attempt email from Gmail.

I did a basic just send an email PHP scrip i have the emails going to my in box with this 

 

<?php

require_once 'PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth= true;



$mail->Host = 'smtp-mail.outlook.com';
$mail->Username = 'myemail@outlook.com';
$mail->Password = 'pass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->From = 'sender@outlook.com';
$mail->FromName = $name;
$mail->addReplyTo('sender@outlook.com',$name);
$mail->addAddress('myemail@outlook.com',me);

$mail->Subject = 'Testing Email';
$mail->Body = 'This is a Test Email ssl';
$mail->AltBody ='This is a Test Email';

var_dump($mail->send());

?>

This may seem obvious, but it has't been mentioned.  Have you turned on SMTP error reporting?  

/*
* $mail->SMTPDebug = 0; disable debugging, 0 is the default)
* $mail->SMTPDebug = 1; echo errors and server responses
* $mail->SMTPDebug = 2; echo errors, server responses and client messages
*/
$mail->SMTPDebug = 1;

I'm not as familiar with PHPMailer, but with other classes/frameworks, you can pass additional headers.  That may be where you want to research, specifically for MS mail.  BTW, I've used part of your ajax script to update on of my own.  I like how your handling post submit UI feedback. Good luck

Literally 5 minutes later and i get this "





2015-03-06 11:59:37 CLIENT -> SERVER: EHLO localhost 2015-03-06 11:59:37 CLIENT -> SERVER: STARTTLS 2015-03-06 11:59:37 CLIENT -> SERVER: EHLO localhost 2015-03-06 11:59:38 CLIENT -> SERVER: AUTH LOGIN 2015-03-06 11:59:38 CLIENT -> SERVER: bWF0dGhld3B3aGl0dGluZ3RvbkBvdXRsb29rLmNvbQ== 2015-03-06 11:59:38 CLIENT -> SERVER: TWF2ZXJpY2s4OQ== 2015-03-06 11:59:39 CLIENT -> SERVER: MAIL FROM: 2015-03-06 11:59:39 CLIENT -> SERVER: RCPT TO: 2015-03-06 11:59:39 CLIENT -> SERVER: DATA 2015-03-06 11:59:39 CLIENT -> SERVER: Date: Fri, 6 Mar 2015 11:59:37 +0000 2015-03-06 11:59:39 CLIENT -> SERVER: To: Matthew Whittington 2015-03-06 11:59:39 CLIENT -> SERVER: From: matthewpwhittington@outlook.com 2015-03-06 11:59:39 CLIENT -> SERVER: Reply-To: sender 2015-03-06 11:59:39 CLIENT -> SERVER: Subject: Testing Email 2015-03-06 11:59:39 CLIENT -> SERVER: Message-ID: <26e958edff522d3f6a43f03aaf8bab81@localhost> 2015-03-06 11:59:39 CLIENT -> SERVER: X-Priority: 3 2015-03-06 11:59:39 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.9 (https://github.com/PHPMailer/PHPMailer/) 2015-03-06 11:59:39 CLIENT -> SERVER: MIME-Version: 1.0 2015-03-06 11:59:39 CLIENT -> SERVER: Content-Type: multipart/alternative; 2015-03-06 11:59:39 CLIENT -> SERVER: boundary="b1_26e958edff522d3f6a43f03aaf8bab81" 2015-03-06 11:59:39 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit 2015-03-06 11:59:39 CLIENT -> SERVER: 2015-03-06 11:59:39 CLIENT -> SERVER: --b1_26e958edff522d3f6a43f03aaf8bab81 2015-03-06 11:59:39 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii 2015-03-06 11:59:39 CLIENT -> SERVER: 2015-03-06 11:59:39 CLIENT -> SERVER: This is a Test Email 2015-03-06 11:59:39 CLIENT -> SERVER: 2015-03-06 11:59:39 CLIENT -> SERVER: 2015-03-06 11:59:39 CLIENT -> SERVER: --b1_26e958edff522d3f6a43f03aaf8bab81 2015-03-06 11:59:39 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii 2015-03-06 11:59:39 CLIENT -> SERVER: 2015-03-06 11:59:39 CLIENT -> SERVER: This is a Test Email ssl 2015-03-06 11:59:39 CLIENT -> SERVER: 2015-03-06 11:59:39 CLIENT -> SERVER: 2015-03-06 11:59:39 CLIENT -> SERVER: 2015-03-06 11:59:39 CLIENT -> SERVER: --b1_26e958edff522d3f6a43f03aaf8bab81-- 2015-03-06 11:59:39 CLIENT -> SERVER: 2015-03-06 11:59:39 CLIENT -> SERVER: . 2015-03-06 11:59:40 SMTP ERROR: DATA END command failed: 550 5.3.4 Requested action not taken; To continue sending messages, please sign in to your account. 2015-03-06 11:59:40 SMTP Error: data not accepted. bool(false) 2015-03-06 11:59:40 CLIENT -> SERVER: QUIT 2015-03-06 11:59:40 SMTP ERROR: QUIT command failed:" @outlook.com>@outlook.com>@outlook.com>@outlook.com>

  On 3/6/2015 at 11:54 AM, rwhite35 said:

This may seem obvious, but it has't been mentioned.  Have you turned on SMTP error reporting?  

/*
* $mail->SMTPDebug = 0; disable debugging, 0 is the default)
* $mail->SMTPDebug = 1; echo errors and server responses
* $mail->SMTPDebug = 2; echo errors, server responses and client messages
*/
$mail->SMTPDebug = 1;

I'm not as familiar with PHPMailer, but with other classes/frameworks, you can pass additional headers.  That may be where you want to research, specifically for MS mail.  BTW, I've used part of your ajax script to update on of my own.  I like how your handling post submit UI feedback. Good luck

 

If my AJAX is working it has to be on the PHP end

The error reporting looks like your being denied SMTP because you're not logged in.  Do you have access to sendmail and have you tried setting that instead of SMTP?  

 

Reason for delay in error message is because your mail server is set to try and send your mail and fails after a set period of time.

 

Also, your sending the test from you - to you... That email never leave your machine, those are handled differently then mail to remote address. Try another outlook.com address, outside your development server, if you can.

<?php
require_once 'PHPMailerAutoload.php';
// check if fields passed are empty
if(empty($_POST['name'])  		||
   empty($_POST['email']) 		||
   empty($_POST['message'])	||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
	echo "No arguments Provided!";
	
   }
	
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
	

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth= true;
$mail->SMTPDebug = 1;


$mail->Host = 'smtp-mail.outlook.com';
$mail->Username = 'email';
$mail->Password = 'pass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->From = $email_address;
$mail->FromName = $name;
$mail->addReplyTo('reply@outlook.com','reply');
$mail->addAddress('testytestersontesting@outlook.com','Testy Testerson');

   $mail->isHTML(true);
   $mail->Subject = "Contact Form has been submitted";
   $mail->Body = "You have received a new message. <br><br>".
                  " Here are the details:<br> Name: $name <br> Phone Number: $phone <br>".
                  "Email: $email_address<br> Message <br> $message";

echo var_dump($mail->send());
return true;			
?>

I just flat out cannot see why this does not work , the script on its own works but when i try to integrate it into the form i get my ajax sending back my error alert.

<?php
// check if fields passed are empty
if(empty($_POST['name'])      ||
   empty($_POST['email'])     ||
   empty($_POST['phone'])     ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
  echo "No arguments Provided!";
  return false;
   }
  
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
  
// create email body and send it  
$to = 'email@gmail.com'; // put your email
$email_subject = "Contact form submitted by:  $name";
$email_body = "You have received a new message. \n\n".
          " Here are the details:\n \nName: $name \n ".
          "Email: $email_address\n Phone: $phone\n Message \n $message";
$headers = "From: whitegatescattery.com\n";
$headers .= "Reply-To: email@gmail.com"; 
mail($to,$email_subject,$email_body,$headers);
return true;      
?>

This worked with Gmail i dont get why this 

 

 

<?php

require_once './libPHPMailerAutoload.php';
// check if fields passed are empty
// check if fields passed are empty

$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];

if(empty($_POST['name'])      ||
   empty($_POST['email'])     ||
   empty($_POST['phone'])     ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
  echo "No arguments Provided!";
  return false;
   }
  

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth= true;
$mail->SMTPDebug = 1;


$mail->Host = 'smtp-mail.outlook.com';
$mail->Username = 'email';
$mail->Password = 'pass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->From = $email_address;
$mail->FromName = $name;
$mail->addReplyTo('reply@outlook.com','reply');
$mail->addAddress('@outlook.com','me');

   $mail->isHTML(true);
   $mail->Subject = "Contact Form has been submitted";
   $mail->Body = "You have received a new message. <br><br>".
                  " Here are the details:<br> Name: $name <br> Phone Number: $phone <br>".
                  "Email: $email_address<br> Message <br> $message";

var_dump($mail->send());
return true;			


?>

Is causing my ajax to append my fail message

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.