web 2.0

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

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


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
$wdAlignParagraphCenter = "1";
$wdAlignParagraphRight = "2";

$Wrd = new COM("Word.Application");
$DocName = "MyDoc/MyWord.doc";
$Wrd->Application->Visible = False;

//$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp

$WrdDoc = $Wrd->Documents->Open(realpath("shotdev.dot"));

$MyRange1 = $WrdDoc->Paragraphs->Add->Range;
$MyRange1->ParagraphFormat->Alignment = $wdAlignParagraphCenter;
$MyRange1->Font->Name = "Verdana";
$MyRange1->Font->Size = "20";
$MyRange1->Font->Bold = True;
$MyRange1->InsertBefore("Customer Report".chr(13));

$objConnect = mysql_connect("localhost","root","root") or die(mysql_error());
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer";
$objQuery = mysql_query($strSQL);
$intRows = mysql_num_rows($objQuery);

$MyRange2 = $WrdDoc->Paragraphs->Add->Range;
$MyRange2->Font->Size = "10";
$objTable = $Wrd->ActiveDocument->Tables->Add($MyRange2,$intRows+1,6,1,2); //** Range,Rows,Column **//

//*** Header ***//
$objTable->Cell(1,1)->Range->InsertAfter("CustomerID");
$objTable->Cell(1,1)->Range->Bold = True;
$objTable->Cell(1,1)->Range->ParagraphFormat->Alignment = 1;

$objTable->Cell(1,2)->Range->InsertAfter("Name");
$objTable->Cell(1,2)->Range->Bold = True;
$objTable->Cell(1,2)->Range->ParagraphFormat->Alignment = 1;

$objTable->Cell(1,3)->Range->InsertAfter("Email");
$objTable->Cell(1,3)->Range->Bold = True;
$objTable->Cell(1,3)->Range->ParagraphFormat->Alignment = 1;

$objTable->Cell(1,4)->Range->InsertAfter("CountryCode");
$objTable->Cell(1,4)->Range->Bold = True;
$objTable->Cell(1,4)->Range->ParagraphFormat->Alignment = 1;

$objTable->Cell(1,5)->Range->InsertAfter("Budget");
$objTable->Cell(1,5)->Range->Bold = True;
$objTable->Cell(1,5)->Range->ParagraphFormat->Alignment = 1;

$objTable->Cell(1,6)->Range->InsertAfter("Used");
$objTable->Cell(1,6)->Range->Bold = True;
$objTable->Cell(1,6)->Range->ParagraphFormat->Alignment = 1;

//*** Detail ***//
$intRows = 1;
while($result = mysql_fetch_array($objQuery))
{
$intRows++;
$objTable->Cell($intRows,1)->Range->InsertAfter($result["CustomerID"]);
$objTable->Cell($intRows,1)->Range->ParagraphFormat->Alignment = 1;

$objTable->Cell($intRows,2)->Range->InsertAfter($result["Name"]);
$objTable->Cell($intRows,2)->Range->ParagraphFormat->Alignment = 0;

$objTable->Cell($intRows,3)->Range->InsertAfter($result["Email"]);
$objTable->Cell($intRows,3)->Range->ParagraphFormat->Alignment = 0;

$objTable->Cell($intRows,4)->Range->InsertAfter($result["CountryCode"]);
$objTable->Cell($intRows,4)->Range->ParagraphFormat->Alignment = 1;

$objTable->Cell($intRows,5)->Range->InsertAfter(number_format($result["Budget"],2));
$objTable->Cell($intRows,5)->Range->ParagraphFormat->Alignment = 2;

$objTable->Cell($intRows,6)->Range->InsertAfter(number_format($result["Used"],2));
$objTable->Cell($intRows,6)->Range->ParagraphFormat->Alignment = 2;
}

$MyRange3 = $WrdDoc->Paragraphs->Add->Range;
$MyRange3->ParagraphFormat->Alignment = $wdAlignParagraphRight;
$MyRange3->Font->Name = "Verdana";
$MyRange3->Font->Size = "10";
$MyRange3->InsertBefore(chr(13).chr(13).chr(13)." ................................Manager".chr(13).date("Y-m-d H:i:s"));

//$WrdDoc->SaveAs($strPath."/".$DocName);
$WrdDoc->SaveAs(realpath($DocName));
$Wrd->Application->Quit;
$Wrd = null;

//*************** Send Email ***************//

$strTo = "member@shotdev.com";
$strSubject = "Word Report";
$strMessage = "Word MyWord.doc Report";

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";

$strHeader .= "From: Mr.Weerachai Nukitram<webmaster@shotdev.com>\nReply-To: webmaster@shotdev.com\n";
$strHeader .= "Cc: Mr.Surachai Sirisart<surachai@shotdev.com>";
$strHeader .= "Bcc: webmaster@shotdev.com";

$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("MyDoc/MyWord.doc")));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"MyWord.doc\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"MyWord.doc\"\n\n";
$strHeader .= $strContent1."\n\n";

$flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //
if($flgSend)
{
echo "Word Generated & Email Sending.";
}
else
{
echo "Cannot send mail.";
}

?>
</body>
</html>

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_word_mail.php

Screenshot

PHP & Generate Word and Send Email Attachment
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.