PHP mail()函数发送重复(空)电子邮件

时间:2016-08-24 20:07:29

标签: php email duplicates

我试过在论坛上搜索,我发现其他人一直和我有同样的问题,但还没有找到一个有效的解决方案。

我正在创建一个门户网站,客户在表单中输入信息并上传图片,然后将图片作为附件发送到电子邮件中。

我发现当我使用PHP mail()函数时,它会发送重复的电子邮件,一个包含POST数据,另一个没有。我只调用一次函数,据我所知,我只加载了一次页面。

这是我的代码:

//recipient address (made up but you get the idea)
$to = 'sales@skycommunications.net';

//subject of email
$subject = 'Phone Order from Online Portal'; 

//create body of message
$message =  "An order has been placed using the Portal.\n";
$message .= "The order details are as follows:\n";
$message .= "\n";
$message .= "First Name: ".$_POST["firstname"]."\n";
$message .= "Last Name: ".$_POST["lastname"]."\n";
$message .= "Phone Number: ".$_POST["phonenumber"]."\n";
$message .= "Email Address: ".$_POST["emailaddress"]."\n";
$message .= "\n";
$message .= "Phone: " . $_POST["phone"] . "\n";
$message .= "Color: " . $_POST["color"] . "\n";
$message .= "Voice Plan: " . $_POST["voiceplan"] . "\n";
$message .= "Data Plan: " . $_POST["dataplan"] . "\n";

//get file details from previous form
$file_tmp_name    = $_FILES['uploaded_file']['tmp_name'];
$file_name        = $_FILES['uploaded_file']['name'];
$file_size        = $_FILES['uploaded_file']['size'];
$file_type        = $_FILES['uploaded_file']['type'];

//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));

//random number for headers
$boundary = md5("sanwebe"); 

//create the headers
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "From: noreply@skycommunications.net\r\n"; 
$headers .= "Reply-To: noreply@skycommunications.net\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

//plain text info
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
$body .= chunk_split(base64_encode($message)); 

//attachment info
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name='$file_name'\r\n";
$body .="Content-Disposition: attachment; filename='$file_name'\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
$body .= $encoded_content;  

//send the email
mail($to, $subject, $body, $headers);

除了发送一封包含信息和附件的电子邮件,另一封没有来自POST和0kb附件的信息的事实外,一切都很美妙。有任何想法吗?这可能是服务器的问题吗?

4 个答案:

答案 0 :(得分:1)

我的打赌是你用.htaccess或其他一些重定向重定向。它将使用post数据调用一次,并在重定向后再次调用。

一个简单的解决方法是前置:

if(!empty($_POST['someinput'])) {
//send email
}

答案 1 :(得分:0)

将所有邮件代码包装成某种验证逻辑。总的来说你想:

  1. 确保正在发出的请求实际上是一个POST请求
  2. 确保请求中包含所需的POST参数。
  3. 您可以使用类似于以下内容的代码实现此目的:

    function validRequest() {
        return (
            // make sure that the request type is POST
            $_SERVER['REQUEST_METHOD'] === 'POST'
            // make sure the required POST variables were included
            && isset($_POST['firstname'])
            && isset($_POST['lastname'])
            && isset($_POST['phonenumber'])
            && isset($_POST['emailaddress'])
            && isset($_POST['phone'])
            && isset($_POST['color'])
            && isset($_POST['voiceplan'])
            && isset($_POST['dataplan'])
            // make sure that there is a file
            && $_FILES['uploaded_file']
        );
    }
    
    if (validRequest()) {
        // your email code
    } else {
        // there was some sort of error
    }
    

    我敢打赌,如果你查看你的服务器日志,你会收到一些错误,说数组密钥不存在。

答案 2 :(得分:0)

如果您将此代码放在页面顶部,然后在初始页面加载时将其发送到sales@skycommunications.net。

当您实际提交表单时,将发送第二封电子邮件。

为了防止这种情况,您需要将其包装在IF条件中并检查$ _POST是否为空。这样脚本就不会在初始页面加载时执行。

最好的方法是检查提交值,您也可以验证其他帖子值。

假设您的提交按钮名称为"提交"。

if (isset($_POST['submit']) && !empty($_POST['submit'])){
//recipient address (made up but you get the idea)
$to = 'sales@skycommunications.net';

//subject of email
$subject = 'Phone Order from Online Portal'; 

//create body of message
$message =  "An order has been placed using the Portal.\n";
$message .= "The order details are as follows:\n";
$message .= "\n";
$message .= "First Name: ".$_POST["firstname"]."\n";
$message .= "Last Name: ".$_POST["lastname"]."\n";
$message .= "Phone Number: ".$_POST["phonenumber"]."\n";
$message .= "Email Address: ".$_POST["emailaddress"]."\n";
$message .= "\n";
$message .= "Phone: " . $_POST["phone"] . "\n";
$message .= "Color: " . $_POST["color"] . "\n";
$message .= "Voice Plan: " . $_POST["voiceplan"] . "\n";
$message .= "Data Plan: " . $_POST["dataplan"] . "\n";

//get file details from previous form
$file_tmp_name    = $_FILES['uploaded_file']['tmp_name'];
$file_name        = $_FILES['uploaded_file']['name'];
$file_size        = $_FILES['uploaded_file']['size'];
$file_type        = $_FILES['uploaded_file']['type'];

//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));

//random number for headers
$boundary = md5("sanwebe"); 

//create the headers
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "From: noreply@skycommunications.net\r\n"; 
$headers .= "Reply-To: noreply@skycommunications.net\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

//plain text info
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
$body .= chunk_split(base64_encode($message)); 

//attachment info
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name='$file_name'\r\n";
$body .="Content-Disposition: attachment; filename='$file_name'\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
$body .= $encoded_content;  

//send the email
mail($to, $subject, $body, $headers);
}

答案 3 :(得分:0)

尤里卡!感谢大家的帮助!

以下是我在调用mail()函数之前验证POST数据的代码:

//check for empty post so duplicate emails are not sent
if (
    isset($_POST["firstname"]) && 
    isset($_POST["lastname"]) && 
    isset($_POST["phonenumber"]) && 
    isset($_POST["emailaddress"]) &&
    isset($_POST['phone']) &&
    isset($_POST['color']) &&
    isset($_POST['voiceplan']) &&
    isset($_POST['dataplan']) &&
    isset($_FILES["uploaded_file"])
    )
{
    mail($to, $subject, $body, $headers);
}

我发现如果我使用了else语句,该页面无法完成加载,所以我只使用了if语句,并且只发送了一封包含POST数据的电子邮件。

再次感谢大家的帮助。