Email PHP output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

Email PHP output

Post by micknc »

I have a script that converts a mysql table to xls for users who would like to use it on our site.

Here is the code

Code: Select all

 
<?php
 
include 'availconfig.php';
 
include 'availopendb.php';
 
 
 
$fileName = 'North_Carolina_Farms_Inc_Availabiity.xls';
 
header("Content-type: application/vnd.ms-excel");
 
header("Content-Disposition: attachment; filename=$fileName");
 
 
 
 
 
$query  = "SELECT * FROM avail";
 
$result = mysql_query($query) or die('Error, query failed');
 
 
 
$tsv  = array();
 
$html = array();
 
while($row = mysql_fetch_array($result, MYSQL_NUM))
 
{
 
   $tsv[]  = implode("\t", $row);
 
   $html[] = "<tr><td>" .implode("</td><td>", $row) .              "</td></tr>";
 
}
 
 
 
$tsv = implode("\r\n", $tsv);
 
$html = "<table>" . implode("\r\n", $html) . "</table>";
 
 
 
 
 
echo $tsv;
 
//echo $html;
 
 
 
include 'availclosedb.php';
 
?>
 
 
 
Lots of people call wanting this emailed and faxed (just need to save as txt file to a directory and my fax server will take it from there) to them so I am working on a couple of scripts that would allow me to submit a form from our office that would run the script and email as an attachment (or save as txt). I have been playing with it but so far I have nothing that will work.
Does anyone know what direction I should go?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Email PHP output

Post by Christopher »

Check out SwiftMailer. It will make emailing an attachment easy.
(#10850)
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

Re: Email PHP output

Post by micknc »

I am going to look into it. Thanks,

On a related note I have been trying to just recreate the output inside of an html email but so far all I can get is the last result to show in the email. Here is the code:

Code: Select all

 
if ($type = 1) {
$mail->Subject = "Your requested Unrooted Availabilty from North Carolina Farms Inc";
$query = "select * FROM avail WHERE Type=2";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    // HTML body
    $body = "" . $row["name"] . "" . $row["one"] . "" . $row["two"] . "";
    }
    // Plain text body (for mail clients that cannot read HTML)
    $text_body  = "will have text here";
  } elseif ($type = 2) {
   } else {
   }
 
    $mail->Body    = $body;
    $mail->AltBody = $text_body;
        $mail->AddAddress($add);
 
if(!$mail->Send()) {
  echo "<font color='ffffff' face='arial'>Failed to send mail.  Check your address again.";
} else {
  echo "<font color='ffffff' face='arial'>Availability sent to '$add'";
}
 
I didn't throw in the headers and everything because everything is working fine except that it only reports the last entry that matches the select statement.
Any ideas?
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

Re: Email PHP output

Post by micknc »

Another solution that I have found is a script that was written by someone at planteozh.

It does a great job. If you have problems with not being able to load the file just make sure your dir is relative (../dir) and not a full url and chmod (777).

Here is the link:

http://planetozh.com/blog/my-projects/ ... tachment/

I am off to adapt it to my needs now.
Thanks for the suggestions.

I little update if any stumbles on to this post looking for an email solution. This is a code that I found emails the executed page as attached variable (works will phpmailer and swiftmailer)

http://www.beginnercode.com/index.php/ ... hpmailer/
Post Reply