PHP mail() функция $ не работает должным образом

Мой HTML-код:

<form role="form" method="post" >
    <fieldset>
        <div class="form-group">
            <input class="form-control" placeholder="Enter Your Email" name="email" type="text" autofocus>
        </div>

        <!-- Change this to a button or input when using this as a form -->
        <input type="submit" href="dashboard.php" class="btn btn-lg btn-success btn-block btn-warning" value="Reset" name="sub">
    </fieldset>
</form>

мой PHP mail() код:

$email = $_POST['email'];
$to = $email;
$subject = "Password Recovery";
$full="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$reffurl=str_replace("forget_pass_nw_pass.php","",$full);

$message = "You are receiving this e-mail because you have requested to recover your password.";

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <noreply@caveo.sg>' . "\r\n";

$mail_sent = mail($to,$subject,$message,$headers);

я пытался изменить

$to = $email

в

$to = "xxx@gmail.com"

и это на самом деле работает. но когда электронная почта $ to $, электронная почта не отправляется.

2 ответа

Похоже на дополнительную проблему с пробелами. менять

$email = $_POST['email'];

в

$email = trim($_POST['email']);
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
    $to =  $_POST['email'];   
    $to = $email;
$subject = "Password Recovery";
$full="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$reffurl=str_replace("forget_pass_nw_pass.php","",$full);

$message = "You are receiving this e-mail because you have requested to recover your password.";

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <noreply@caveo.sg>' . "\r\n";

$mail_sent = mail($to,$subject,$message,$headers);
Другие вопросы по тегам