gpt4 book ai didi

php - 回复发件人 - PHP 电子邮件

转载 作者:可可西里 更新时间:2023-10-31 23:54:14 25 4
gpt4 key购买 nike

这是我的电子邮件表单代码。它运作良好,它发送到我的电子邮件。但是我怎样才能回复从表单收到的电子邮件呢?您能否编辑我的代码并将其放入,因为我是一个大 php 新手。非常感谢!

<?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');

?>

最佳答案

你需要设置headers才能通过sender email:

效果:

<?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);

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';

关于php - 回复发件人 - PHP 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17418751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com