I have the following form:
<form action="assets/php/sendmail.php" method="post" id="contactForm">
<p class="text_cont"><input id="name" input type="text" name="name" placeholder="<?php echo FORM_PLACEHOLDER_1 ?>" required></p>
<p class="text_cont"><input id="email" input type="text" name="email" placeholder="<?php echo FORM_PLACEHOLDER_2 ?>" required></p>
<p class="text_cont"><input id="subject" input type="text" name="subject" placeholder="<?php echo FORM_PLACEHOLDER_3 ?>"></p>
<p class="text_cont"><textarea id="message" textarea name="message" placeholder="<?php echo FORM_PLACEHOLDER_4 ?>"></textarea></p>
<div class="col-lg-4 col-md-3 col-sm-3"><p><input type="submit" id="send" class="btn btn-default" value="<?php echo SEND_PLACEHOLDER ?>" /></p></div>
sendmail.php contains the following code because the hosting company gave it to me to run the form (windows servers). I modified it a bit:
error_reporting( E_ALL & ~( E_NOTICE | E_STRICT | E_DEPRECATED ) );
require_once "Mail.php";
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message']);
$to = '[email protected]';
$from = $email;
$host = 'smtp.myhost.com';
$username = 'usernameformail';
$password = 'passwordformail';
$body =
utf8_decode("This mail has been written from your web: \r\n") .
utf8_decode("Name: " . $name . "\r\n") .
utf8_decode("Email: " . $email . "\r\n") .
utf8_decode("Subject: " . $subject . "\r\n") .
utf8_decode("Message: " . $message);
$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject,
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
};
$name = '';
$email = '';
$subject = '';
$message = '';
?>
The fact is that when the user writes and sends the mail, everything is correct. The problem is in the "if (PEAR :: isError ($ mail))" condition. When the "send" button is clicked the web does not redirect:
if (PEAR::isError($mail)) {
header('Location: http://www.myweb.com/error.php');
} else {
header('Location: http://www.myweb.com/thanks.php');
};
If I write a script either:
if (PEAR::isError($mail)) {
echo "<script type='text/javascript'> redirect in javascript to error </script>";
} else {
echo "<script type='text/javascript'> redirect in javascript to thanks</script>";
};
The mail always arrives correctly but the user never knows if everything works correctly. Can you tell me where my error may be?