0

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?

GAMITG
  • 3,810
  • 7
  • 32
  • 51
Jordan U.
  • 313
  • 1
  • 5
  • 16
  • 3
    headers array is just wrong, check the manual. –  Sep 08 '15 at 04:47
  • There doesn't seem to be anything in the spam folder. I also have my webhost set to forward emails to my personal email address, and there isn't anything on that account either – Jordan U. Sep 08 '15 at 04:48
  • I updated the my coding to the following, using the headers section of the manual as a guideline, but it still doesn't seem to work. `$headers = "From: ".$_POST['email'] . "\r\n" . $_POST['first_name'] . "\r\n" . $_POST['last_name'] . "\r\n" . $_POST['company'] . "\r\n" . 'CC: '.$admin_mail; $message = wordwrap($_POST['message'], 70, "\n", false); mail($admin_mail,"Portfolio Message",$message,$headers); header("location: success.html");` – Jordan U. Sep 08 '15 at 05:05
  • that's still wrong, do you actually read the manual page/ –  Sep 08 '15 at 06:22
  • This question, and all the bug-filled answers posted so far, are a good example of why you should never call `mail()` yourself. Use a library that will do it correctly - for example [PHPMailer](https://github.com/PHPMailer/PHPMailer) that you tagged this question with. – Synchro Sep 08 '15 at 06:49

4 Answers4

0

just check..by imputing the real email Id

<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
?>
Suman Singh
  • 1,379
  • 12
  • 20
0

headers can be added as below

            $headers =  'From: '.$from.''. "\r\n" .
                'Reply-To: '.$from.'' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
                $message = htmlspecialchars($body);
                mail($to, $subject, $message, $headers);
mahen3d
  • 7,047
  • 13
  • 51
  • 103
-1

The $headers is wrong. Do check php mail function docs

Basit
  • 936
  • 5
  • 13
-1

(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' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$mail = mail($to, $subject, $m, $headers);
return $mail;
Kevin Ard
  • 594
  • 4
  • 12
  • @mahen3d and I use the same header format, and it seems to work for us both. Stringify your assoc. array? – Kevin Ard Sep 08 '15 at 05:04