回复发件人 - PHP 电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17418751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reply to sender - PHP email
提问by Adam G
Here is my code for a email form. It works well, it sends to my email. But how can i make it so i can reply to the email that i received from the form? Would you be able to edit my code and put it in because im a BIG php noobie. many thanks!
这是我的电子邮件表单代码。它运行良好,它发送到我的电子邮件。但是我怎样才能做到这样我就可以回复我从表单中收到的电子邮件?您能否编辑我的代码并将其放入,因为我是个大 php noobie。非常感谢!
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "[email protected]";
mail ($to, $subject, $message, "From: " . $name);
header('Location: contact_thankyou.html');
?>
回答by jycr753
you need to set the headers
to be able to pass the sender email:
您需要设置headers
能够传递发件人电子邮件:
fx:
外汇:
<?php
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
so your code will look something like this:
所以你的代码看起来像这样:
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "[email protected]";
$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');
Note: I never tested myself, I normally use smtp.mail class to do all this for me, since it is more easy, clean... just check it out...
注意:我从来没有测试过自己,我通常使用 smtp.mail 类来为我做这一切,因为它更容易、更干净......只需检查一下......
then it will look something like this:
然后它看起来像这样:
<?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 = '[email protected]';
$mail->FromName = 'Mailer';
$mail->AddAddress('[email protected]', 'Josh Adams'); // Add a recipient
$mail->AddAddress('[email protected]'); // Name is optional
$mail->AddReplyTo('[email protected]', 'Information');
$mail->AddCC('[email protected]');
$mail->AddBCC('[email protected]');
$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';