PHP,MYSQL从数据库发送带有pdf附件的电子邮件

时间:2014-10-23 14:05:55

标签: php mysql email

我有一张这样的表:

file_id : INT
file_name : VARCHAR[20]
file_data : BLOB

我将PDF文件存储在里面,我想发送一封电子邮件(使用PHP)和数据库中的附件。任何想法或指南?

编辑:我不想下载它然后附加它,然后删除临时文件。

1 个答案:

答案 0 :(得分:0)

试试这个

<?php
$path = "temp/";
$my_file="file.pdf";

$from_name = "hhhhh";

$from_mail = "abc@gmail.com";

$mailto = "abc@gmail.com";

$subject = "This is a mail with attachment.";

$message = "Hi,\r\n do you got attachment?\r\n\r\Hara";

$replyto="haraprasad@lemonpeak.com";
$file = $path.$my_file;
    $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()));

$name = basename($file);

 $header = "From: ".$from_name." <".$from_mail.">\r\n";

$header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";

 $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

$header .= "This is a multi-part message in MIME format.\r\n";

$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";
    $header .= "--".$uid."\r\n";

$header .= "Content-Type: application/octet-stream; name=\"".$name."\"\r\n"; // use different content types here

$header .= "Content-Transfer-Encoding: base64\r\n";

$header .= "Content-Disposition: attachment; filename=\"".$name."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";

$header .= "--".$uid."--";

mail($mailto, $subject, $message, $header) 

?>