Learning & Integrating web technology and code help directory

How to use PHP & Word (Word.Application) - Generate Word and Send Email Attachment

No comments
How to use PHP & Word (Word.Application) - Generate Word and Send Email Attachment The Learn / Lutorial / Sctipts php programming how to using  PHP Generate Word and Send Email Attachment
ShotDev Focus:
- PHP  & Generate Word and Send Email Attachment
Example
php_word_mail.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. $wdAlignParagraphCenter = "1";  
  8. $wdAlignParagraphRight = "2";  
  9.   
  10. $Wrd = new COM("Word.Application");  
  11. $DocName = "MyDoc/MyWord.doc";  
  12. $Wrd->Application->Visible = False;  
  13.   
  14. //$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp  
  15.   
  16. $WrdDoc = $Wrd->Documents->Open(realpath("shotdev.dot"));  
  17.   
  18. $MyRange1 = $WrdDoc->Paragraphs->Add->Range;  
  19. $MyRange1->ParagraphFormat->Alignment = $wdAlignParagraphCenter;  
  20. $MyRange1->Font->Name = "Verdana";  
  21. $MyRange1->Font->Size = "20";  
  22. $MyRange1->Font->Bold = True;  
  23. $MyRange1->InsertBefore("Customer Report".chr(13));  
  24.   
  25. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  26. $objDB = mysql_select_db("mydatabase");  
  27. $strSQL = "SELECT * FROM customer";  
  28. $objQuery = mysql_query($strSQL);  
  29. $intRows = mysql_num_rows($objQuery);  
  30.   
  31. $MyRange2 = $WrdDoc->Paragraphs->Add->Range;  
  32. $MyRange2->Font->Size = "10";  
  33. $objTable = $Wrd->ActiveDocument->Tables->Add($MyRange2,$intRows+1,6,1,2); //** Range,Rows,Column **//  
  34.   
  35. //*** Header ***//  
  36. $objTable->Cell(1,1)->Range->InsertAfter("CustomerID");  
  37. $objTable->Cell(1,1)->Range->Bold = True;  
  38. $objTable->Cell(1,1)->Range->ParagraphFormat->Alignment = 1;  
  39.   
  40. $objTable->Cell(1,2)->Range->InsertAfter("Name");  
  41. $objTable->Cell(1,2)->Range->Bold = True;  
  42. $objTable->Cell(1,2)->Range->ParagraphFormat->Alignment = 1;  
  43.   
  44. $objTable->Cell(1,3)->Range->InsertAfter("Email");  
  45. $objTable->Cell(1,3)->Range->Bold = True;  
  46. $objTable->Cell(1,3)->Range->ParagraphFormat->Alignment = 1;  
  47.   
  48. $objTable->Cell(1,4)->Range->InsertAfter("CountryCode");  
  49. $objTable->Cell(1,4)->Range->Bold = True;  
  50. $objTable->Cell(1,4)->Range->ParagraphFormat->Alignment = 1;  
  51.   
  52. $objTable->Cell(1,5)->Range->InsertAfter("Budget");  
  53. $objTable->Cell(1,5)->Range->Bold = True;  
  54. $objTable->Cell(1,5)->Range->ParagraphFormat->Alignment = 1;  
  55.   
  56. $objTable->Cell(1,6)->Range->InsertAfter("Used");  
  57. $objTable->Cell(1,6)->Range->Bold = True;  
  58. $objTable->Cell(1,6)->Range->ParagraphFormat->Alignment = 1;  
  59.   
  60. //*** Detail ***//  
  61. $intRows = 1;  
  62. while($result = mysql_fetch_array($objQuery))  
  63. {  
  64. $intRows++;  
  65. $objTable->Cell($intRows,1)->Range->InsertAfter($result["CustomerID"]);  
  66. $objTable->Cell($intRows,1)->Range->ParagraphFormat->Alignment = 1;  
  67.   
  68. $objTable->Cell($intRows,2)->Range->InsertAfter($result["Name"]);  
  69. $objTable->Cell($intRows,2)->Range->ParagraphFormat->Alignment = 0;  
  70.   
  71. $objTable->Cell($intRows,3)->Range->InsertAfter($result["Email"]);  
  72. $objTable->Cell($intRows,3)->Range->ParagraphFormat->Alignment = 0;  
  73.   
  74. $objTable->Cell($intRows,4)->Range->InsertAfter($result["CountryCode"]);  
  75. $objTable->Cell($intRows,4)->Range->ParagraphFormat->Alignment = 1;  
  76.   
  77. $objTable->Cell($intRows,5)->Range->InsertAfter(number_format($result["Budget"],2));  
  78. $objTable->Cell($intRows,5)->Range->ParagraphFormat->Alignment = 2;  
  79.   
  80. $objTable->Cell($intRows,6)->Range->InsertAfter(number_format($result["Used"],2));  
  81. $objTable->Cell($intRows,6)->Range->ParagraphFormat->Alignment = 2;  
  82. }  
  83.   
  84. $MyRange3 = $WrdDoc->Paragraphs->Add->Range;  
  85. $MyRange3->ParagraphFormat->Alignment = $wdAlignParagraphRight;  
  86. $MyRange3->Font->Name = "Verdana";  
  87. $MyRange3->Font->Size = "10";  
  88. $MyRange3->InsertBefore(chr(13).chr(13).chr(13)." ................................Manager".chr(13).date("Y-m-d H:i:s"));  
  89.   
  90. //$WrdDoc->SaveAs($strPath."/".$DocName);  
  91. $WrdDoc->SaveAs(realpath($DocName));  
  92. $Wrd->Application->Quit;  
  93. $Wrd = null;  
  94.   
  95. //*************** Send Email ***************//  
  96.   
  97. $strTo = "member@shotdev.com";  
  98. $strSubject = "Word Report";  
  99. $strMessage = "Word MyWord.doc Report";  
  100.   
  101. //*** Uniqid Session ***//  
  102. $strSid = md5(uniqid(time()));  
  103.   
  104. $strHeader = "";  
  105.   
  106. $strHeader .= "From: Mr.Weerachai Nukitram<webmaster@shotdev.com>\nReply-To: webmaster@shotdev.com\n";  
  107. $strHeader .= "Cc: Mr.Surachai Sirisart<surachai@shotdev.com>";  
  108. $strHeader .= "Bcc: webmaster@shotdev.com";  
  109.   
  110. $strHeader .= "MIME-Version: 1.0\n";  
  111. $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
  112. $strHeader .= "This is a multi-part message in MIME format.\n";  
  113.   
  114. $strHeader .= "--".$strSid."\n";  
  115. $strHeader .= "Content-type: text/html; charset=windows-874\n"// or UTF-8 //  
  116. $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
  117. $strHeader .= $strMessage."\n\n";  
  118.   
  119. $strContent1 = chunk_split(base64_encode(file_get_contents("MyDoc/MyWord.doc")));  
  120. $strHeader .= "--".$strSid."\n";  
  121. $strHeader .= "Content-Type: application/octet-stream; name=\"MyWord.doc\"\n";  
  122. $strHeader .= "Content-Transfer-Encoding: base64\n";  
  123. $strHeader .= "Content-Disposition: attachment; filename=\"MyWord.doc\"\n\n";  
  124. $strHeader .= $strContent1."\n\n";  
  125.   
  126. $flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //  
  127. if($flgSend)  
  128. {  
  129. echo "Word Generated & Email Sending.";  
  130. }  
  131. else  
  132. {  
  133. echo "Cannot send mail.";  
  134. }  
  135.   
  136. ?>  
  137. </body>  
  138. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Generate Word and Send Email Attachment

No comments :

Post a Comment