如何使用PHP在电子邮件中发送附件文件

时间:2014-06-27 10:50:36

标签: php

这是我使用PHP发送电子邮件的方法。我希望附加我的文件类型,如.doc,.docx,.pdf。

            //Send Mail//
            $file_name = $_FILES['cv']['name'];
            $temp_name = "../abc/cv/" . $_FILES["cv"]["name"];
            $file_type = $_FILES['cv']['type'];


            $message1 = "Name : $name \n\n";
            $message1.= "Current Designation : $designation \n\n";
            $message1.= "Current Company : $current_company \n\n";
            $message1.= "Email : $email \n\n";
            $message1.= "Tel. : $tel \n\n";
            $message1.= "Preferred Contact Time : $contact_time \n\n";
            $message1.= "Apply For Position : $position \n\n";
            $message1.= "Earliest Available Date : $available_date \n\n";
            $message1.= "Expected Salary : $salary \n\n";

            //get the extension of the file
            $base = basename($file_name);
            $extension = substr($base, strlen($base)-4, strlen($base));

            // mail sessenstials
            $from = $email;
            $to = "abc@hotmail.com";
            $subject = $position." Application Form";
            $message = $message1;

            // things you need
            $file = $temp_name;
            $content = chunk_split(base64_encode(file_get_contents($file)));
            $uid = md5(uniqid(time()));

            //standard mail headers
            $header = "FROM: ".$email. "\r\n";
            $header .= "Reply To: ".$to."\r\n";
            $header .= "MIME-Version: 1.0\r\n";

            //declaring we have nultiple kinds of email (plain text)
            $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
            $header .="This is a multi-part message in MIME format. \r\n";

            //plain text part
            $header .= "--".$uid."\r\n";
            $header .= "Content-type:text/plain: charset=iso-8859-1\r\n";
            $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
            $header .= $message."\r\n\r\n";

            //file attachment
            $header .= "--".$uid."\r\n";
            $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
            $header .= "Content-Transfer-Encoding: base64\r\n";
            $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
            $header .= $content."\r\n\r\n";

            //send mail
            if(mail($to, $subject, "", $header))
            {
                echo "success";
            }else {
                echo "fail";
            }

但是当我看到我的电子邮件时,附件文件如下所示:

--6eba57b85fe691e5db4019960b9911a9
Content-Type: image/jpeg; name="2.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="2.jpg"

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcG
BwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwM
DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAIYBYMDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p

我的代码有问题吗?

此解决方案是我从其他网站引用的。

0 个答案:

没有答案