PHP邮件功能发送了重复的电子邮件

2022-04-05 00:00:00 html-email php

我构建了一个带有reCaptcha验证的表单,系统会向我的设置发送一式两份的电子邮件,并收到来自Office的垃圾邮件。

我的错误是什么?

一切正常,除了在填写表格时,我们当时收到了2封电子邮件。

<?php
if (isset($_POST["submit"])) {

    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $privatekey = "******";

    $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']);
    $data = json_decode($response);

    if(isset($data->success) AND $data->success==true){

        $to = "whois@sent.this";
        $emailfrom = $_POST['email'];
        $email = $_POST['email'];
        $nom = $_POST['nom'];
        $phone = $_POST['phone'];
        $subject = "Formulaire de Contact Web  ";
        $commentaire = $_POST['commentaire'];

        $headers  = 'MIME-Version: 1.0' . "
";
        $headers .= 'Content-type: text/html; charset=UTF-8' . "
";
        $headers .= "Reply-to: $emailfrom";

        $message = "
         <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <title>Appel de service  (rempli sur le site internet)</title>
        <meta charset="UTF-8">
        <style type="text/css">
        h1,h2,h3,h4,h5,h6 {
            font-family: Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif;
            color: #272727;
        }
        </style>
        </head>
        <body>
        <h2 style="font-size: 1.25em; font-family: Gotham, ʼHelvetica Neueʼ, Helvetica, Arial, sans-serif;">Vous avez re&ccedil;u une demande de contact sur votre site internet. </h2>
        <br>
        <table width="500" border="1" cellpadding="5" cellspacing="2">
          <tbody>
            <tr>
              <td colspan="2" align="left" valign="middle" bgcolor="#84BDEC"><h3>Formulaire de contact Copiscan.com</h3></td>
            </tr>
            <tr>
              <td width="155" align="left" valign="middle" bgcolor="#D5D5D5">Nom</td>
              <td width="313" align="left" valign="middle">$nom</td>
            </tr>
            <tr>
              <td width="155" align="left" valign="middle" bgcolor="#D5D5D5">Téléphone</td>
              <td width="313" align="left" valign="middle">$phone</td>
            </tr>
            <tr>
              <td align="left" valign="middle" bgcolor="#D5D5D5">Adresse courriel</td>
              <td align="left" valign="middle">$email</td>
            </tr>
            <tr bgcolor="#D5D5D5">
              <td colspan="2" align="left" valign="middle">Commentaire :</td>
            </tr>
            <tr>
              <td height="75" colspan="2" align="left" valign="top">$commentaire</td>
            </tr>
          </tbody>
        </table>
        </body>
        </html>
         ";
// Always set content-type when sending HTML email
mail($to, $subject, $message, $headers); 

        // so on...all your mail parameters here


        if(mail($to, $subject, $message, $headers)){
                    header('Location: contactez-nous.php?CaptchaPass');

        }else{
            echo "mail could not be sent";
        }
    }else{
                header('Location: contactez-nous.php?CaptchaFail');
    }

}else{
    header('Location: contactez-nous.php?CaptchaError');

}

?> 

解决方案

双重函数调用mail

// Always set content-type when sending HTML email
mail($to, $subject, $message, $headers); 

        // so on...all your mail parameters here
        if(mail($to, $subject, $message, $headers)){

试试这个:

// Always set content-type when sending HTML email
$mailOk = mail($to, $subject, $message, $headers); 

        // so on...all your mail parameters here
        if($mailOk){

或者反过来:

// Always set content-type when sending HTML email
// Remove this line --> mail($to, $subject, $message, $headers); 

        // so on...all your mail parameters here
        if(mail($to, $subject, $message, $headers))){

相关文章