2014-10-16 77 views
0

我需要發送帶附件的電子郵件,並且該附件文件包含一些同時從mysql數據庫獲取的數據。從mysql創建excel並將其作爲附件郵件發送到php

這個問題已經被詢問和描述here但沒有任何工作答案。 任何人都可以有解決方案,比請回答。

while($row = mysql_fetch_row($sqlQuery)) 
      { 
      $line = ''; 
       foreach($row as $value) 
       {            
        if ((!isset($value)) || ($value == "")) 
        { 
        $value = "\t"; 
        } 
        else 
        { 
        $value = str_replace('"' , '""' , $value); 
        $value = '"' . $value . '"' . "\t"; 
        } 
       $line .= $value; 
       } 
      $data .= trim($line) . "\n"; 
      } 
      $data = str_replace("\r" , "" , $data); 

      $cho = "$header\n$data"; 
      echo $cho; 

$headers = "From:[email protected](PFC Web Admin) \r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: application/vnd.ms-excel"; 
$headers .= 'Content-Disposition: attachment; filename=Test.xls' . "\r\n"; 
$headers .= "Content-Transfer-Encoding: base64\r\n"; 
$headers .= "".$cho."\r\n"; 
$subject = 'Record November'; 

    $mail = mail('[email protected]', $subject, $msg, $headers); 
    if($mail == true) 
    { 
     echo "Message successfully sent"; 
    } 
    else 
    { 
     echo "Message could not be sent"; 
    } 

Excel正在創建並具有適當的數據,但我需要郵寄而不是下載。

回答

0

我在您的代碼中看不到任何企圖以Excel文件或電子郵件附件。我把這些看作是兩個單獨的問題。

當涉及到電子郵件附件,最好使用已經存在的東西,如:

http://swiftmailer.org

https://github.com/Synchro/PHPMailer

(順序並不意味着任何東西)

對於Excel同樣適用,因爲使用Excel或CSV或XML版本文件我總是有問題。但我沒有任何建議。我沒有找到這個更一般的網站:

http://webdeveloperplus.com/php/5-libraries-to-generate-excel-reports-in-php/

+0

這就是問題所在,我需要爲這兩個問題的解決方案。 – 2014-10-16 11:08:07

+0

我不認爲你會發現。兩個問題需要兩個解決方案。使用一種解決方案,您可以製作Excel文件,並通過電子郵件發送給其他人。把東西放在一起,讓它做你想做的,是編程的基礎。 – 2014-10-16 12:31:44

1

試試這個,它的工作

<?php 
include_once('inc/dbConnect.inc.php'); 
error_reporting(E_ERROR); 
$sql = mysql_query("SELECT * FROM tablename"); 
$row=mysql_fetch_assoc($sql); 
$filename='temp/'.$filename.'.csv'; 
$fp=fopen($filename,"w"); 
$seperator=""; 

$comma=""; 

foreach($row as $name =>$value) 

{ 

$seperator.=$comma.''.str_replace('','""',$name); 
$comma=","; 

} 
$seperator.="\n"; 
$seperator; 

fputs($fp,$seperator); 

mysql_data_seek($sql,0); 

while($row=mysql_fetch_assoc($sql)) 

{ 

$seperator=""; 

$comma=""; 
foreach($row as $name =>$value) 

{ 

$seperator.=$comma.''.str_replace('','""',$value); 
$comma=","; 

} 

$seperator.="\n"; 
fputs($fp,$seperator); 

} 

fclose($fp); 
$my_file = $filename.'.csv'; 
$path = "temp/"; 


$from_name = "hhhhh"; 

$from_mail = "[email protected]"; 

$mailto = "[email protected]"; 

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

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

$replyto="[email protected]"; 
$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=\"".$filename."\"\r\n"; // use different content types here 

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

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

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

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

?> 
+0

你的代碼是非常好的@哈拉和工作幾乎適當,但附件文件在郵件中爲空,你可以給我任何建議。 – 2014-10-18 09:19:52

+0

我檢查了我的查詢,很好。將數據生成到臨時文件夾中,但不要郵寄此文件。它是郵寄另一個文件名前綴與「temp_」這是空白的,你能幫我嗎 – 2014-10-18 12:12:37

+0

檢查您傳遞的文件名作爲參數。請讓我知道如果您仍然面臨任何問題其他請將您的代碼發送給我的電子郵件我會檢查並回復你。 – 2014-10-18 12:55:59

相關問題