【问题标题】:Does php mail need configuration to work?php邮件需要配置才能工作吗?
【发布时间】:2010-12-16 16:28:36
【问题描述】:

我正在尝试在 PHP 中创建一个非常简单的邮件表单,但出现错误:

PHP公告:未定义索引:在/var/www/html/sites/send_contact.php第10行,referer:http://example.com/contact2.php

我的 PHP 文件如下所示:

<?php // send_contact.php
// Contact subject
$subject =$_POST["$subject"]; 
// Details
$message=$_POST["$detail"];

// Mail of sender
$mail_from=$_POST["$customer_mail"]; 
// From 
$name2=$_POST["$name2"];

$header="From: $mail_from\r\n";

// Enter your email address
$to="joe@mail.com";

$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email 
// display message "We've recived your information"
if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}
?>

【问题讨论】:

  • 如果您的字段被命名为主题,那么您将输入 $_POST["subject"] 。如果您希望 $_POST 中的值具有存储在 $subject 中的键,请使用 $_POST[$subject] 。
  • 以防万一:一旦这个问题解决了,一定要在投入生产之前做一些输入清理...phpbuilder.com/columns/ian_gilfillan20060412.php3

标签: php email


【解决方案1】:

错误告诉你到底哪里出了问题:

第 10 行:

$name2=$_POST["$name2"];

您在定义之前使用“$name2”。

PHP 可以替换字符串中的变量:

$var1 = "bar";
echo "foo $var1"; // prints "foo bar"

在您的 HTML 中使用类似于以下内容的内容:

<input type="...whatever..." name="name2" />

然后在 PHP 中,假设数据已发布,您将使用以下方式访问它:

$name2 = $_POST["name2"];

【讨论】:

  • 这正是我所做的,我的 $name2 = $_POST["name2"];声明和你放的完全一样。
  • 在这种情况下,您的 HTML 没有任何名为“name2”的字段,或者,至少,它没有与您的表单一起发布。否则 POST 超全局将有一个 name2 键,你不会得到任何错误。你的页面是活的吗?一个链接会很有帮助。
  • 也许我的系统上没有设置 smtp 服务器?
  • 乔,我的和你的不同的是,我的版本在'$_POST[]'中的'name2'前面没有'$'。我上面的帖子解释了为什么这在某种程度上不起作用。你的问题不是邮件服务器 - 我再说一遍 - 错误清楚地指出了这种情况下的问题。
【解决方案2】:

最简单的猜测是您在访问变量时出错了。

代替:

$name2=$_POST["$name2"];

使用这个:

$name2=$_POST["name2"];

或者,如果您知道其中的区别并且故意这样做,请确保您的 $name2 变量使用正确的 HTML 表单字段名称定义。

顺便说一句,我强烈建议使用像 PHPMailer 这样的库来发送电子邮件。
您的示例非常简单,并且 mail() 应该可以正常工作,但是对于更复杂的内容(即具有附件或 html)或需要通过 SMTP 使用外部邮件服务器发送(有或没有身份验证),它将执行更好的工作并为您节省大量时间。

如需更好的主意,请查看他们网站上的this example

require_once('../class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on     errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

try {
  $mail->Host       = "mail.yourdomain.com"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
  $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "yourname@yourdomain"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';     // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

【讨论】:

    【解决方案3】:

    您的代码似乎没有按预期传递 $_POST 数据。我建议你检查 $_POST 的键如下:

    <?php
    print_r(array_keys($_POST));
    ?>
    

    如果没有显示$name2的值,说明你应该修改网页发送表单数据到send_contact.php。

    【讨论】:

      猜你喜欢
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2012-02-03
      相关资源
      最近更新 更多