简体   繁体   English

从mysql创建Excel并将其作为附件电子邮件发送到php

[英]Creating excel from mysql and sending this as attachment email in php

I need to send an email with attachment, and that attachment file contain some data fetched from mysql database at the same time. 我需要发送带有附件的电子邮件,并且该附件文件包含一些同时从mysql数据库中获取的数据。

That problem is already asked and described here but there is no any working answer. 该问题已在此处提出并描述但没有任何有效的答案。 Can anyone have solution, than please answer. 任何人都可以解决,比请回答。

   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:abcdf.k@gmail.com(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( 'abc.k@gmail.com', $subject, $msg, $headers );
   if( $mail == true )
   {
      echo "Message successfully sent";
   }
   else
   {
      echo "Message could not be sent";
   }

Excel is creating and have proper data but I need to mail this rather than download. Excel正在创建并具有正确的数据,但是我需要邮寄而不是下载。

Try this,it's working 试试这个,它正在工作

<?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 = "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=\"".$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) 

?>

I don't see any attempt in your code to make a Excel file or an email with an attachment. 我看不到您的代码尝试制作Excel文件或带有附件的电子邮件。 I see those as two seperate questions. 我认为这是两个单独的问题。

When it comes to email attachments, it's best to use something that already exists, like: 对于电子邮件附件,最好使用已存在的附件,例如:

http://swiftmailer.org http://swiftmailer.org

or 要么

https://github.com/Synchro/PHPMailer https://github.com/Synchro/PHPMailer

(order does not suggest anything) (顺序不代表任何东西)

For Excel the same thing applies, because with the CSV or XML versions of Excel files I always have problems. 对于Excel同样适用,因为使用CSV或XML版本的Excel文件时,我总是会遇到问题。 I have no suggestions though. 我没有任何建议。 I did find this more general site: 我确实找到了这个更一般的网站:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM