通过一个PHP页面发送两封电子邮件,

时间:2018-08-31 20:06:01

标签: php html

有与此类似的问题,但它们未概述我的案情Similar Question (Link)

<form method="post" name="process.php" action="process.php">

Name:   <input type="text" name="name">

Email Address:  <input type="text" name="email">

Message:    <textarea name="message"></textarea>

<input type="submit" value="Send Form">
</form>

此文件另存为index.php在我的bookings文件夹中。

我已经创建了这段代码来处理表单提交,这确实有效。

<?php
  $name = $_POST['name'];
  $visitor_email = $_POST['email'];
  $message = $_POST['message'];
?>

<?php
    $email_from = $visitor_email;

    $email_subject = "New Form submission";

    $email_body = "You have received a new message from the user $name.\n".
                            "Here is the message:\n $message".



  $to = "bookings@mycompany.com";

  $headers = "From: $email_from \r\n";

  $headers .= "Reply-To: $visitor_email \r\n";

  mail($to,$email_subject,$email_body,$headers);


 ?>

<?php
 $to = "user@emailprovider.com";

  $headers = "From: 'bookings@mycompany.com' \r\n";

  $headers .= "Reply-To: 'bookings@mycompany.com' \r\n";

  mail($to,$email_subject,$email_body,$headers);
  $email_body = "Hi, we have received your booking request, someone from the team will get back to you shortly"
?>

 <p>Sending Booking Request....</p>

我认为这与重叠变量有关,但是我希望能够向用户发送电子邮件确认已收到他们的请求。目前,用户确实收到了电子邮件,但其内容与发送给bookings@mycompany.com

的电子邮件相同

预先感谢:)

2 个答案:

答案 0 :(得分:1)

要先发送电子邮件,然后再更改电子邮件正文。更改为:

<?php
 $to = "user@emailprovider.com";

  $headers = "From: 'bookings@mycompany.com' \r\n";

  $headers .= "Reply-To: 'bookings@mycompany.com' \r\n";

  $email_body = "Hi, we have received your booking request, someone from the team will get back to you shortly";

  mail($to,$email_subject,$email_body,$headers);

?>

答案 1 :(得分:0)

问题:$email_body在邮件发送后分配。

替换

mail($to,$email_subject,$email_body,$headers);
$email_body = "Hi, we have received your booking request, someone from the team will get back to you shortly"

$email_body = "Hi, we have received your booking request, someone from the team will get back to you shortly"
mail($to,$email_subject,$email_body,$headers);