PHP - 无法从表单发送带附件的电子邮件

I having some trouble sending e-mail with attachment from the php MAIL function. The error checking is working but I can't get the file to upload and send in the e-mail. I'm using code I've pulled off the net. Just can't seem to get it working. Anyone have any thoughts? Any help would be appreciated. Thanks! URL to follow:

http://www.xsp.com/careers2.php

if(array_key_exists('submit_check', $_POST)) {
                        if($_POST['first_name'] != NULL && $_POST['last_name'] != NULL && $_POST['e-mail'] != NULL && $_POST['address'] != NULL && $_POST['city'] != NULL && $_POST['zipcode'] != NULL && $_POST['country'] != NULL && $_POST['telephone'] != NULL && $_POST['first_name'] != '' && $_POST['last_name'] != '' && $_POST['e-mail'] != '' && $_POST['address'] != '' && $_POST['city'] != '' && $_POST['zipcode'] != '' && $_POST['country'] != '' || 
        (($_FILES["file"]["type"] == "application/doc") || ($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/pdf")) && $_FILES["file"]["size"] < 100000) {                         
                            $first_name = preg_replace('/[^a-zA-Z0-9_]/s', '', $_POST['first_name']);
                            $last_name = preg_replace('/[^a-zA-Z0-9_]/s', '', $_POST['last_name']);
                            $city = preg_replace('/[^a-zA-Z0-9_]/s', '', $_POST['city']);
                            $state = preg_replace('/[^a-zA-Z0-9_]/s', '', $_POST['state']);
                            $zipcode = preg_replace('/[^a-zA-Z0-9_]/s', '', $_POST['zipcode']);
                            $email = $_POST['e-mail'];

                            // we'll begin by assigning the To address and message subject
                            $to = "myhiddenemailaddress@domain.com";

                            $subject = $form_title;

                           // get the sender's name and email address
                           // we'll just plug them a variable to be used later
                           //$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
                           $form = $email;

                           // generate a random string to be used as the boundary marker
                           $mime_boundary = "==Multipart_Boundary_x".md5(mt_rand())."x";

                           // store the file information to variables for easier access
                           $tmp_name = $_FILES['file']['tmp_name'];
                           $type = $_FILES['file']['type'];
                           $name = $_FILES['file']['name'];
                           $size = $_FILES['file']['size'];

                           // here we'll hard code a text message
                           // again, in reality, you'll normally get this from the form submission
                           $message = "Here is your file: $name";

                           /*echo $message."<br />";
                           echo "TMP NAME:".$tmp_name."<br />";
                           echo $_FILES['file'];*/

                           // if the upload succeded, the file will exist
                           if (file_exists($tmp_name)){

                              // check to make sure that it is an uploaded file and not a system file
                              if(is_uploaded_file($tmp_name)){

                                 // open the file for a binary read
                                 $file = fopen($tmp_name,'rb');

                                 // read the file content into a variable
                                 $data = fread($file,filesize($tmp_name));

                                 // close the file
                                 fclose($file);

                                // now we encode it and split it into acceptable length lines
                                $data = chunk_split(base64_encode($data));
                             }

                             // now we'll build the message headers
                              $headers = "From: $from
" .
                                 "MIME-Version: 1.0
" .
                                 "Content-Type: multipart/mixed;
" .
                                 " boundary=\"{$mime_boundary}\"";

                              // next, we'll build the message body
                              // note that we insert two dashes in front of the
                              // MIME boundary when we use it
                              $message = "This is a multi-part message in MIME format.

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

" .
                                 $message . "

";
                                $message .="Application Form 
";
                                $message .="First Name: ".$first_name."
";
                                $message .="Last Name: ".$last_name."
";
                                $message .="E-Mail: ".$email."
";

                                if ($_POST["company"] != NULL && $_POST["company"] != "") {
                                    $company = $_POST["company"];
                                    $message .= "COMPANY: ".$company."
";
                                }

                                if ($_POST["position"] != NULL && $_POST["position"] != "") {
                                    $position = $_POST["position"];
                                    $message .= "POSITION: ".$position."
";
                                }

                                if ($_POST["address"] != NULL && $_POST["address"] != "") {
                                    $address = $_POST["address"];
                                    $message .= "ADDRESS: ".$address."
";
                                }

                                if ($city != NULL && $city != "") {
                                    $message .= "CITY: ".$city."
";
                                }

                                if ($state != NULL && $state != "") {
                                    $message .= "STATE: ".$state."
";
                                }

                                if ($zipcode != NULL && $zipcode != "") {
                                    $message .= "ZIPCODE: ".$zipcode."
";
                                }

                                if ($_POST["telephone"] != NULL && $_POST["telephone"] != "") {
                                    $telephone = $_POST["telephone"];
                                    $message .= "TELEPHONE: ".$telephone."
";
                                }

                                if ($_POST["website"] != NULL && $_POST["website"] != "") {
                                    $website = $_POST["website"];
                                    $message .= "WEBSITE: ".$website."

";
                                }

                                if ($_POST["message"] != NULL && $_POST["message"] != "") {
                                    $message_text = $_POST["message"];
                                    $message .= "MESSAGE TEXT: ".$message_text."

";
                                }


                              // now we'll insert a boundary to indicate we're starting the attachment
                              // we have to specify the content type, file name, and disposition as
                              // an attachment, then add the file content and set another boundary to
                              // indicate that the end of the file has been reached
                              $message .= "--{$mime_boundary}
" .
                                 "Content-Type: {$type};
" .
                                 " name=\"{$name}\"
" .
                                 //"Content-Disposition: attachment;
" .
                                 //" filename=\"{$fileatt_name}\"
" .
                                 "Content-Transfer-Encoding: base64

" .
                                 $data . "

" .
                                 "--{$mime_boundary}--
";

                              // now we just send the message
                              if (@mail($to, $subject, $message, $headers))
                                 echo "Message Sent";
                              else
                                 echo "Failed to send";
                           }
                        } else {
                            if ($file > 0) {
                                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
                            }

                            echo '<p class="note" style="margin:5px 0 5px 0; color:#ff0000;">Please fill in all the required fields</p>';
                        }
                        //echo '<p class="note" style="margin:5px 0 0 0;">First Name: '.$first_name.'</p>';
                    } ?>

2个回答



我使用PHPMAILER,它易于使用,有很多例子(包括附加文件的方法)
pagina de phpmailer </ p>
</ div>

展开原文

原文

I use PHPMAILER, its easy to use, a had a lot of examples (included a method to attach files) pagina de phpmailer

dongpi3237
dongpi3237 这只是一个图书馆,而不是一个框架
8 年多之前 回复
dqwn64004
dqwn64004 我有太多的专有代码可悲地将它用于这个框架。
8 年多之前 回复




  • 您需要删除
    并用
    替换它们。 大多数邮件服务器不再接受 </ li>
  • 您需要MIME的随机哈希分隔符(例如,参见下面的代码)</ p>


    $ filename =“MyAttachment.pdf”; </ p>

    $ attachment = chunk_split(base64_encode($ data)); </ p>

    $ separator = md5(time( )); </ p>

    //回车类型(我们使用PHP行结束常量)</ p>

    $ eol = PHP_EOL; </ p> \ n

    //主标题</ p>

    $ headers =“From:”。$ from。$ eol; </ p>

    $ headers。=
    “MIME-Version:1.0”。$ eol; $ headers。=“Content-Type:multipart / mixed;
    boundary = \”“。$ separator。”\“”; </ p>

    //此后不再有标题,我们开始 身体! // </ p>

    $ body =“ - ”。$ separator。$ eol; </ p>

    $ body。=“Content-Transfer-Encoding:
    7bit”。$ eol。$ eol; </ p>

    $ body。=“这是一个MIME编码的消息。”。$ eol; </ p>

    // message $ body。=“ - ” 。$分隔符$ EOL; </ p>

    $ body。=“Content-Type:
    text / html; charset = \”iso-8859-1 \“”。$ eol; </ p>

    $ body。=
    “Content-Transfer-Encoding:8bit”。$ eol。$ eol; </ p>

    $ body。= $ message。$ eol; </ p>

    // attachment </ p>

    $ body。= 。 “ - ” 分隔符$ $ EOL; </ p>

    $ body。=“Content-Type:
    application / octet-stream; name = \”“。$ filename。”\“”。$ eol; </ p>

    $ body。=
    “Content-Transfer-Encoding:base64”。$ eol; </ p>

    $ body。=
    “Content-Disposition:attachment”。$ eol。$ eol; </ p>

    $ body。=
    $ attachment。$ eol; </ p>

    $ body。=“ - ”。$ separator。“ - ”; </ p>

    //发送消息</ p>
    \ n

    邮件($ to,$ subject,$ body,$ headers); </ p>
    </ blockquote> </ li>
    </ ul>

    或者你可以 如上所述从PHPmailer下载3个类文件,因为它已经完成了所有这些。</ p>
    </ div>

展开原文

原文

  • You need to remove the 's and replace them with 's. Most mailservers no longer accept
  • You need random hash separators for the MIME (see below code for example)

    $filename = "MyAttachment.pdf";

    $attachment = chunk_split(base64_encode($data));

    $separator = md5(time());

    // carriage return type (we use a PHP end of line constant)

    $eol =PHP_EOL;

    // main header

    $headers = "From: ".$from.$eol;

    $headers .= "MIME-Version: 1.0".$eol; $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

    // no more headers after this, we start the body! //

    $body = "--".$separator.$eol;

    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;

    $body .= "This is a MIME encoded message.".$eol;

    // message $body .= "--".$separator.$eol;

    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;

    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;

    $body .= $message.$eol;

    // attachment

    $body .= "--".$separator.$eol;

    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;

    $body .= "Content-Transfer-Encoding: base64".$eol;

    $body .= "Content-Disposition: attachment".$eol.$eol;

    $body .= $attachment.$eol;

    $body .= "--".$separator."--";

    // send message

    mail($to, $subject, $body, $headers);

or you can just download the 3 class files from PHPmailer as suggested above since it has all of this already finished for you.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐