【问题标题】:PHP, Not able to send EmailsPHP,无法发送电子邮件
【发布时间】:2012-08-31 18:20:38
【问题描述】:

我想在我的表格上有一个需要 *fullname、*email 地址以及 *subject 和 *message 的联系表。我按照教程开发了我的表单,但由于某种原因它没有发送我的测试消息。

我没有足够的 PHP 经验来弄清楚我做错了什么,所以我希望得到有关如何解决这个问题的建议。欢迎提出所有问题、建议和可能的解决方案。谢谢。

联系表格 php: 代码:

<?php 

// EDIT THE FOLLOWING LINE BELOW AS REQUIRED

$send_email_to = "jb@me.com";

function send_email($name,$email,$email_subject,$email_message)
{
  global $send_email_to;  

  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $headers .= "From: ".$email. "\r\n";

  $message = "<strong>Email = </strong>".$email."<br>";
  $message .= "<strong>Name = </strong>".$name."<br>";
  $message .= "<strong>Message = </strong>".$email_message."<br>";
  @mail($send_email_to, $email_subject, $message,$headers);
  return true;
}

function validate($name,$email,$message,$subject)
{
  $return_array = array();
  $return_array['success'] = '1';
  $return_array['name_msg'] = '';
  $return_array['email_msg'] = '';
  $return_array['message_msg'] = '';
  $return_array['subject'] = '';

 if($email == '')
  {
    $return_array['success'] = '0';
    $return_array['email_msg'] = 'email is required';
  }
  else
  {
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email)) {
      $return_array['success'] = '0';
      $return_array['email_msg'] = 'enter valid email.';  
    }
  }

  if($name == '')
  {
    $return_array['success'] = '0';
    $return_array['name_msg'] = 'name is required';
  }
  else
  {
     $string_exp = "/^[A-Za-z .'-]+$/";
    if (!preg_match($string_exp, $name)) {
      $return_array['success'] = '0';
     $return_array['name_msg'] = 'enter valid name.';
    }
  }


  if($subject == '')
  {
    $return_array['success'] = '0';
    $return_array['subject_msg'] = 'subject is required';
  }

  if($message == '')
  {
    $return_array['success'] = '0';
    $return_array['message_msg'] = 'message is required';
  }
  else
  {
    if (strlen($message) < 2) {
      $return_array['success'] = '0';
      $return_array['message_msg'] = 'enter valid message.';
    }
  }
  return $return_array;
}

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];

$return_array = validate($name,$email,$message,$subject);
if($return_array['success'] == '1')
{
  send_email($name,$email,$subject,$message);
}

header('Content-type: text/json');
echo json_encode($return_array);
die();

?>

联系表格 HTML: 代码:

<fieldset id="contact_form">
          <div id="msgs"> </div>
          <form id="cform" name="cform" method="post" action="">
            <input type="text" id="name" name="name" value="Full Name*" onfocus="if(this.value == 'Full Name*') this.value = ''"
                            onblur="if(this.value == '') this.value = 'Full Name*'" />
            <input type="text" id="email" name="email" value="Email Address*" onfocus="if(this.value == 'Email Address*') this.value = ''"
                            onblur="if(this.value == '') this.value = 'Email Address*'" />
            <input type="text" id="subject" name="subject" value="Subject*" onfocus="if(this.value == 'Subject*') this.value = ''"
                            onblur="if(this.value == '') this.value = 'Subject*'" />
            <textarea id="msg" name="msg" onfocus="if(this.value == 'Your Message*') this.value = ''"
                            onblur="if(this.value == '') this.value = 'Your Message*'">Your Message*</textarea>
            <button id="submit" class="button"> Send Message</button>
          </form>
        </fieldset>

【问题讨论】:

  • 从邮件中删除“@”...并且不要通过“手”验证电子邮件地址
  • @Dagon:我从评论中猜测 OP 从某个地方得到了它,所以它可能在某种程度上有效。不管它是否运作良好......
  • 我同意@Dagon。 var_dump($_POST) 查看您的表单是否到达控制器。
  • 名称验证也是错误的。只是抢占 OP 的下一篇文章。
  • 从使用的 json_encode() 来看,我假设这里涉及到 AJAX。 OP,只要您使用 JS,您就应该在客户端验证表单。如果您还没有使用它,请获取自己的 jQuery,然后获取此插件。 docs.jquery.com/Plugins/Validation

标签: php forms validation


【解决方案1】:

您的表单操作未设置为任何内容。您需要将其指向您那里的发送电子邮件的脚本。

即:

<form id.. name.. method.. action="/handle_post.php">

【讨论】:

  • 根本不是这样的。如果您查看脚本,它会在同一个 url 上发布给自己。 action='' 是一种有效的方法(不是我推荐的)。
  • @MatthewBlancarte:假设表单是由同一个脚本输出的,当然。但是,从脚本看起来“开箱即用”的事实来看,它可能没问题。
  • PHP 表单脚本仍然无法正常工作。我回到我下载脚本的网站,它甚至在那里不起作用!!!所以看起来我将不得不编写一个 PHP 表单脚本.. sigh。谁能给我指出一个关于创建带有验证的 php 表单脚本的好教程?同样出于好奇,php表单不是必须在.php文件中还是可以在.html文件中?
【解决方案2】:

设置正确的操作,然后尝试它应该可以工作.. 如果它仍然不起作用,请使用 curl 实用程序对其进行测试,以查看您的脚本是否正常.. 我看到的另一个错误是,你的文本区域表单名称是 msg,而在服务器上你期望你的 post 请求它有消息。那行不通。。 如果您仍然面临问题,那么我们将进一步调试 :)

【讨论】:

    【解决方案3】:

    首先,您需要为一些脚本设置操作,这些脚本将处理同一文件的 $_POST 输入 (contactform.php) 或 $_SERVER['PHP_SELF']

    其次你应该通过输入type=submit而不是按钮来发送数据。

    【讨论】:

    • 为 action 属性保留一个空字符串是一种将表单发布到自身的有效方式。 $_SERVER['PHP_SELF'] 是不必要的。
    猜你喜欢
    • 2013-09-04
    • 1970-01-01
    • 2013-07-15
    • 2016-04-20
    • 1970-01-01
    • 2013-09-22
    • 2021-03-22
    相关资源
    最近更新 更多