Php posta çalışmak yapılandırma ihtiyacı var mı?

3 Cevap php

PHP çok basit bir posta formu yapmak ama hatayı almaya çalışıyorum:

PHP Notice: Undefined index: / var / www / html / siteler / send_contact.php hattında 10, referer: http://example.com/contact2.php

Benim PHP dosyası gibi görünüyor:

<?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 Cevap

Kolay tahmin sizin değişkenleri erişen bir hata yapıyoruz olmasıdır.

yerine:

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

Bu kullanabilirsiniz:

$name2=$_POST["name2"];

Eğer farkı biliyor ve bilerek bu yapıyorsun ya, senin $name2 değişkeni HTML form alanının doğru adı ile tanımlanan emin olun.

As an aside, I would strongly recommend using a library like PHPMailer to send emails.
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.

Daha iyi bir fikir için, this example kendi web sitesinden kontrol edin:

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

Hata yanlış tam olarak ne olduğunu anlatıyor:

Satır 10:

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

Tanımlanmadan önce '$ ad2' kullanıyor.

PHP dizeleri değişkenleri yerine geçebilir:

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

HTML'nizdeki aşağıdaki benzer bir şey kullanın:

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

Sonra PHP, veri POSTed varsayarak, kullanarak erişmek istiyoruz:

$name2 = $_POST["name2"];

Bu beklediğiniz gibi kod $ _POST verileri geçmez gibi görünüyor. Ben aşağıdaki gibi $ _POST anahtarları kontrol etmek için tavsiye:

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

O $ isim2 değerini görüntülemek değilse, bu send_contact.php form verilerini gönderme web sayfasını değiştirmeniz gerektiği anlamına gelir.