Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在php中将pdf附加到电子邮件_Php_Email_Pdf - Fatal编程技术网

无法在php中将pdf附加到电子邮件

无法在php中将pdf附加到电子邮件,php,email,pdf,Php,Email,Pdf,我试着遵循这个论坛中给出的例子,比如freestate给出的例子 下面是我从他修改过的示例中复制的代码。很好。收到电子邮件时,会在电子邮件中显示附件。但是,它无法打开,并且大小始终为125k 我已经验证了返回的文件大小带有“$file_size=filesize($file);”。而且“fopen”确实打开了流。事实上,在我将Multipart添加到标题之前,我可以看到文本主体中的内容(尽管不可读) 我只想在一个窗口平台(Windows10)、Apache2.4、PHP5.6.16.0上附加一个

我试着遵循这个论坛中给出的例子,比如freestate给出的例子

下面是我从他修改过的示例中复制的代码。很好。收到电子邮件时,会在电子邮件中显示附件。但是,它无法打开,并且大小始终为125k

我已经验证了返回的文件大小带有“$file_size=filesize($file);”。而且“fopen”确实打开了流。事实上,在我将Multipart添加到标题之前,我可以看到文本主体中的内容(尽管不可读)

我只想在一个窗口平台(Windows10)、Apache2.4、PHP5.6.16.0上附加一个基本的pdf文件

什么会导致附件无法正确连接

    emailer('//serverName/path/', 'documentName.pdf');

    function eMailer($path, $filename)
    {
        $eol = PHP_EOL;
        $to = "xxxx@yyyyyyyy.com";
        $subject = "File Name: ".$filename;

        $body = "The this the written content of the body";

        $file = $path.$filename;
        $file_size = filesize($file);

        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);

        fclose($handle);

        $content = chunk_split(base64_encode($content));
        $uid = md5(uniqid(time()));

        $header = "From: NameOfPerson <their@emailAddress.com>".$eol.
              "MIME-Version: 1.0\r\n".
              "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

        $message = "--".$uid.$eol.
             "Content-Type: text/html; charset=ISO-8859-1".$eol.
             "Content-Transfer-Encoding: 8bit".$eol.$eol.
             $body.$eol.

             "--".$uid.$eol.
             "Content-Type: application/pdf; name=\"".$filename."\"".$eol.
             "Content-Transfer-Encoding: base64".$eol.
             "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.
             $content.$eol.
             "--".$uid."--";

        echo (mail($to, $subject, $message, $header))?'success':'failure';  
    }
emailer(“//serverName/path/”,“documentName.pdf”);
函数eMailer($path,$filename)
{
$eol=PHP\u eol;
$to=”xxxx@yyyyyyyy.com";
$subject=“文件名:”.$filename;
$body=“这是正文的书面内容”;
$file=$path.$filename;
$file\u size=文件大小($file);
$handle=fopen($file,“r”);
$content=fread($handle,$file\u size);
fclose($handle);
$content=chunk_split(base64_encode($content));
$uid=md5(uniqid(time());
$header=“From:NameOfPerson”。$eol。
“MIME版本:1.0\r\n”。
“内容类型:多部分/混合;边界=\”.$uid.\”;
$message=“--”$uid.$eol。
“内容类型:text/html;字符集=ISO-8859-1”。$eol。
“内容传输编码:8位”。$eol.$eol。
$body.$eol。
“-”$uid.$eol。
“内容类型:application/pdf;名称=\”.$filename.\”.$eol。
“内容传输编码:base64”。$eol。
“内容处置:附件;文件名=\”.$filename.\”.$eol。
$content.$eol。
“——”$uid。”——”;
echo(邮件($to,$subject,$message,$header))?'success':'failure';
}

因此,在花了大量时间试图自己解决附件问题后,我决定使用两个常见的软件包中的一个。两个看起来都不错。我只在一个企业项目上工作,没有安装。所以我想要一些简单的东西,可以手动下载并插入到我的项目中。在阅读之后,我决定这将是易于安装和使用

这些是我采取的步骤

  • 我从Github下载了zip文件
  • 从解压后的文件中,我将“lib”文件夹复制到我的项目中“C:\myproject\lib
  • 然后我阅读了在线手册中的
  • 要在我的项目中使用它,我需要做的就是将其包含在我的代码中,如下所示:

        function eMailer($path, $filename, $mailto, $subject, $message, $user)
    {
        $file = $path.$filename;
        require_once 'C:\myProject\lib\swift_required.php';
    
        // Create the SMTP configuration
        $transport = Swift_SmtpTransport::newInstance("mymail_server_address ie: 192.111.22.33", 25);
        $transport->setUsername($user['email']);
        $transport->setPassword($user['password']);
    
        // Create the mail transport configuration
        $transport = Swift_MailTransport::newInstance();
    
        // Create the message
        $message = Swift_Message::newInstance();
        $message->setTo(array(
          $mailto['email']=> $mailto['name']
        ));
        $message->setSubject($subject);
        $message->setBody($message);
        $message->setFrom($user['email'], $user['name']);
        $message->attach(Swift_Attachment::fromPath($file));
    
        // Send the email
        $mailer = Swift_Mailer::newInstance($transport);
        $mailer->send($message);    
    }
    
  • 这显然比我现在需要的功能多得多,但如果将来我需要在项目的另一部分中使用邮件发送功能,它随时可供我使用。

    请尝试以下代码:

    $name           = "Name";
    $email      = "Email Address";
    $to             = "$name <$email>";
    $from           = "XYZ";
    $subject        = "TEST SUBJECT";
    $mainMessage    = "Hi, here's the file.";
    $fileatt        = "./test.pdf";
    $fileatttype    = "application/pdf";
    $fileattname    = "newname.pdf";
    $headers        = "From: $from";
    // File
    $file           = fopen ( $fileatt, 'rb' );
    $data           = fread ( $file, filesize ( $fileatt ) );
    fclose ( $file );
    // Attach the file
    $semi_rand  = md5 ( time () );
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    $headers       .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
    $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" . $mainMessage . "\n\n";
    $data           = chunk_split ( base64_encode ( $data ) );
    $message      .= "--{$mime_boundary}\n" . "Content-Type: {$fileatttype};\n" . " name=\"{$fileattname}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileattname}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "-{$mime_boundary}-\n";
    // Send Email
    if (mail ( $to, $subject, $message, $headers )) {   
        echo "The email was sent.";
    } else {    
        echo "There was an error sending the mail.";
    }
    
    $name=“name”;
    $email=“电子邮件地址”;
    $to=“$name”;
    $from=“XYZ”;
    $subject=“测试对象”;
    $mainMessage=“嗨,这是文件。”;
    $fileatt=“./test.pdf”;
    $fileatttype=“应用程序/pdf”;
    $fileattname=“newname.pdf”;
    $headers=“From:$From”;
    //文件
    $file=fopen($fileatt,'rb');
    $data=fread($file,filesize($fileatt));
    fclose($文件);
    //附上文件
    $semi_rand=md5(时间());
    $mime_boundary=“==Multipart_boundary_x{$semi_rand}x”;
    $headers.=“\n时间版本:1.0\n”。“内容类型:多部分/混合;\n”。“边界=\”{$mime\u boundary}\”;
    $message=“这是MIME格式的多部分消息。\n\n”。“-{$mime_boundary}\n”。“内容类型:text/plain;字符集=\“iso-8859-1\n”。“内容传输编码:7bit\n\n”。$mainMessage。”\n\n”;
    $data=chunk_split(base64_encode($data));
    $message.=“--{$mime\u boundary}\n.”内容类型:{$fileatttype}\n.““名称=\”{$fileattname}\“\n.”内容处置:附件\n.““文件名=\”{$fileattname}\”\n.“内容传输编码:base64\n\n.“$data”。\n\n.“-{$mime\U边界}-\n”;
    //发送电子邮件
    如果(邮件($to,$subject,$message,$headers)){
    回显“电子邮件已发送。”;
    }否则{
    echo“发送邮件时出错。”;
    }
    
    不要自己动手,而是使用预构建的库来处理细微差别: