Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML电子邮件通过PHP发送到两个电子邮件地址,其中一个只接收HTML代码_Php_Html_Email - Fatal编程技术网

HTML电子邮件通过PHP发送到两个电子邮件地址,其中一个只接收HTML代码

HTML电子邮件通过PHP发送到两个电子邮件地址,其中一个只接收HTML代码,php,html,email,Php,Html,Email,我有一个通过PHP发送HTML格式电子邮件的联系人表单。填写此表格后,电子邮件将发送到我的电子邮件地址,并将稍作修改的版本发送给填写表格的客户 这很好,但是我收到了预期的电子邮件,但是客户只收到了原始HTML代码,而不是格式正确的电子邮件 我已将我的代码包含在下面,但出于隐私原因替换了我的电子邮件地址 PHP代码: <?php // Get the form fields, removes html tags and whitespace. $first_name = s

我有一个通过PHP发送HTML格式电子邮件的联系人表单。填写此表格后,电子邮件将发送到我的电子邮件地址,并将稍作修改的版本发送给填写表格的客户

这很好,但是我收到了预期的电子邮件,但是客户只收到了原始HTML代码,而不是格式正确的电子邮件

我已将我的代码包含在下面,但出于隐私原因替换了我的电子邮件地址

PHP代码:

<?php

    // Get the form fields, removes html tags and whitespace.
    $first_name = strip_tags(trim($_POST["first_name"]));
    $first_name = str_replace(array("\r","\n"),array(" "," "),$first_name);
    $last_name = strip_tags(trim($_POST["last_name"]));
    $last_name = str_replace(array("\r","\n"),array(" "," "),$last_name);
    $company = strip_tags(trim($_POST["company"]));
    $company = str_replace(array("\r","\n"),array(" "," "),$company);
    $phone = strip_tags(trim($_POST["phone"]));
    $phone = str_replace(array("\r","\n"),array(" "," "),$phone);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $checklist = trim($_POST["checklist"]);
    $details = trim($_POST["details"]);

    // Check the data.
    if (empty($first_name) OR empty($last_name) OR empty($details) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        header("Location: http://www.EXAMPLE.com/index-test.html");
        exit;
    }

    // Set the recipient email address. Update this to YOUR desired email address.
    $to = "contact@EXAMPLE.com";
    $noreply = "noreply@EXAMPLE.com";

    // Set the email subject.
    $subject = "New contact from $first_name $last_name of $company";
    $subject2 = "Thank you for contacting EXAMPLE!";

    $message = "
    <html>
        <head>
        </head>
        <body style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
            <table style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
                <tr>
                    <td><b>Name:</b></td>
                    <td>$first_name $last_name</td>
                </tr>
                <tr>
                    <td><b>Company Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td>
                    <td>$company</td>
                </tr>
                <tr>
                    <td><b>Phone:</b></td>
                    <td>$phone</td>
                </tr>
                <tr>
                    <td><b>Email:</b></td>
                    <td>$email</td>
                </tr>
                <tr>
                    <td><b>Services:</b></td>
                    <td>$checklist</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td><b>Details of Job:</b><br>$details</td>
                </tr>
            </table>
        </body>
    </html>
    ";

    $message2 = "
    <html>
        <head>
        </head>
        <body style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
            <p>Hi $first_name,<br><br>Thank you for contacting me. Once I have reviewed your job request, I will be in contact to discuss the project further.<br><br>You will find a copy of your message below. Please do not reply to this email as your reply will be sent to an unmonitored email address.</p>
            <table style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
                <tr>
                    <td><b>Name:</b></td>
                    <td>$first_name $last_name</td>
                </tr>
                <tr>
                    <td><b>Company Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td>
                    <td>$company</td>
                </tr>
                <tr>
                    <td><b>Phone:</b></td>
                    <td>$phone</td>
                </tr>
                <tr>
                    <td><b>Email:</b></td>
                    <td>$email</td>
                </tr>
                <tr>
                    <td><b>Services:</b></td>
                    <td>$checklist</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td><b>Details of Job:</b><br>$details</td>
                </tr>
            </table>
            <br>
            <br>
            <span style=\"color: #3a3a3c;\"><b>EXAMPLE NAME</b></span>
            <br>
            <span style=\"font-size: 13px; color: #F04E4C;\">Freelance Cinematography &amp; Post-Production</span>
            <br>
            <br>
            <span style=\"font-size: 13px; color: #F04E4C;\">P</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>000 000 0000</b>&nbsp;&nbsp;</span>
            <span style=\"font-size: 13px; color: #F04E4C;\">E</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>contact@EXAMPLE.com</b>&nbsp;&nbsp;</span>
            <span style=\"font-size: 13px; color: #F04E4C;\">W</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>www.EXAMPLE.com</b></span>
            <br>
            <br>
            <img alt=\"EXAMPLE Logo\" src=\"http://EXAMPLE.com/resources/img/logos/logo-black-orange-cropped.png\">
            <br>
            <div>&nbsp;</div>
        </body>
    </html>
    ";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers2 .= "MIME-Version: 1.0" . "\r\n";
$headers2 .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= "From: $first_name $last_name <$email>";
$headers2 = "From: EXAMPLE <$noreply>";


mail($to,$subject,$message,$headers);
mail($email,$subject2,$message2,$headers2);



?>

我认为问题在于您定义
$headers2
的顺序-这是不正确的-因为您最初设置了
$headers2.=
,这意味着附加,然后您可以通过显式地再次设置它而不进行连接来覆盖它

<?php

    // Get the form fields, removes html tags and whitespace.
    $first_name = strip_tags(trim($_POST["first_name"]));
    $first_name = str_replace(array("\r","\n"),array(" "," "),$first_name);
    $last_name = strip_tags(trim($_POST["last_name"]));
    $last_name = str_replace(array("\r","\n"),array(" "," "),$last_name);
    $company = strip_tags(trim($_POST["company"]));
    $company = str_replace(array("\r","\n"),array(" "," "),$company);
    $phone = strip_tags(trim($_POST["phone"]));
    $phone = str_replace(array("\r","\n"),array(" "," "),$phone);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $checklist = trim($_POST["checklist"]);
    $details = trim($_POST["details"]);

    // Check the data.
    if (empty($first_name) OR empty($last_name) OR empty($details) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        header("Location: http://www.EXAMPLE.com/index-test.html");
        exit;
    }

    // Set the recipient email address. Update this to YOUR desired email address.
    $to = "contact@EXAMPLE.com";
    $noreply = "noreply@EXAMPLE.com";

    // Set the email subject.
    $subject = "New contact from $first_name $last_name of $company";
    $subject2 = "Thank you for contacting EXAMPLE!";

    $message = "
    <html>
        <head>
        </head>
        <body style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
            <table style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
                <tr>
                    <td><b>Name:</b></td>
                    <td>$first_name $last_name</td>
                </tr>
                <tr>
                    <td><b>Company Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td>
                    <td>$company</td>
                </tr>
                <tr>
                    <td><b>Phone:</b></td>
                    <td>$phone</td>
                </tr>
                <tr>
                    <td><b>Email:</b></td>
                    <td>$email</td>
                </tr>
                <tr>
                    <td><b>Services:</b></td>
                    <td>$checklist</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td><b>Details of Job:</b><br>$details</td>
                </tr>
            </table>
        </body>
    </html>
    ";

    $message2 = "
    <html>
        <head>
        </head>
        <body style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
            <p>Hi $first_name,<br><br>Thank you for contacting me. Once I have reviewed your job request, I will be in contact to discuss the project further.<br><br>You will find a copy of your message below. Please do not reply to this email as your reply will be sent to an unmonitored email address.</p>
            <table style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
                <tr>
                    <td><b>Name:</b></td>
                    <td>$first_name $last_name</td>
                </tr>
                <tr>
                    <td><b>Company Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td>
                    <td>$company</td>
                </tr>
                <tr>
                    <td><b>Phone:</b></td>
                    <td>$phone</td>
                </tr>
                <tr>
                    <td><b>Email:</b></td>
                    <td>$email</td>
                </tr>
                <tr>
                    <td><b>Services:</b></td>
                    <td>$checklist</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td><b>Details of Job:</b><br>$details</td>
                </tr>
            </table>
            <br>
            <br>
            <span style=\"color: #3a3a3c;\"><b>EXAMPLE NAME</b></span>
            <br>
            <span style=\"font-size: 13px; color: #F04E4C;\">Freelance Cinematography &amp; Post-Production</span>
            <br>
            <br>
            <span style=\"font-size: 13px; color: #F04E4C;\">P</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>000 000 0000</b>&nbsp;&nbsp;</span>
            <span style=\"font-size: 13px; color: #F04E4C;\">E</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>contact@EXAMPLE.com</b>&nbsp;&nbsp;</span>
            <span style=\"font-size: 13px; color: #F04E4C;\">W</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>www.EXAMPLE.com</b></span>
            <br>
            <br>
            <img alt=\"EXAMPLE Logo\" src=\"http://EXAMPLE.com/resources/img/logos/logo-black-orange-cropped.png\">
            <br>
            <div>&nbsp;</div>
        </body>
    </html>
    ";

    // Always set content-type when sending HTML email
    /*
        Set the initial value
        and then append additional headers to the string
    */
    $headers = "From: $first_name $last_name <$email>\r\n";
    $headers .= "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";


    $headers2 = "From: EXAMPLE <$noreply>\r\n";
    $headers2 .= "MIME-Version: 1.0" . "\r\n";
    $headers2 .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    mail($to,$subject,$message,$headers);
    mail($email,$subject2,$message2,$headers2);
?>
很高兴你把它整理好了!
$headers_1=array();
$headers_2=array();

$headers_1[]="From: $first_name $last_name <$email>";
$headers_1[]="MIME-Version: 1.0";
$headers_1[]="Content-Type: text/html;charset=UTF-8";

$headers_2[]="From: EXAMPLE <$noreply>";
$headers_2[]="MIME-Version: 1.0";
$headers_2[]="Content-Type: text/html;charset=UTF-8";

mail( $to, $subject, $message, implode( "\r\n",$headers_1 ) );
mail($email,$subject2,$message2,implode( "\r\n",$headers_2 ));