samoht Posted October 28, 2008 Share Posted October 28, 2008 Hello all, I am trying to create a invoice and timesheet application using php and mysql. I have been able to create a nice form to submit hours - and another form that retrieves the hours and other info for each client work was done for through I time frame specified by the user. All very good. Now I need to separate out each client into their own invoice which I would like to send as and email attachment. I would like to send one email - but have multiple attachments (if required) How do I go about creating files to be attached?? can I set them as WORD documents? - or just html files?? then how would I send them all in 1 email? Any ideas?? Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted October 28, 2008 Share Posted October 28, 2008 hi. i looked around i i found these: http://www.tutorialized.com/view/tutorial/Send-an-Email-Attachment-with-PHP/14956 http://www.hollowearth.co.uk/tech/php/email_attachments.php http://www.edmondscommerce.co.uk/blog/php/php-email-attachment-function/ http://www.codewalkers.com/c/a/Email-Code/PHP-Email-Attachment-v2-UPDATED-091106/ http://www.codewalkers.com/c/a/Email-Code/PHP-Text-HTML-Email-with-Unlimited-Attachments/ i didn't read through them thoroughly, but i looked over them quickly. the 1st one seems to be the most heavily explained and documented, while the last one is horribly messy. i don't think any of these tutorials use any deprecated php features, but you should make sure before implementing anything. hope this is what you were looking for. good luck. Quote Link to comment Share on other sites More sharing options...
samoht Posted October 28, 2008 Author Share Posted October 28, 2008 Those told me how to include an attachment but I did not see how to create a file as an attachment. I did see this: <? $fp = fopen("amit.doc", 'w+'); $str = "<B>This is the text for the word file created through php programming</B>"; fwrite($fp, $str); fclose($fp); ?> which I will try - but If it works I'll still need to collect all the files created as attachments to one email. Quote Link to comment Share on other sites More sharing options...
samoht Posted October 28, 2008 Author Share Posted October 28, 2008 OK so I tried: <?php if($_POST['date_0']){ $q = mysql_query('SELECT TIMEDIFF(ts_timeout, ts_timein) as totaltime, TIME_TO_SEC(TIMEDIFF(ts_timeout, ts_timein)) as totalsecs, ts_client, ts_dscr, ts_created FROM jos_chronoforms_timesheet2 WHERE ts_created BETWEEN "' .$_POST['date_0'].'" AND "'.$_POST['date_1'].'" ORDER BY ts_client, ts_created') or die (mysql_error()); $c=1; $t=0; $prevclient = ''; //loop through db and separate by each client while($row = mysql_fetch_assoc( $q )){ if($row['ts_client'] != $prevclient){ if($prevclient != '') { //this is just to add extra rows for filling out the page! for($w=0; $w < 17 - $t; $w++){ $str .= ' <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr>'; } //get the total time in seconds and convert it to a decimal for each client $n=$gt/3600; //close each client table except for the last $str .= ' <tr style="page-break-inside:avoid;height:.2in"> <td width=414 valign=top style="width:310.5pt;border:none;padding:2.15pt 5.75pt 2.15pt 5.75pt; height:.2in"> <p class=MsoNormal> </p> </td> <td width=204 colspan=2 style="width:153.0pt;border:none;border-right:solid windowtext 1.0pt; padding:2.15pt .15in 2.15pt .15in;height:.2in"> <p class=RightAligned>TOTAL</p> </td> <td width=102 style="width:76.5pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:2.15pt .15in 2.15pt .15in;height:.2in"> <p class=Amount>$'.round($n * 37.75, 2) .'</p> </td> </tr> </table> </div> </body> </html> '."\n"; fwrite($fp, $str); fclose($fp); //set these back to 0 so that the total does not include the previous clients $n=0; $gt = 0; $t=0; $fp=''; $str=''; } // start the new WORD document $cname = str_replace(".","-",$row['ts_client']); $filename = '"'.$cname.'-invoice_'.date('m-j-Y').'.doc"'; $fp = fopen($filename, 'x+'); //start writting the file contents. //throw in some style $str = ' <html> <style> <!-- /* Font Definitions */ @font-face {font-family:Tahoma; panose-1:2 11 6 4 3 5 4 4 2 4;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; line-height:110%; font-size:8.5pt; font-family:Tahoma; letter-spacing:.2pt;} h1 {margin:0in; margin-bottom:.0001pt; text-align:right; line-height:110%; font-size:20.0pt; font-family:Tahoma; color:gray; letter-spacing:.2pt;} h2 {margin:0in; margin-bottom:.0001pt; line-height:110%; font-size:8.0pt; font-family:Tahoma; text-transform:uppercase; letter-spacing:.2pt;} h3 {margin:0in; margin-bottom:.0001pt; line-height:110%; font-size:8.5pt; font-family:Tahoma; letter-spacing:.2pt; font-weight:normal; font-style:italic;} p.MsoAcetate, li.MsoAcetate, div.MsoAcetate {margin:0in; margin-bottom:.0001pt; line-height:110%; font-size:8.0pt; font-family:Tahoma; letter-spacing:.2pt;} p.Companyname, li.Companyname, div.Companyname {margin-top:7.0pt; margin-right:0in; margin-bottom:0in; margin-left:0in; margin-bottom:.0001pt; line-height:110%; font-size:12.0pt; font-family:Tahoma; letter-spacing:.2pt; font-weight:bold;} p.Columnheading, li.Columnheading, div.Columnheading {margin:0in; margin-bottom:.0001pt; text-align:center; line-height:110%; font-size:8.0pt; font-family:Tahoma; letter-spacing:.2pt; font-weight:bold;} p.RightAligned, li.RightAligned, div.RightAligned {margin:0in; margin-bottom:.0001pt; text-align:right; line-height:110%; font-size:8.0pt; font-family:Tahoma; text-transform:uppercase; letter-spacing:.2pt;} p.Thankyou, li.Thankyou, div.Thankyou {margin:0in; margin-bottom:.0001pt; text-align:center; line-height:110%; font-size:10.0pt; font-family:Tahoma; letter-spacing:.2pt; font-weight:bold;} p.Amount, li.Amount, div.Amount {margin:0in; margin-bottom:.0001pt; text-align:right; line-height:110%; font-size:8.5pt; font-family:Tahoma; letter-spacing:.2pt;} @page Section1 {size:8.5in 11.0in; margin:.5in .5in 36.7pt .5in;} div.Section1 {page:Section1;} /* List Definitions */ ol {margin-bottom:0in;} ul {margin-bottom:0in;} --> </style> <body>'."\n"; $strc=$row['ts_client']; $fl=$strc[0]; //begin actual page $str .= ' <div align=center> <table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=720 style="width:7.5in;border-collapse:collapse"> <tr style="height:39.75pt"> <td width=361 rowspan=2 valign=top style="width:270.6pt;padding:0in 5.4pt 0in 5.4pt; height:39.75pt"> <p class=Companyname>Andrew T Adcock</p> <h3>Web work</h3> <p class=MsoNormal> </p> <p class=MsoNormal>233 Hamel Ave</p> <p class=MsoNormal>Glenside, PA 19038</p> <p class=MsoNormal>Phone (267)943-1097 </p> </td> <td width=359 valign=top style="width:269.4pt;padding:0in 5.4pt 0in 5.4pt; height:39.75pt"> <h1>INVOICE</h1> </td> </tr> <tr style="height:39.75pt"> <td width=359 valign=bottom style="width:269.4pt;padding:0in 5.4pt 0in 5.4pt; height:39.75pt"> <p class=RightAligned>Invoice #'.$c.'</p> <p class=RightAligned>Date: '.date('F jS, Y').'</p> </td> </tr> </table> </div> <p class=MsoNormal> </p> <div align=center> <table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=720 style="width:7.5in;border-collapse:collapse"> <tr style="height:1.0in"> <td width=360 valign=top style="width:3.75in;padding:0in 5.4pt 0in 5.4pt; height:1.0in"> <h2>To:</h2> <p class=MsoNormal>Irene Galvan </p> <p class=MsoNormal>TNTmax</p> <p class=MsoNormal></p> <p class=MsoNormal></p> <p class=MsoNormal></p> </td> <td width=360 valign=top style="width:3.75in;padding:0in 5.4pt 0in 5.4pt; height:1.0in"> <h2>For:</h2> <p class=MsoNormal>'.$row['ts_client'].'</p> <p class=MsoNormal>#'.$fl.date('mdy').rand(31,69).'</p> </td> </tr> </table> </div> <p class=MsoNormal> </p> <p class=MsoNormal> </p>'; $str .= ' <div align=center> <table class=MsoNormalTable border=1 cellspacing=0 cellpadding=0 width=720 style="width:7.5in;border-collapse:collapse;border:none"> <tr height=20 class="headrow"> <td width="66%"><p class=Columnheading>Description:</p></td> <td width="12%"><p class=Columnheading>Hours:</p></td> <td width="12%"><p class=Columnheading>Rate:</p></td> <td width="12%"><p class=Columnheading>Amount:</p></td> </tr>'; $prevclient = $row['ts_client']; } $ts=$row['totalsecs']/3600; $str .= ' <tr> <td>'.$row['ts_dscr'].'</td> <td>'.round($ts,2) .'</td> <td>$37.75</td> <td>'.round($ts * 37.75, 2) .'</td> </tr>'; $gt += $row['totalsecs']; $gtt += $row['totalsecs']; $c++; $t++; } //tidy up the last table for($w=0; $w < 17 - $t; $w++){ $str .= ' <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr>'; } $n=$gt/3600; $str .= ' <tr style="page-break-inside:avoid;height:.2in"> <td width=414 valign=top style="width:310.5pt;border:none;padding:2.15pt 5.75pt 2.15pt 5.75pt; height:.2in"> <p class=MsoNormal> </p> </td> <td width=204 colspan=2 style="width:153.0pt;border:none;border-right:solid windowtext 1.0pt; padding:2.15pt .15in 2.15pt .15in;height:.2in"> <p class=RightAligned>TOTAL</p> </td> <td width=102 style="width:76.5pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:2.15pt .15in 2.15pt .15in;height:.2in"> <p class=Amount>$'.round($n * 37.75, 2) .'</p> </td> </tr> </table> </div> </body> </html> '."\n"; fwrite($fp, $str); fclose($fp); } ?> but this code send me these warnings: Warning: fopen("Darrenholden-com-invoice_10-28-2008.doc") [function.fopen]: failed to open stream: Invalid argument in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 426 Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 405 Warning: fclose(): supplied argument is not a valid stream resource in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 407 Warning: fopen("Diecispa-com-invoice_10-28-2008.doc") [function.fopen]: failed to open stream: Invalid argument in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 426 Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 405 Warning: fclose(): supplied argument is not a valid stream resource in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 407 Warning: fopen("Marclazar-com-invoice_10-28-2008.doc") [function.fopen]: failed to open stream: Invalid argument in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 426 Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 405 Warning: fclose(): supplied argument is not a valid stream resource in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 407 Warning: fopen("mywebpage-com-invoice_10-28-2008.doc") [function.fopen]: failed to open stream: Invalid argument in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 426 Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 641 Warning: fclose(): supplied argument is not a valid stream resource in C:\xampp\htdocs\andy\templates\ja_purity\invoiceForm.php on line 643 What could be wrong?? I tested the code with just writing to straight html first - and every thing worked fine, but once I added the fopen() etc - then no good? Any help?? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 28, 2008 Share Posted October 28, 2008 What php version? Quote Link to comment Share on other sites More sharing options...
samoht Posted October 29, 2008 Author Share Posted October 29, 2008 PHP Version 5.2.5 Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 found it. replace this: $filename = '"'.$cname.'-invoice_'.date('m-j-Y').'.doc"'; with this: $filename = $cname.'-invoice_'.date('m-j-Y').'.doc'; you were trying to open a file called "something-invoice_10-29-2008.doc" but what you actually want to open is a file called something-invoice_10-29-2008.doc (without the surrounding " ") Quote Link to comment Share on other sites More sharing options...
samoht Posted October 29, 2008 Author Share Posted October 29, 2008 Great!! Thanks for that bobbinsbro! It works just fine creating all the WORD doc invoices. now I just need to collect all those just created (Probably by looking for files with "invoice_m-d-Y.doc" - as long as I only run the script once in a day) and send them as attachments in my email. Quote Link to comment Share on other sites More sharing options...
samoht Posted October 29, 2008 Author Share Posted October 29, 2008 Does anyone have an idea how to query a directory and include files as an attachment? I would settle for query of the directory for the files at this point. Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 tell me if i get it - you want to make an automated spell-checker for your attachments? i'm sure i saw something about dictionaries lately... i'll try and find it again. Quote Link to comment Share on other sites More sharing options...
samoht Posted October 29, 2008 Author Share Posted October 29, 2008 No not an automated spell checker - I want to email the WORD doc files that I create as attachments So the code loops through my db and organizes my info by client and creates a separate invoice for each client as a WORD doc - now I need to gather all those files up and make them attachments to my 1 email. Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 i hope this doesn't make you re-write your code, but i just found this: http://phpmailer.codeworxtech.com/ and it's free, and it looks to me like you can attach unlimited amount of files by using this class. check out the "methods" and "examples" pages. hope this helps. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.