Trouble with Contact Form

the mail() function

Moderators: egami, macek, gesf

Post Reply
elenvi
New php-forum User
New php-forum User
Posts: 1
Joined: Sun Apr 01, 2018 4:14 pm

Sun Apr 01, 2018 5:12 pm

Hi there,
I have a contact form on a website and when filled out, no email is being sent to the designated email I assigned.
When filled out, the page goes to the home page as stated in the code but that is the only thing it is doing correctly.

Below is what I have for my PHP file.

Code: Select all


<?php

// Define some constants
define( "RECIPIENT_NAME", "John Doe" );
define( "RECIPIENT_EMAIL", "myemail@yahoo.com" );
define( "EMAIL_SUBJECT", "Subject" );


$success = true;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$original_message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
$message = 'Name: '.$senderName.'<br/>Email: '.$senderEmail.'<br/>Message: '.$original_message;


if ( $senderName && $senderEmail && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $senderName . " <" . $senderEmail . ">\n";
  $headers .= "MIME-Version: 1.0\n"; 
  $headers .= "Content-Type: text/HTML; charset=ISO-8859-1\n";
  $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
}

if ( $success )
{
?>
	<script>
		window.location='index.html';
	</script>
<?php
}
else
{
	echo "<h1>test, test.</h1>";
}
?>

Below is what I have in my HTML file.

Code: Select all


<!-- page-section : starts -->
<section id="contact" class="contact page-section add-top add-bottom">

	<!-- inner-section : starts -->
	<section class="inner-section">
	
	<!-- container : starts -->
	<section class="container">

		<div class="row">
			<article class="col-md-12 text-center">
				<h1 class="main-heading main-heading">Contact Us</h1>
				<div class="liner"><span></span></div>
				<p class="promo-text white-text">For any questions or comments, please complete our form below or give us a call us.</p>
			</article>
		</div>

		<div class="row">
			<article class="col-md-12 text-center">
				<div class="alert alert-error error" id="fname">
		                            Name must not be empty
		                            </div>
		        <div class="alert alert-error  error" id="fmail">
		                            Please provide a valid email
		                            </div>
		        <div class="alert alert-error  error" id="fmsg">
		                            Message should not be empty
		                            </div>
			</article>
		</div>

		<form name="myform" action="sendcontact.php" enctype="multipart/form-data" method="post"> 
			<div class="row add-top-small animated" data-fx="pulse">
	        	<article class="col-md-6">
	                    <input type="text" placeholder="Name" id="name" name="name" size="100">
	                    <input type="text" placeholder="Email" name="email" id="email" size="30">
	                     <textarea placeholder="Message" name="message" cols="40" rows="3" id="msg"></textarea>
	        	</article>
	        	<article class="col-md-2">
	                    <button class="btn btn-rounded" id="submit" name="submit" type="submit">Go</button>
	        	</article>

	    </div>
	    </form>

	</section>
	<!-- container : ends -->



	</section>
	<!-- inner-section : ends -->




</section>
<!-- page-section : ends -->

Please let me know what I need to fix. Thank you for your time!

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 708
Joined: Mon Feb 22, 2016 5:52 pm

Mon Apr 02, 2018 6:26 am

If you are sending from a server, you will probably need to specify and return address

Code: Select all

# Add this to your header string
# Putting your reply e-mail address as appropriate
"Reply-To: webmaster@example.com\r\n"
Many mail servers need to know that it is from its own server to prevent spammers.

You might not be able to send from your PC, not sure if that's what you are doing?

More on it here.

Enache
New php-forum User
New php-forum User
Posts: 7
Joined: Mon Apr 02, 2018 1:19 pm

Mon Apr 02, 2018 2:13 pm

Aye, sending mail without it being caught by a spam filter is like walking in a minefield. There's several different protocols (SPF, DKIM, etc) and each email service (Hotmail, Gmail and such) has their own specific requirements your server config must meet before they'll accept mail from you.

Also, I see you allow the "from" address to be specified? If you're sending mail from email addresses on domains other than your own, that's pretty much guaranteed to get blocked.

In any case, if you're just interested in testing the script there should be ways to configure your mail server to log outgoing mail to a file or something.

Post Reply