Learning & Integrating web technology and code help directory

How to use PHP & Convert/Export To CSV and Send Email Attachment

No comments
How to use PHP & Convert/Export To CSV and Send Email Attachment Learn / tutorial php programming how to using  PHP Convert/Export To CSV and Send Email Attachment
ShotDev Focus:
- PHP Convert/Export To CSV and Send Email Attachment.
Example
php_csv_mail1.php

  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  8. $objDB = mysql_select_db("mydatabase");  
  9. $strSQL = "SELECT * FROM customer";  
  10. $objQuery = mysql_query($strSQLor die ("Error Query [".$strSQL."]");  
  11. ?>  
  12. <table width="600" border="1">  
  13. <tr>  
  14. <th width="91"> <div align="center">CustomerID </div></th>  
  15. <th width="98"> <div align="center">Name </div></th>  
  16. <th width="198"> <div align="center">Email </div></th>  
  17. <th width="97"> <div align="center">CountryCode </div></th>  
  18. <th width="59"> <div align="center">Budget </div></th>  
  19. <th width="71"> <div align="center">Used </div></th>  
  20. </tr>  
  21. <?  
  22. while($objResult = mysql_fetch_array($objQuery))  
  23. {  
  24. ?>  
  25. <tr>  
  26. <td><div align="center"><?=$objResult["CustomerID"];?></div></td>  
  27. <td><?=$objResult["Name"];?></td>  
  28. <td><?=$objResult["Email"];?></td>  
  29. <td><div align="center"><?=$objResult["CountryCode"];?></div></td>  
  30. <td align="right"><?=$objResult["Budget"];?></td>  
  31. <td align="right"><?=$objResult["Used"];?></td>  
  32. </tr>  
  33. <?  
  34. }  
  35. ?>  
  36. </table>  
  37. <?  
  38. mysql_close($objConnect);  
  39. ?>  
  40. <div align="center"><br>  
  41. <input name="btnExport" type="button" value="Export / Send Mail" onClick="JavaScript:window.location='php_csv_mail2.php';">  
  42. </div>  
  43. </body>  
  44. </html>  
php_csv_mail2.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. $strPath = "shotdev";  
  8. $filName = "customer.csv";  
  9. $objWrite = fopen($strPath."/".$filName"w");  
  10.   
  11. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  12. $objDB = mysql_select_db("mydatabase");  
  13. $strSQL = "SELECT * FROM customer";  
  14. $objQuery = mysql_query($strSQLor die ("Error Query [".$strSQL."]");  
  15.   
  16. while($objResult = mysql_fetch_array($objQuery))  
  17. {  
  18. fwrite($objWrite"\"$objResult[CustomerID]\",\"$objResult[Name]\",\"$objResult[Email]\",");  
  19. fwrite($objWrite"\"$objResult[CountryCode]\",\"$objResult[Budget]\",\"$objResult[Used]\" \n");  
  20. }  
  21. fclose($objWrite);  
  22.   
  23. //*************** Send Email ***************//  
  24.   
  25. $strTo = "member@shotdev.com";  
  26. $strSubject = "CSV Report";  
  27. $strMessage = "Download $filName for CSV Report";  
  28.   
  29. //*** Uniqid Session ***//  
  30. $strSid = md5(uniqid(time()));  
  31.   
  32. $strHeader = "";  
  33. $strHeader .= "From: Mr.Weerachai Nukitram<webmaster@shotdev.com>\nReply-To: webmaster@shotdev.com\n";  
  34. $strHeader .= "Cc: Mr.Surachai Sirisart<surachai@shotdev.com>";  
  35. $strHeader .= "Bcc: webmaster@shotdev.com";  
  36.   
  37. $strHeader .= "MIME-Version: 1.0\n";  
  38. $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
  39. $strHeader .= "This is a multi-part message in MIME format.\n";  
  40.   
  41. $strHeader .= "--".$strSid."\n";  
  42. $strHeader .= "Content-type: text/html; charset=windows-874\n"// or UTF-8 //  
  43. $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
  44. $strHeader .= $strMessage."\n\n";  
  45.   
  46. $strContent1 = chunk_split(base64_encode(file_get_contents($strPath."/".$filName)));  
  47. $strHeader .= "--".$strSid."\n";  
  48. $strHeader .= "Content-Type: application/octet-stream; name=\"".$filName."\"\n";  
  49. $strHeader .= "Content-Transfer-Encoding: base64\n";  
  50. $strHeader .= "Content-Disposition: attachment; filename=\"".$filName."\"\n\n";  
  51. $strHeader .= $strContent1."\n\n";  
  52.   
  53. $flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //  
  54. if($flgSend)  
  55. {  
  56. echo "CSV Generated & Email Sending.";  
  57. }  
  58. else  
  59. {  
  60. echo "Can not send mail.";  
  61. }  
  62. ?>  
  63. </body>  
  64. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Convert/Export To CSV and Send Email Attachment

No comments :

Post a Comment