编辑:这不是该问题的重复。我没有任何问题使用我的PHP脚本发送电子邮件OUT,如该问题中所述。我在从HTML表单中获取数据时遇到问题。
大家好我有一个我无法解决的问题,希望你能帮助我。
当我使用这个PHP代码时,我能够从我的HTML表单发送一封电子邮件:
<?php
$email ="anymail@gmail.com";
$to ="myemail@gmail.com";
$subject ="Test Subject";
$message ="Message";
$headers ='From:support@mywebsite.com';
mail($to, $subject, $message, $headers);
?>
但是当我用我的HTML表单中的消息替换$message
时不是这样:
<?php
$email ="anymail@gmail.com";
$to ="myemail@gmail.com";
$subject ="Test Subject";
$message =$_POST['InputMessage'];
$headers ='From:support@mywebsite.com';
mail($to, $subject, $message, $headers);
?>
HTML表单代码:
<form role="form" action="send_contact.php" method="post" >
<div class="col-md-6">
<div class="form-group">
<label for="InputName">Your Name</label>
<div class="input-group">
<input type="text" class="form-control" name="InputName" id="InputName" placeholder="Enter Name" required>
<span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
</div>
</div>
<div class="form-group">
<label for="InputEmail">Your Email</label>
<div class="input-group">
<input type="email" class="form-control" id="InputEmail" name="InputEmail" placeholder="Enter Email" required >
<span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
</div>
</div>
<div class="form-group">
<label for="InputMessage">Message</label>
<div class="input-group">
<textarea name="InputMessage" id="InputMessage" class="form-control" rows="5" required></textarea>
<span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
</div>
</div>
<input type="submit" name="submit" id="submit" value="Submit" class="btn wow tada btn-embossed btn-primary pull-right">
</div>
</form>
有谁知道为什么会这样?
答案 0 :(得分:0)
如果您使用的是PHP MAILER,则可以使用此代码。它可以使用。
<?php
require_once "PHPMailer-master/PHPMailerAutoload.php";
$receiver_mail = ''; //Your Email id
$receiver_name = ''; //Your Name
$sender_name = $_POST['InputName'];
$sender_mail = $_POST['InputEmail'];
$message = $_POST['InputMessage'];
$body = 'Name: '.$sender_name.'<br/>
Email: '.$sender_mail.'<br/>
Message: '.$message.'<br/>';
$mail = new PHPMailer;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "" ;
$mail->SMTPAuth = false;
//Provide username
$mail->Username = "" ;
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "ssl";
//Set TCP port to connect to
$mail->Port = 465;
$mail->From = $sender_mail;
$mail->FromName = $sender_name;
$mail->addAddress($receiver_mail, $receiver_name);
$mail->isHTML(true);
$mail->Body = $body;
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
header("Location of Your site",true,301);
exit();
}
?>