使用PHP中的mail()发送附件文件

时间:2014-06-27 16:44:13

标签: php

我从其他帖子中引用了这个解决方案,我尝试修改内容以满足我的要求。

以下是我使用PHP通过邮件发送附件文件的代码。

       $htmlbody = " Your Mail Contant Here.... You can use html tags here...";
        $to = "foo@bar.com"; //Recipient Email Address
        $subject = "Test email with attachment"; //Email Subject
        $headers = "From: foo@bar.com\r\nReply-To: foo@bar.com";
        $random_hash = md5(date('r', time()));
        $headers .= "\r\nContent-Type: multipart/mixed; 
        boundary=\"PHP-mixed-".$random_hash."\"";
        // Set your file path here
        $path = $_FILES["cv"]["name"];
        $attachment = chunk_split(base64_encode(file_get_contents($path))); 


        //define the body of the message.
        $message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; 
        boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
        $message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; 
        charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";


        //Insert the html message.
        $message .= $htmlbody;
        $message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";


        //include attachment
        $message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip; 
        name=\"$path\"\r\n"."Content-Transfer-Encoding: 
        base64\r\n"."Content-Disposition: attachment\r\n\r\n";

        $message .= $attachment;
        $message .= "/r/n--PHP-mixed-$random_hash--";


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

        echo $mail ? "Mail sent" : "Mail failed";

我能够获取附件文件,但我无法打开它。

它向我显示"您选择的应用程序("")无法找到。检查文件名或选择其他应用程序。"

任何人都可以纠正我的错误吗?

1 个答案:

答案 0 :(得分:0)

下载后问题似乎依赖于文件名。服务器完成了工作并发送了电子邮件,这是客户端问题。

  1. 检查文件扩展名并确保它与发送它的那一面相同,如果没有,请确保你更正了PHP代码命名功能。

  2. 确保您的PC上有可读取该文件类型的应用程序。

  3. 尝试在另一台计算机上打开该文件。

相关问题