dongzhidian3538 2016-08-24 20:07
浏览 206
已采纳

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

I've tried searching the forums, I have found that some other people have been having the same issue as me, but haven't found a solution that works yet.

I am creating a portal where the customer enters their information in the form and uploads an image, which is then sent as an attachment to the email.

I am finding that when I use the PHP mail() function, it is sending duplicate emails, one with the POST data, and one without. I am only calling the function once, and as far as I can tell I am only loading the page once.

Here is my code:

//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.
";
$message .= "The order details are as follows:
";
$message .= "
";
$message .= "First Name: ".$_POST["firstname"]."
";
$message .= "Last Name: ".$_POST["lastname"]."
";
$message .= "Phone Number: ".$_POST["phonenumber"]."
";
$message .= "Email Address: ".$_POST["emailaddress"]."
";
$message .= "
";
$message .= "Phone: " . $_POST["phone"] . "
";
$message .= "Color: " . $_POST["color"] . "
";
$message .= "Voice Plan: " . $_POST["voiceplan"] . "
";
$message .= "Data Plan: " . $_POST["dataplan"] . "
";

//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
"; 
$headers .= "From: noreply@skycommunications.net
"; 
$headers .= "Reply-To: noreply@skycommunications.net
";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary

";

//plain text info
$body = "--$boundary
";
$body .= "Content-Type: text/plain; charset=ISO-8859-1
";
$body .= "Content-Transfer-Encoding: base64

"; 
$body .= chunk_split(base64_encode($message)); 

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

"; 
$body .= $encoded_content;  

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

Everything works beautifully except for the fact that it sends one email complete with the information and attachment, another with no information from POST and a 0kb attachment. Any ideas? Is it a problem with the server possibly?

  • 写回答

4条回答 默认 最新

  • duandou8120 2016-08-24 21:41
    关注

    Wrap all of the mail code some sort of validation logic. Overall you want to:

    1. Ensure that the request being made is in fact a POST request
    2. Ensure that the required POST parameters are included in the request.

    You can achieve that with code similar to the following:

    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
    }
    

    I would bet if you looked at your server log you would be getting some errors saying that the array key does not exist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败