php邮件是否需要配置才能运行?

时间:2009-11-14 22:17:23

标签: php email

我正在尝试在PHP中执行一个非常简单的邮件表单,但得到错误:

  

PHP注意:未定义的索引:在第10行的/var/www/html/sites/send_contact.php中,引用者: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";
}
?>

3 个答案:

答案 0 :(得分:4)

最简单的猜测是你在访问变量时遇到了错误。

而不是:

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

使用它:

$name2=$_POST["name2"];

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

顺便说一句,我强烈建议您使用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!
}

答案 1 :(得分: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"];

答案 2 :(得分:0)

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

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

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