PHP发送表单数据&两个不同的电子邮件

时间:2018-05-28 09:49:23

标签: php forms email post

  • PHP版本:5.3.3
  • PHPMailer版本:6.0.5
  • CentOS版本:6.9

我使用下面的脚本将表单数据发送到特定的电子邮件地址。它按预期工作。

    // Only process POST reqeusts.
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Get the form fields and remove whitespace.
        $name = strip_tags(trim($_POST["name"]));
        $phone = trim($_POST["phone"]);
        $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);

        // Check that data was sent to the mailer.
        if ( empty($name) OR empty($phone)) {
            // Set a 400 (bad request) response code and exit.
            http_response_code(400);
            echo "Oops! There was a problem with your submission. Please complete the form and try again.";
            exit;
        }

        // Set the recipient email address.
        $recipient = "email@domain.com";

        // Set the email subject.
        $emailsubject = "New contact from $name";

        // Build the email content.
        $email_content = "Name: $name\n";
        $email_content .= "Email: $email\n\n";
        $email_content .= "Phone:\n$phone\n";

        // Build the email headers.
        $email_headers = "From: $name <$email>";

        // Send the email.
        if (mail($recipient, $emailsubject, $email_content, $email_headers)) {
            // Set a 200 (okay) response code.
            http_response_code(200);
            echo 'Thank You! Your message has been sent.';
        } else {
            // Set a 500 (internal server error) response code.
            http_response_code(500);
            echo "Oops! Something went wrong and we couldn't send your message.";
        }

    } else {
        // Not a POST request, set a 403 (forbidden) response code.
        http_response_code(403);
        echo "There was a problem with your submission, please try again.";
    }

我现在想要发送一封单独​​的电子邮件到发件人的电子邮件$email),并使用不同的电子邮件正文 - 类似于

  

感谢您的提交。这些是您提交的详细信息......

感谢任何建议。

注意:我知道我的PHP版本不安全

1 个答案:

答案 0 :(得分:0)

为此使用简单的if语句。您所做的是在成功发送初始邮件后将邮件发送到自定义发件人地址。

<?php

//verify that email is valid
if ($email === true) {
    if (mail($recipient, $emailsubject, $email_content, $email_headers) === true) {
        //start processing mail for custom sender email
        //custom content
        $customContent = '';
        $customContent .= 'Thank you for your submission. These are the details you submitted..';
        if (mail($_POST['email'], 'Custom-Subject', $customContent, $email_headers) === true) {
            // Set a 200 (okay) response code.
            http_response_code(200);
            echo 'Thank You! Your message has been sent.';
        }
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }
} else {
   echo 'Your email address is invalid';
   die;
}

另请注意$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);$email是一个布尔值(true / false)。 $_POST['email'];包含实际的电子邮件地址