如何从表单提交电子邮件?

时间:2016-07-18 06:12:37

标签: php email

我正在尝试通过单击表单的提交按钮发送电子邮件,但在提交给定表单后,消息显示为"您的邮件已成功发送!感谢您的反馈意见#34;但实际上电子邮件没有发送到用户在表单中输入的给定电子邮件ID ...请帮我解决给定的问题!

//secure_email_form.php
<body>

<div class="container">
<!-- Feedback Form Starts Here -->
<div id="feedback">
<!-- Heading Of The Form -->
<div class="head">
<h3>FeedBack Form</h3>
<p>This is feedback form. Send us your feedback !</p>
</div>
<!-- Feedback Form -->
<form action="#" id="form" method="post" name="form">
<input name="vname" placeholder="Your Name" type="text" value="">
<input name="vemail" placeholder="Your Email" type="text" value="">
<input name="sub" placeholder="Subject" type="text" value="">
<label>Your Suggestion/Feedback</label>
<textarea name="msg" placeholder="Type your text here..."></textarea>
<input id="send" name="submit" type="submit" value="Send Feedback">
</form>
<h3><?php include "secure_email_code.php"?></h3>
</div>
<!-- Feedback Form Ends Here -->
</div>
</body>

//secure_email_code.php
<?php
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if($_POST["vname"]==""||$_POST["vemail"]==""||$_POST["sub"]==""||$_POST["msg"]==""){
echo "Fill All Fields..";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['vemail'];
// 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{
$subject = $_POST['sub'];
$message = $_POST['msg'];
$headers = 'From:'.'info@example.com'."\r\n"; // Sender's Email
$headers = 'Cc:'.'info@example1.com'."\r\n"; // Carbon copy to Sender
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
mail("recievers_mail_id@xyz.com", $subject, $message, $headers);
echo "Your mail has been sent successfuly ! Thank you for your feedback";
}
}
}
?>

2 个答案:

答案 0 :(得分:1)

看起来你的邮件功能没有正常执行。您可以尝试使用var_dump或其他东西进行调试。

此外,您的代码逻辑上不正确。它应该仅在邮件功能成功执行时显示消息。

$send_mail = mail("recievers_mail_id@xyz.com", $subject, $message, $headers);
var_dump($send_mail);  // See what you get here. It'll probably show false.

if ($send_mail) {
   echo "Your mail has been sent successfuly ! Thank you for your feedback";
} else {
    echo "Email sending failed!";
}

答案 1 :(得分:0)

您必须使用php文件网址或使用空白操作。在这里你必须使用动作空白。

<form action="" id="form" method="post" name="form"> 

而不是

<form action="#" id="form" method="post" name="form">