dougan6982 2016-07-15 09:23
浏览 28

too long

I try to add send email attachment using php,file attachment working fine but attachment file open blank document.How to solve this issue.Below mentioned my code.

$from_email = 'sender_mail@example.com'; //sender email
$recipient_email = 'manosk24@gmail.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body

$filename = "file1.pdf";
$path = $_SERVER['DOCUMENT_ROOT'] . "/mail-function/upload/";
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "rb");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));


$boundary = md5(uniqid(time()));
//header
$headers = "MIME-Version: 1.0
";
$headers .= "From:" . $from_email . "
";
$headers .= "Reply-To: " . $user_email . "" . "
";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary

";

//plain text 
$body = "--$boundary
";
$body .= "Content-Type: text/plain; charset=ISO-8859-1
";
$body .= "Content-Transfer-Encoding: base64

";
$body .= chunk_split(base64_encode($message));

//attachment
$body .= "--$boundary
";
$body .="Content-Type: application/pdf; name=" . $filename . "
";
$body .="Content-Disposition: attachment; filename=" . $filename . "
";
$body .="Content-Transfer-Encoding: base64
";
$body .="X-Attachment-Id: " . rand(1000, 99999) . "

";
$body .= $encoded_content;

$sentMail = @mail($recipient_email, $subject, $body, $headers);
if ($sentMail) { //output success or failure messages
    die('Thank you for your email');
} else {
    die('Could not send mail! Please check your PHP mail configuration.');
}

Sorry for my spelling mistake..

  • 写回答

1条回答 默认 最新

  • duanqu9292 2016-07-15 09:52
    关注

    try this code,

        $filename = "file1.pdf";
        $file = $path . "/" . $filename;
        $message ="my message";
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $content = chunk_split(base64_encode($content));
    
        // a random hash will be necessary to send mixed content
        $separator = md5(time());
    
        // carriage return type (we use a PHP end of line constant)
        $eol = PHP_EOL;
    
        // main header (multipart mandatory)
        $headers = "From: name <test@test.com>" . $eol;
        $headers .= "MIME-Version: 1.0" . $eol;
        $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
        $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
        $headers .= "This is a MIME encoded message." . $eol;
    
        // message
        $headers .= "--" . $separator . $eol;
        $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
        $headers .= "Content-Transfer-Encoding: 8bit" . $eol;
        $headers .= $message . $eol;
    
        // attachment
        $headers .= "--" . $separator . $eol;
        $headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
        $headers .= "Content-Transfer-Encoding: base64" . $eol;
        $headers .= "Content-Disposition: attachment" . $eol;
        $headers .= $content . $eol;
        $headers .= "--" . $separator . "--";
    
        //SEND Mail
         if (mail($mailto, $subject, "", $headers)) {
            echo "mail send ... OK"; // or use booleans here
          } else {
            echo "mail send ... ERROR!";
          }
    

    i hope it will be helpful.

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致