0

我在从 php MAIL 函数发送带有附件的电子邮件时遇到了一些问题。错误检查正常,但我无法上传文件并通过电子邮件发送。我正在使用我从网上下载的代码。只是似乎无法让它工作。有人有什么想法吗?任何帮助,将不胜感激。谢谢!要关注的网址:

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\r\n" .
                                 "MIME-Version: 1.0\r\n" .
                                 "Content-Type: multipart/mixed;\r\n" .
                                 " 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.\n\n" .
                                 "--{$mime_boundary}\n" .
                                 "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
                                 "Content-Transfer-Encoding: 7bit\n\n" .
                                 $message . "\n\n";
                                $message .="Application Form \n";
                                $message .="First Name: ".$first_name."\n";
                                $message .="Last Name: ".$last_name."\n";
                                $message .="E-Mail: ".$email."\n";

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

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

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

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

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

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

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

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

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


                              // 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}\n" .
                                 "Content-Type: {$type};\n" .
                                 " name=\"{$name}\"\n" .
                                 //"Content-Disposition: attachment;\n" .
                                 //" filename=\"{$fileatt_name}\"\n" .
                                 "Content-Transfer-Encoding: base64\n\n" .
                                 $data . "\n\n" .
                                 "--{$mime_boundary}--\n";

                              // 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>';
                    } ?>
4

2 回答 2

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

    $filename = "我的附件.pdf";

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

    $分隔符 = md5(time());

    // 回车类型(我们使用 PHP 行尾常量)

    $eol =PHP_EOL;

    // 主标题

    $headers = "发件人:".$from.$eol;

    $headers .= "MIME 版本:1.0".$eol; $headers .= "Content-Type: multipart/mixed; 边界=\"".$separator."\"";

    // 在此之后不再有标题,我们开始正文!//

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

    $body .= "内容传输编码:7bit".$eol.$eol;

    $body .= "这是一个 MIME 编码的消息。".$eol;

    // 消息 $body .= "--".$separator.$eol;

    $body .= "内容类型: text/html; charset=\"iso-8859-1\"".$eol;

    $body .= "内容传输编码:8bit".$eol.$eol;

    $body .= $message.$eol;

    // 附件

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

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

    $body .= "内容传输编码:base64".$eol;

    $body .= "内容配置:附件".$eol.$eol;

    $body .= $attachment.$eol;

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

    // 发信息

    邮件($to,$subject,$body,$headers);

或者您可以按照上面的建议从 PHPmailer 下载 3 个类文件,因为它已经为您完成了所有这些。

于 2012-07-29T23:51:12.603 回答
0

我用的是PHPMAILER,它好用,有很多例子(包括一个附加文件的方法) pagina de phpmailer

于 2012-04-19T15:43:32.040 回答