表格不发送电子邮件

时间:2013-08-29 04:51:37

标签: php html forms email

我无法将PHP表单中的信息发送到电子邮件地址。我是PHP的新手。代码如下:

if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$myEmail = "shivambh28@gmail.com";

if (empty($name) || empty($subject) || empty($message)) {
  $error = 'Please make sure to double check the fields for errors.';
} elseif (!filter_var($email1, FILTER_VALIDATE_EMAIL)) {
  $error = 'Email is incorrect';
} else {
  $headers .= "From: $email\r\n";
  $headers .= "Reply-To: $myEmail\r\n";
  $headers .= "Return-Path: $myEmail\r\n";
  $headers .= "CC: $email\r\n";
  $headers .= "BCC: $myEmail\r\n";

  if ( mail($to,$subject,$message,$headers) ) {
    $to = $myEmail;
    $subject = $subject;
    $message = $message;
    $from = $email;
    $headers = "From:" . $from;
    echo "Mail Sent.";
  } else {
    echo 'failure to send email';
  }
}
}

HTML:

<form id="contactForm" class="form-horizontal" action="<?php echo get_option('home'); ?>/email/"  method="POST">
    <input id="name" name="name" placeholder="Full Name" type="text">
    <input id="subject" name="subject" placeholder="Subject" type="text">
    <input id="email" name="email" placeholder="Email Address" type="email">
    <textarea placeholder="Your Message" id="message" name="message" rows="10"></textarea>
    <input type="submit" value="SEND" class="btn btn-primary btn-block">
</form>

注意:我使用的是WP CMS。

6 个答案:

答案 0 :(得分:1)

您的表单缺少method属性。编辑代码,以便您的表单具有method POST。

<form id="contactForm" class="form-horizontal" action="contact.tpl.php" method="POST">

其次删除一个mail函数调用。如果不是,您的电子邮件将被发送两次

答案 1 :(得分:0)

更改您的代码。它发送邮件2次

$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;

if ( mail($to,$subject,$message,$headers) ) {

    echo "Mail Sent.";
} else {
    echo 'failure to send email';
}

您的表单方法POST

类似
<form id="contactForm" class="form-horizontal" action="contact.php" method="post">

您的文件名主要是contact.phpcontact.tpl contact.tpl.php

答案 2 :(得分:0)

第一个.变量声明中缺少$headers句号)。可能有帮助。

答案 3 :(得分:0)

表单标记中缺少表单方法 POST

<form id="contactForm" class="form-horizontal" action="contact.tpl.php" method="post">

答案 4 :(得分:0)

$to

中的参数mail()错误

尝试

....
....
//// use $email here not $to which is not initialised yet
if ( mail($email,$subject,$message,$headers) ) {

的地方
 if ( mail($to,$subject,$message,$headers) ) {

答案 5 :(得分:0)

$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;
if(@mail($to, $subject, $message, $headers)) {
   echo "Mail Sent";
} else {
   echo "Fail";
}