PHP E-mail Feedback Form

653 views Asked by At
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = '[email protected]';
$subject = 'Feedback from '. $field_name

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for your feedback, have a nice day!');
        window.location = 'some undefined location';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Sorry your feedback was not sent, please try again soon!');
        window.location = 'some undefined location';
    </script>
<?php
}

?>

I'm trying to create a form that submits the feedback to my desired email adress, the form seems to be running smoothly when i enter all the details but I am not receiving any emails?

Can anyone see why?

Thanks!

3

There are 3 answers

1
Aaska Patel On BEST ANSWER

write headers like this :

$headers = "From: " . $field_email. "\r\n";
$headers .= "Reply-To: ". $field_email. "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

example :

$from = "[email protected]";
$to = "[email protected]";
$subject = "subject trail mail";
$mail_msg = "trail mail body";

$headers = "From: "  . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$retval = mail ($to,$subject,$mail_msg,$headers);

if( $retval == true )  
{
    $st = "mail sent successfully";
}
else{
    $st = "mail error" ;
}
echo "mail Status : " . $st ;
1
Ash King On

$subject = 'Feedback from '. $field_name

should be: $subject = 'Feedback from '. $field_name;

0
Freek Gille On

Is there actually anything being POST'ed? If I replace the three first POST with just data I get the mails correctly. Can you echo the value you're posting?

This code:

>  <?php $field_name = 'Name'; $field_email = '[email protected]';
> $field_message = 'This is the message';
> 
> $mail_to = '[email protected]'; $subject = 'Feedback from '.
> $field_name;
> 
> $body_message = 'From: '.$field_name."\n"; $body_message .= 'E-mail:
> '.$field_email."\n"; $body_message .= 'Message: '.$field_message;
> 
> $headers = 'From: '.$field_email."\r\n"; $headers .= 'Reply-To:
> '.$field_email."\r\n";
> 
> $mail_status = mail($mail_to, $subject, $body_message, $headers);
> 
> if ($mail_status) { ?>
>     <script language="javascript" type="text/javascript">
>         alert('Thank you for your feedback, have a nice day!');
>     </script> <?php } else { ?>
>     <script language="javascript" type="text/javascript">
>         alert('Sorry your feedback was not sent, please try again soon!');
>     </script> <?php } ?>

Results in this: enter image description here