这是我的电子邮件表单代码。它运作良好,它发送到我的电子邮件。但我怎么能这样做,所以我可以回复我从表格收到的电子邮件?你能编辑我的代码并把它放进去,因为我是一个很大的php noobie。非常感谢!
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "adamgoredesign@gmail.com";
mail ($to, $subject, $message, "From: " . $name);
header('Location: contact_thankyou.html');
?>
答案 0 :(得分:17)
您需要将headers
设置为能够通过sender email:
FX:
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
所以你的代码看起来像这样:
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "adamgoredesign@gmail.com";
$headers = 'From: '.$email."\r\n" .
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers, "From: " . $name);
header('Location: contact_thankyou.html');
注意:我从未测试过自己,我通常使用smtp.mail类为我做这一切,因为它更简单,干净... just check it out...
那么它看起来像这样:
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'jswan'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->AddAddress('josh@example.net', 'Josh Adams'); // Add a recipient
$mail->AddAddress('ellen@example.com'); // Name is optional
$mail->AddReplyTo('info@example.com', 'Information');
$mail->AddCC('cc@example.com');
$mail->AddBCC('bcc@example.com');
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';