简体   繁体   English

php邮件需要配置才能工作吗?

[英]Does php mail need configuration to work?

I'm trying to do a very simple mail form in PHP but get the error: 我正在尝试在PHP中做一个非常简单的邮件表单,但收到错误消息:

PHP Notice: Undefined index: in /var/www/html/sites/send_contact.php on line 10, referer: http://example.com/contact2.php PHP注意:未定义的索引:在第10行的/var/www/html/sites/send_contact.php中,引荐网址: http : //example.com/contact2.php

My PHP file looks like this: 我的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";
}
?>

The easiest guess is that you're doing a mistake accessing your variables. 最简单的猜测是您在访问变量时犯了一个错误。

instead of: 代替:

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

use this: 用这个:

$name2=$_POST["name2"];

Or, if you know the difference and are doing this on purpose, make sure your $name2 variable is defined with the correct name of the HTML form field. 或者,如果您知道差异并有意这样做,请确保使用正确的HTML表单字段名称定义$name2变量。

As an aside, I would strongly recommend using a library like PHPMailer to send emails. 顺便说一句,我强烈建议使用类似PHPMailer的库来发送电子邮件。
Your example is quite simple and the mail() should work just fine, but for anything more elaborate (ie. having attachments or html) or needing to send using an external mail server by SMTP (with or without authentication), it will do a much better job and save you lots of time. 您的示例非常简单,mail()应该可以正常工作,但是对于更复杂的内容(例如,具有附件或html)或需要通过SMTP使用外部邮件服务器发送邮件(带有或不带有身份验证),它将执行更好的工作,并节省大量时间。

For a better idea, check this example from their website: 要获得更好的主意,请在其网站上查看以下示例

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!
}

The error is telling you exactly what is wrong: 该错误告诉您确切的问题是:

Line 10: 第10行:

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

You are using '$name2' before it is defined. 在定义之前,您正在使用“ $ name2”。

PHP can substitute variables in strings: PHP可以替换字符串中的变量:

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

In your HTML use something similar to the following: 在HTML中使用类似于以下内容的内容:

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

Then in PHP, assuming the data was POSTed, you would access it using: 然后在PHP中,假设数据已过帐,则可以使用以下命令进行访问:

$name2 = $_POST["name2"];

It seems that your code doesn't pass the $_POST data as you expect. 您的代码似乎没有按预期传递$ _POST数据。 I recommend you to check the keys of $_POST as follows: 我建议您按以下方式检查$ _POST的键:

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

If it doesn't display the value of $name2, it means you should modify the web page sending form data to send_contact.php. 如果未显示$ name2的值,则意味着您应该修改将表单数据发送到send_contact.php的网页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM