我想使用带附件(CSV文件)的php发送电子邮件。它工作正常,但我想从我的数据库中的一个表的数据制作该csv文件。我可以这样做吗?
<?php
require_once('\PHPMailer-master\class.phpmailer.php');
$email = new PHPMailer();
$email->From = 'myid@yahoo.in';
$email->FromName = 'name';
$email->Subject = 'Message Subject';
$email->Body = "GO away";
$email->AddAddress( 'abc@example.com' );
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('serial', 'group','end'));
$rows = mysqli_query($db, 'SELECT * FROM list');
while ($row = mysqli_fetch_assoc($rows)) {
fputcsv($output, $row);
}
fclose($output);
mysqli_close($db);
$file_to_attach = 'data.csv';
$email->AddAttachment( $output , 'data.csv' );
return $email->Send();
?>
答案 0 :(得分:0)
将$output = fopen('php://output', 'w');
行更改为$output = fopen('data.csv', 'w');
。
那应该在PHPscript所在的目录中创建一个data.csv文件。在该目录上拥有脚本的写入权限非常重要。
答案 1 :(得分:0)
对我有用。希望对您有帮助
$con = mysqli_connect('localhost', 'username', 'password', 'databasename');
if (!$con)
{
die("error" . mysqli_connect_error());
}
error_reporting(E_ERROR);
$filename = "teaching_information";
$sql = mysqli_query($con, "SELECT * FROM teaching_information order by id desc limit 0,10");
$row = mysqli_fetch_assoc($sql);
$filename2='datas/'.$filename.'.csv';
$fp = fopen($filename2, "w");
$seperator = "";
$comma = "";
foreach ($row as $name => $value){$seperator .= $comma . '' . str_replace('', '""', $name);$comma = ",";}
$seperator .= "\n";
$seperator;
fputs($fp, $seperator);
mysqli_data_seek($sql, 0);
while ($row = mysqli_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 = $filename2;
$path = "datas/";
$from_name = "solomon";
$from_mail = "pss@gmail.com";
$mailto = "pssworkcse@gmail.com";
$subject = "This is a mail with attachment.";
$message = "Hi,\r\n do you got attachment?\r\n\r\Solomon";
$replyto = "pssworkcse@gmail.com";
$file = $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=\"" . $filename2 . "\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" . $filename2 . "\"\r\n\r\n";
$header .= $content . "\r\n\r\n";
$header .= "--" . $uid . "--";
mail($mailto, $subject, "", $header)
?>