This question already has an answer here:
I'm currently working on a web portfolio site for one of my web design courses, and it will hopefully become my actual portfolio when finished. Part of the assignment requires a "contact me" section for the site, which would include a form that validates input data and redirects the user on a proper submission. I have this finished, but I want it to actually email me when some submits the form. Here is my code:
$admin_mail = example@mail.com
-code to validate form-
$headers = array("First Name: " => $_POST['first_name'],
"Last Name: " => $_POST['last_name'],
"Email: " => $_POST['email'],
"Company: " => $_POST['company']);
$message = wordwrap($_POST['message'],70);
mail($admin_mail,"Portfolio Message",$message,$headers);
header("location: success.html");
I checked both the $headers
and $message
variables with the print_r
function, and they're working properly. All of the entered data is being added and the page redirects correctly, but the email doesn't appear to send at all. I checked that my webhost can send and receive emails, and that appears to work correctly as well.
Any ideas on how to fix this?
</div>
just check..by imputing the real email Id
<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "
" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
?>
The $headers is wrong. Do check php mail function docs
(Assuming you don't have step-debug available)
Looks like your location header will be set regardless of whether or not the mail sends. mail() returns boolean, so use something like:
if(mail($admin_mail,"Portfolio Message",$message,$headers)){
header(...);
}
else{
// it failed. run some debug here.
}
A jump-start: this is the copy-paste code from a similar contact form on my site. It works like a charm - sends me mail every day. ...well, every day someone uses it :)
$to = 'my.mail@gmail.com';
$subject = 'The Site Contact Request';
$headers = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
$mail = mail($to, $subject, $m, $headers);
return $mail;
headers can be added as below
$headers = 'From: '.$from.''. "
" .
'Reply-To: '.$from.'' . "
" .
'X-Mailer: PHP/' . phpversion();
$message = htmlspecialchars($body);
mail($to, $subject, $message, $headers);