0

我无法将信息从我的 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。

4

6 回答 6

1

您的表单缺少该method属性。编辑代码,使您的表单具有methodPOST。

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

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

于 2013-08-29T04:53:47.667 回答
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) ) {
于 2013-08-29T05:01:56.943 回答
0
$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;
if(@mail($to, $subject, $message, $headers)) {
   echo "Mail Sent";
} else {
   echo "Fail";
}
于 2013-08-29T05:38:38.390 回答
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.php要么contact.tpl 不是 contact.tpl.php

于 2013-08-29T04:54:13.723 回答
0

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

于 2013-08-29T04:55:33.883 回答
0

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

<form id="contactForm" class="form-horizontal" action="contact.tpl.php" method="post">
于 2013-08-29T04:56:05.117 回答