3
votes

I bought a theme from theme forest to further build it with html and css. However there is a contact form which I have no idea how to use. I'm beginner in php and javascript. I thought that I should just insert my email address to: //ENTER YOUR EMAIL ADDRESS HERE $address = '[email protected]';. But this did not workout.

My question is: How do I make this work so that when somebody try to contact me by filling the contact form I will receive the mail to my email address. I tested by myself on my local server, I don't receive any mail when I tried to fill the contact form.. Anyone please bring me to the right direction.

<?php

if(!$_POST) exit;

function tommus_email_validate($email) { return filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email); }

$name = $_POST['name']; 
$email = $_POST['email']; 
$website = $_POST['website']; 
$comments = $_POST['comments'];

if(trim($name) == '') {
	exit('<div class="alert danger">Attention! You must enter your name.</div>');
} else if(trim($name) == 'Name') {
	exit('<div class="alert danger">Attention! You must enter your name.</div>');
} else if(trim($email) == '') {
	exit('<div class="alert danger">Attention! Please enter a valid email address.</div>');
} else if(!tommus_email_validate($email)) {
	exit('<div class="alert danger">Attention! You have entered an invalid e-mail address.</div>');
} else if(trim($website) == 'Website') {
	exit('<div class="alert danger">Attention! Please enter your website.</div>');
} else if(trim($website) == '') {
	exit('<div class="alert danger">Attention! Please enter your website.</div>');
} else if(trim($comments) == 'Message') {
	exit('<div class="alert danger">Attention! Please enter your message.</div>');
} else if(trim($comments) == '') {
	exit('<div class="alert danger">Attention! Please enter your message.</div>');
} else if( strpos($comments, 'href') !== false ) {
	exit('<div class="alert danger">Attention! Please leave links as plain text.</div>');
} else if( strpos($comments, '[url') !== false ) {
	exit('<div class="alert danger">Attention! Please leave links as plain text.</div>');
} if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }

//ENTER YOUR EMAIL ADDRESS HERE
$address = '[email protected]';

$e_subject = 'You\'ve been contacted by ' . $name . '.';
$e_body = "You have been contacted by $name from $website from your contact form, their additional message is as follows." . "\r\n" . "\r\n";
$e_content = "\"$comments\"" . "\r\n" . "\r\n";
$e_reply = "You can contact $name via email, $email";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $address" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";

if(mail($address, $e_subject, $msg, $headers)) { 
	echo "<fieldset><div id='success_page'><h4 class='remove-bottom'>Email Sent Successfully.</h4><p>Thank you <strong>$name</strong>, your message has been submitted to us.</p></div></fieldset>"; 
}
<section id="contact" class="page-section dark-wrapper">
		<div class="container">
			<div class="row">
				
				<div class="page-header text-center">
					<!-- multiple h1's are perfectly acceptable on a page in valid HTML5, when wrapped in individual sections, they're 100% semantically correct -->
					<h1>Contact Me<small>Here's a Working Contact Form</small></h1>
					<!-- seriously -->
				</div>
				
				<div class="col-sm-8">
					<div id="message"></div>
					<form method="post" action="sendemail.php" id="contactform">
						<input type="text" name="name" id="name" placeholder="Name" />
						<input type="text" name="email" id="email" placeholder="Email" />
						<input type="text" name="website" id="website" placeholder="Website" />
						<textarea name="comments" id="comments" placeholder="Comments"></textarea>
						<input type="submit" name="submit" value="Submit" />
					</form>
				</div>
				
				<div class="col-sm-4 lp30">
					<h5>Contact Me</h5>
					<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar.</p>
					<ul class="fa-ul">
						<li class="bm15"><i class="fa fa-map-marker fa-fw fa-2x fa-li"></i> Denmark</li>
						<li class="bm15"><i class="fa fa-phone-square fa-fw fa-2x fa-li"></i> 08044576869</li>
						<li class="bm15"><i class="fa fa-laptop fa-fw fa-2x fa-li"></i> <a href="mailto:[email protected]" target="_top">[email protected]</a></li>
					</ul>
				</div>	 
				
			</div>
		</div>
	</section>
3
And the question is?u_mulder
Where are you testing? local server or web server.Gaurav Rai
The question is: How do I make this work so that when I send a test mail it will go to my email. Currently I don't receive any mail when I tried to fill the contact form and send.Kami
Yes I tried it on local server. I have my files on my computer.Kami
@RohitashvSinghal mail function works on localhost, you only need to configure mail server. In web servers the administrator has already done that for youbansi

3 Answers

2
votes

If you dont want to bother yourself with writing PHP code, the take a look at Formspree. The service is free for upto 1000 requests a month.

Its also very simple to use : just add the action property to your form. like this :

<form action="//formspree.io/[email protected]"
      method="POST">
    <input type="text" name="name">
    <input type="email" name="_replyto">
    <input type="submit" value="Send">
</form> 
1
votes

You can test this, but you have to have properly configured SMTP server in your php.ini. If you're testing on hosting server, it should not be a problem and if you're testing localy, you have to edit your php.ini file and set SMTP server.

1
votes

Without correct configuration of php.ini you will never send any email locally (you need to set smtp server right). But when you upload it into your webhosting place then it will start to work without any email confing. It will use smtp settings from server. Just try it to upload and you will see that magic.