9 Replies - 401 Views - Last Post: 07 January 2019 - 08:57 AM Rate Topic: -----

#1 happydesiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-September 18

form not sending

Posted 03 January 2019 - 11:08 AM

I have a contact form that is not sending. I think I missed deleted something or messed it up. It was working before. Can someone take a look at it and see what happened. I am so frustrated. It processes through on the consumer end but I never receive the email.

Here is the code.
<?php

if(isset($_POST['email'])) {
	
	$email_to = "[email protected]";
	
	$email_subject = "I have a question about my order";
	
	
	function died($error) {
		echo "We're sorry, but there's errors found with the form you submitted.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
	}

	if(!isset($_POST['first_name']) ||
		!isset($_POST['last_name']) ||
		!isset($_POST['email']) ||
		!isset($_POST['telephone']) ||
		!isset($_POST['comments'])) {
		died('We are sorry, but there appears to be a problem with the form you submitted.');		
	}
	
	$first_name = $_POST['first_name']; // required
	$last_name = $_POST['last_name']; // required
	$email_from = $_POST['email']; // required
	$telephone = $_POST['telephone']; // not required
	$comments = $_POST['comments']; // required
	
	$error_message = "";
	$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
  	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
	$string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
  	$error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
  	$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
  	$error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
  	died($error_message);
  }
	$email_message = "Form details below.\n\n";
	
	function clean_string($string) {
	  $bad = array("content-type","bcc:","to:","cc:","href");
	  return str_replace($bad,"",$string);
	}
	
	$email_message .= "First Name: ".clean_string($first_name)."\n";
	$email_message .= "Last Name: ".clean_string($last_name)."\n";
	$email_message .= "Email: ".clean_string($email_from)."\n";
	$email_message .= "Telephone: ".clean_string($telephone)."\n";
	$email_message .= "Comments: ".clean_string($comments)."\n";
	
	
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
    
?>

<?php
}
die();
?>



Note: This is on a separate page from the form itself. It links but no email is sent.

Is This A Good Question/Topic? 0
  • +

Replies To: form not sending

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2768
  • View blog
  • Posts: 10,963
  • Joined: 03-December 12

Re: form not sending

Posted 03 January 2019 - 11:16 AM

I don't believe in the mail function. Since you rely on this for a business, switch to a service that can guarantee delivery... MailGun. I have switched everything over to it, including my server messages.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6734
  • View blog
  • Posts: 31,197
  • Joined: 10-May 07

Re: form not sending

Posted 03 January 2019 - 11:20 AM

View Posthappydesiger, on 03 January 2019 - 01:08 PM, said:

It links but no email is sent.

How do you know it's not being sent, vs not being received?

I would put a php log entry in the same conditional where it sends the email to mark that it made it this far. If there is no log, the email isn't being sent. If you get a log entry, then the email is being sent, but not received.

View Postastonecipher, on 03 January 2019 - 01:16 PM, said:

Since you rely on this for a business, ....

I would also question the use of hotmail in a business setting.
Was This Post Helpful? 0
  • +
  • -

#4 happydesiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-September 18

Re: form not sending

Posted 03 January 2019 - 11:46 AM

I only use hotmail to receive these messages. I want them separate from everything else I get for my business.

How do I add a log entry? I am kind of new to php.

I am trying to find out more about mailgun
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6734
  • View blog
  • Posts: 31,197
  • Joined: 10-May 07

Re: form not sending

Posted 03 January 2019 - 12:13 PM

View Posthappydesiger, on 03 January 2019 - 01:46 PM, said:

How do I add a log entry?

error_log()
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,775
  • Joined: 12-December 12

Re: form not sending

Posted 04 January 2019 - 01:39 AM

Also add a few print (echo) statements to your code to see how far it reaches, and whether it ever gets the point of calling the mail function.

Quote

Can someone take a look at it and see what happened.

Erm, difficult to know what happened :whistling: unless source/version control is in place.
Was This Post Helpful? 1
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3811
  • View blog
  • Posts: 13,850
  • Joined: 08-August 08

Re: form not sending

Posted 04 January 2019 - 05:09 PM

I'm guessing it never runs, based on the syntax error on line 22.

That said, here's a simple, easy check:
$expected = ['first_name', 'last_name', 'email'];
foreach($expected as $key) {
    if(empty($_POST[$key])) die('oops');
}

Using different keys? Change the array. Want to send different error messages? Do it this way:
$expected = ['first_name'=>'Missing the first name', 'last_name'=>'Please enter a last name', 'email'=> 'We promise not to spam you if you give us your email address!'];
foreach($expected as $key => $error) {
    if(empty($_POST[$key])) die($error);
}

Was This Post Helpful? 0
  • +
  • -

#8 happydesiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-September 18

Re: form not sending

Posted 04 January 2019 - 06:22 PM

CT,

Are you saying to replace my line 22 with the code you just uploaded?
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3811
  • View blog
  • Posts: 13,850
  • Joined: 08-August 08

Re: form not sending

Posted 04 January 2019 - 06:51 PM

I saw line 22 and missed the function at line 10, so you don't have a syntax error.

You could replace line 22 with that code, or you could replace the function's code with something similar. There are many ways to do this. The key is to understand whatever method you choose. Don't just try to get it working!
Was This Post Helpful? 2
  • +
  • -

#10 happydesiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-September 18

Re: form not sending

Posted 07 January 2019 - 08:57 AM

I can not believe I did this. I found my mistake. I spent a few hours looking over my form and php. I found that I had added and extra quotation mark. Once I removed it everything worked perfect.

Thank you everyone for your help.

CT, It was your message that helped most. I started looking for errors. That is how I found the quotation.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1