doujiaci7976 2016-07-14 13:23
浏览 34
已采纳

php生成的电子邮件与csv附件 - 发送额外(不正确)的版本

I am a complete novice with php, so forgive me if this is something very obvious! I have tried to create an html form on my website which, when submitted, generates an email with a csv attachement which is sent to me with the relevant information.

I have been using code found online (some from this forum) to try to create a solution and seem to be vey close - however there is a problem somewhere. I am receiving 2 emails when submitted - the first is incorrect in that it is missing the senders' email address and the csv has no information - the second email is exactly right however.

This is the code I am using:

<?php

        $email=$_POST['email'];
        $customer=$_POST['customer'];
        $quantity=$_POST['quantity'];
        $prefix=$_POST['prefix'];
        $itemno=$_POST['itemno'];
        $format=$_POST['format'];



        $to = "me@myemail.com";


        $subject = "Form 01";

        //Message Body
        $text = "Form 01 attached";

        //The Attachment

         $cr = "
";
        $data .= "$email" . $cr;
        $data .= "$customer" . $cr;
        $data .= "$quantity" . $cr;
        $data .= "$prefix" . $cr;
        $data .= "$itemno" . $cr;
        $data .= "$format" . $cr;
        $fp = fopen('form01.csv','a');
        fwrite($fp,$data);
        fclose($fp);

        $attachments[] = Array(
           'data' => $data,
           'name' => 'form_01.csv',
           'type' => 'application/vnd.ms-excel'
        );


        //Generate a boundary string

        $semi_rand = md5(time());
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";


        //Add the headers for a file attachment


        $headers = "MIME-Version: 1.0
" .
                   "From: {$from}
" .
                   "Content-Type: multipart/mixed;
" .
                   " boundary=\"{$mime_boundary}\"";


        //Add a multipart boundary above the plain message


        $message = "This is a multi-part message in MIME format.

" .
                  "--{$mime_boundary}
" .
                  "Content-Type: text/html; charset=\"iso-8859-1\"
" .
                  "Content-Transfer-Encoding: 7bit

" .
                  $text . "

";


        //Add attachments

        foreach($attachments as $attachment){
           $data = chunk_split(base64_encode($attachment['data']));
           $name = $attachment['name'];
           $type = $attachment['type'];

           $message .= "--{$mime_boundary}
" .
                      "Content-Type: {$type};
" .
                      " name=\"{$name}\"
" .
                      "Content-Transfer-Encoding: base64

" .
                      $data . "

" ;
        }

        $message .= "--{$mime_boundary}--
";
        mail($to, $subject, $message, $headers);


        ?>

I am also trying to add in reCaptcha, this is going in fine except it only seems to be allowing the first email to get through - which is the incorrect one. So I hope if the problem is in the code above and it can be fixed, then this should be solved too.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongpao9437 2016-07-14 13:39
    关注

    The code you are giving us is (I guess) incomplete. It is (obviously) not the complete HTML page. I guess this PHP script is at the top of your HTML page right above your form. Am I right?

    So when you open the page your PHP script sends the first (empty) email. After the form is submitted and your $_POST array is filed with data the second (and correct) email will be sent.

    To fix this you must check if the $_POST array is not empty:

    <?php
    if (!empty($_POST["email"])) {
       //your code here
    }
    ?>
    

    Like this here: If isset $_POST

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程