PHP Convert/Export To CSV and Send Email Attachment |
PHP Convert/Export To CSV and Send Email Attachment ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP เพื่อ Export ฐานข้อมูล MySQL ในรูปแบบไฟล์ CSV และหลังจากการส่งออกแล้วทำการแนบไฟล์ส่งไปพร้อมกับอีเมล์
PHP Sending Email Attachment files
ตัวอย่าง
phpCSVMySQLListRecord3.php
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL To CSV</title>
</head>
<body>
<?php
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<?php
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?php echo $objResult["CustomerID"];?></div></td>
<td><?php echo $objResult["Name"];?></td>
<td><?php echo $objResult["Email"];?></td>
<td><div align="center"><?php echo $objResult["CountryCode"];?></div></td>
<td align="right"><?php echo $objResult["Budget"];?></td>
<td align="right"><?php echo $objResult["Used"];?></td>
</tr>
<?php
}
?>
</table>
<?php
mysql_close($objConnect);
?>
<div align="center"><br>
<input name="btnExport" type="button" value="Export" onClick="JavaScript:window.location='phpCSVMySQLExport.php';">
</div>
</body>
</html>
phpCSVMySQLExport4.php
<html>
<head>
<title>ThaiCreate.Com PHP & Write CSV</title>
</head>
<body>
<?php
$filName = "customer.csv";
$objWrite = fopen("customer.csv", "w");
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery))
{
fwrite($objWrite, "\"$objResult[CustomerID]\",\"$objResult[Name]\",\"$objResult[Email]\",");
fwrite($objWrite, "\"$objResult[CountryCode]\",\"$objResult[Budget]\",\"$objResult[Used]\" \n");
}
fclose($objWrite);
//*************** Send Email ***************//
$strTo = "[email protected]";
$strSubject = "CSV Report";
$strMessage = "Download $filName for CSV Report";
//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));
$strHeader = "";
$strHeader .= "From: Mr.Weerachai Nukitram<[email protected]>\nReply-To: [email protected]\n";
$strHeader .= "Cc: Mr.Surachai Sirisart<[email protected]>";
$strHeader .= "Bcc: [email protected]";
$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=windows-874\n"; // or UTF-8 //
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";
$strContent1 = chunk_split(base64_encode(file_get_contents("$filName")));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$filName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$filName."\"\n\n";
$strHeader .= $strContent1."\n\n";
$flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //
if($flgSend)
{
echo "CSV Generated & Email Sending.";
}
else
{
echo "Email Can Not Send.";
}
?>
</table>
</body>
</html>
ใช้งานจริงกับ Hotmail.Com
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-10-20 19:26:07 /
2017-03-25 01:47:13 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|