Reputation: 583
I want to add contact to my website ,so I have searched on the internet for an example and I found this example :
http://www.html-form-guide.com/contact-form/php-email-contact-form.html
I have followed the example but it didnt work, there is no email in my mailbox but it redirect me to the thank you page after the submit.
I have changed the contact-form-handler.php to
<?php
$errors = '';
$myemail = '[email protected]';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact form handler</title>
</head>
<body>
<?php
echo nl2br($errors);
?>
</body>
</html>
My contact.html file :
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator = new Validator("contactform");
frmvalidator.addValidation("name","req","Please provide your name");
frmvalidator.addValidation("email","req","Please provide your email");
frmvalidator.addValidation("email","email","Please enter a valid email address");
</script>
and onther page contact-form-tank-you.html
<div id="body">
<div class="content">
<h1>Thank you!</h1>
Thank you for submitting the form. We will contact you soon!
</div>
</div>
My used programs are:htmp ,wampserver,php 5.3.10
Update
I have opened my php.ini and this what I found:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.yahoo.fr
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = [email protected]
I have logged to logs/appache_error ans I found this error:
[Thu Dec 27 10:11:51 2012] [error] [client 127.0.0.1] PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. dm3sm56414314wib.9 in C:\\wamp\\www\\MyWebsitname\\en\\contact-form-handler.php on line 32, referer: http://localhost/MyWebsitname/en/contact.html
So I have searched in the internet and I found that I shoul add
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
but the problem is the same?
Upvotes: 1
Views: 11821
Reputation: 184
There are many reasons why mail() function doesn't work sometimes, Please read this post PHP mail() doesn't work
I highly recommend to use PHPMailer Library, it's free, easy to use and it's much more reliable than the native php mail() function
Upvotes: 1
Reputation: 24344
I think you are missing the email configuration.
You can configure this in php.ini, see an example here: http://www.quackit.com/php/tutorial/php_mail_configuration.cfm
You will need an SMTP server, the easiest way to do this is to use a free service like mandrill (http://mandrill.com/). Sign up for an account and you will see SMTP details. Enter these details in your php.ini and the emails will be sent via SMTP server from mandril.
Alternatively you can set up a local SMTP server but I imagine this will be a lot more hassle :)
Upvotes: 0