我需要发送一封带附件的电子邮件,该附件文件同时包含从mysql数据库中提取的一些数据。
这个问题已经被问及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: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正在创建并拥有适当的数据,但我需要邮寄而不是下载。
答案 0 :(得分: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 = "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)
?>
答案 1 :(得分:0)
我没有看到您的代码尝试制作Excel文件或带附件的电子邮件。我认为这些是两个单独的问题。
对于电子邮件附件,最好使用已存在的内容,例如:
或
https://github.com/Synchro/PHPMailer
(订单不提示任何内容)
对于Excel,同样适用,因为使用CSV或XML版本的Excel文件时,我总是遇到问题。我没有建议。我确实找到了这个更通用的网站:
http://webdeveloperplus.com/php/5-libraries-to-generate-excel-reports-in-php/