php mail()不在电子邮件中发送html表单

时间:2015-02-08 03:25:40

标签: php html forms email

我无法在任何地方找到它,我在谷歌研究上花了很多时间。 我正在尝试使用php mail()向客户的电子邮件发送HTML表单,它正在发送输入字段,但是一旦我添加表单标签,它就不会发送它,但是脚本会回显“发送的电子邮件” 。 下面是成功发送html输入字段的代码。

<?php error_reporting(E_ALL); ?>
<?php require "connection.php";?>
<?php require "lib/password.php";?>
<?php
$stmt = $conn->prepare("SELECT `email` FROM `users` WHERE `username` = ?");
$stmt->bind_param("s", $username);
$username = mysqli_real_escape_string($conn, preg_replace("/[^0-9a-zA-Z]/","",strip_tags($_POST["username"])));
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($email);
if($stmt->num_rows){
    while ($stmt->fetch()){
        $hash_username = password_hash($username, PASSWORD_BCRYPT, array("cost" => 27));
        $to = $email;
    }
}else{
echo "Username not found, please use correct username.";
}
$stmt->close();
?>
<?php
$subject = "You Requested To Change Your Password";

$message = '<html><head><title>HTML email</title></head><body><p>This email contains HTML Tags!</p><input type="hidden" name="username" value="'.$hash_username.'"><br>Password:<br><input type="text" name="password" value=""><br>Confirm Password:<br><input type="text" name="confirm_password" value=""><br>Email:<br><input type="text" name="lastname" value=""><br><br><input type="submit" value="Submit"></body></html>';

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Blam.Co<info@'.$_SERVER["SERVER_NAME"].'>' . "\r\n";
$headers .= 'Reply-To: Administrator<someone@gmail.com>'."\r\n";

if(mail($to,$subject,$message,$headers)){
    echo "email sent";
}else{
    echo "not sent";
}
?>

现在,只要我添加表单标签,如下所示,它就不会发送,但是脚本会输出“发送的电子邮件”。

<?php error_reporting(E_ALL); ?>
<?php require "connection.php";?>
<?php require "lib/password.php";?>
<?php
$stmt = $conn->prepare("SELECT `email` FROM `users` WHERE `username` = ?");
$stmt->bind_param("s", $username);
$username = mysqli_real_escape_string($conn, preg_replace("/[^0-9a-zA-Z]/","",strip_tags($_POST["username"])));
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($email);
if($stmt->num_rows){
    while ($stmt->fetch()){
        $hash_username = password_hash($username, PASSWORD_BCRYPT, array("cost" => 14));
        $to = $email;
    }
}else{
echo "Username not found, please use correct username.";
}
$stmt->close();
?>
<?php
$subject = "You Requested To Change Your Password";

$message = '<html><head><title>HTML email</title></head><body><p>This email contains HTML Tags!</p><form action="something.php" method="GET"><input type="hidden" name="username" value="'.$hash_username.'"><br>Password:<br><input type="text" name="password" value=""><br>Confirm Password:<br><input type="text" name="confirm_password" value=""><br>Email:<br><input type="text" name="lastname" value=""><br><br><input type="submit" value="Submit"></form></body></html>';

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Blam.Co<info@'.$_SERVER["SERVER_NAME"].'>' . "\r\n";
$headers .= 'Reply-To: Administrator<someone@gmail.com>'."\r\n";

if(mail($to,$subject,$message,$headers)){
    echo "email sent";
}else{
    echo "not sent";
}
?>

如果我们无法在php邮件中发送html表单,您能否建议让客户从电子邮件中放置信息(更改密码)的最佳方式是什么?

我尝试使用javascript函数发送输入字段的值,但它也不会执行。

0 个答案:

没有答案