PHP表单字段不会发送到电子邮件

时间:2013-03-13 10:20:58

标签: forms field php

我已经创建了一个发送电子邮件PHP脚本。

当只发送一个字段(消息字段)时,表单似乎正常工作,但是一旦添加其他字段,表单就不再通过电子邮件发送到收件箱(但表单仍然正确触发并重新路由到thankyou很好。)

以下是我目前的代码,此不起作用

<?php
  $email = $_REQUEST['email'] ;
  $name = $_REQUEST['name'] ;
  $services = $_REQUEST['services'] ;
  $message = $_REQUEST['message'] ;

  if (!isset($_REQUEST['email'])) {
    header( "Location: feedback.html" );
  }
  elseif (empty($email) || empty($message)) {
     header( "Location: error.html" );
 }
  else {
    mail( "info@website.co.uk", "Message via your website!",
           $name, $services, $message, "From: $email" );
    header( "Location: thankyou.html" );
  }
?>

这是以前的代码,这确实有效,但只显示消息

<?php
  $email = $_REQUEST['email'] ;
  $message = $_REQUEST['message'] ;

  if (!isset($_REQUEST['email'])) {
    header( "Location: feedback.html" );
  }
  elseif (empty($email) || empty($message)) {
     header( "Location: error.html" );
 }
  else {
    mail( "info@website.co.uk", "Message via your website!",
          $message, "From: $email" );
    header( "Location: thankyou.html" );
  }
?>

这是表单的HTML:

<form method="post" action="sendmail.php">
    <label>Name:</label> <input name="name" type="text" /><br />
    <label>Email:</label> <input name="email" type="text" /><br />
    <label>What service do you require?:</label>  <input name="services" type="text" /><br />
    <label>Message:</label><br />
    <textarea name="message" rows="15" cols="40">
    </textarea><br />
    <input type="submit" />
</form>

4 个答案:

答案 0 :(得分:3)

根据mail() docs,消息正文应作为单个参数传递。所以看起来应该更像:

mail( "info@website.co.uk", "Message via your website!", "$name\r\n $services\r\n $message", "From: $email");

答案 1 :(得分:1)

根据php.net,正确使用mail()函数是:

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

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

您正尝试将其他数据($services)作为标题发送。

尝试合并您的$message$service&gt;

$message = $service . "\r\n\r\n" . $message;
 mail( "info@website.co.uk", "Message via your website!",
      $message, "From: $email" );

答案 2 :(得分:0)

您不能只为mail()添加越来越多的变量。只有一个参数指定了电子邮件的内容,这将是第三个参数,您可以在official documentation中看到:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

如果您希望邮件中不只有$message,则需要将所有内容放入一个变量中,作为内容提供给函数。

$content = $message;
$content .= "\n" . $services;
// etc...
mail($to, $subject, $content);

答案 3 :(得分:-1)

试试这个

<?php
  $email = $_POST['email'] ;
  $name = $_POST['name'] ;
  $services = $_POST['services'] ;
  $message = $_POST['message'] ;

  if (!isset($_POST['email'])) {
    header( "Location: feedback.html" );
  }
  elseif (empty($email) || empty($message)) {
     header( "Location: error.html" );
 }
  else {
    mail( "info@website.co.uk", "Message via your website!",
           $name . $services. $message .  "From: $email" );
    header( "Location: thankyou.html" );
  }
?>