Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Form to Excel then send as email attachment


PHP Form to Excel then send as email attachment

Reply
  #1 (permalink)  
Old 09-28-05, 08:58 AM
Moloch Moloch is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Form to Excel then send as email attachment

Hi there,

I have a little situation. I am working on a form, basically, that grades people. Now, I got the form made, my outputs are ok. However, there is something I was my form to do, which I think is possible, but is a major road block for me.

Currently, my form does a POST in an Excel file, which is saved with the values that I want.

But I need this file to be send via email to recipient, CC to the user logged in and the part that I am confuse is to send the XLS file as attachment. Is that possible and if so, can someone guide me on how to get my outputted excel form in an attachment ready to send.

Thanks for your help.
Reply With Quote
  #2 (permalink)  
Old 09-28-05, 10:28 AM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
I am a little confused.

Do you currently have the script creating the excel file on the server in some folder?

If so, I posted a little mailer class that can easily attach files a few days ago: http://www.programmingtalk.com/showthread.php?t=22506

That would probably be helpful to you in attaching the file to the email and sending it. The addFile() method of that class accepts the path to the file.
Reply With Quote
  #3 (permalink)  
Old 09-28-05, 10:38 AM
Moloch Moloch is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply.

I use the header feature to load my POST data from my form to display it in Excel, however, it prompt's the user to either save it to HD or open it.

Code:
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition:filename=$agent-$userid.xls");
So basically, I am searching for something that might be able to send the newly created xls file, without having to save it on the server and simply mail it to the specified address .

thanks again, and in the mean time, I will check your post to see if I can do something with it.

cheers
Reply With Quote
  #4 (permalink)  
Old 09-28-05, 01:47 PM
Moloch Moloch is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Pat

I've added more information to better help you or others to understand my situation. I have also verified the links u supplied and I could find what I really wanted to do. So here goes :-)


I am trying to create a grading sheet. Well, I've done it, it calculates and post my necesary information.

My problem is, I currently have setup to prompt the user to save or open the Excel .Xls file that it generates. However, I've been trying to search all over the place for my result, to send an email to a specific recipient with attached to output that it creates in Xls instead of saving the file.

In other words:

- fill out the form
- click submit
- then it sends email with the .xls file without prompting the user to save it or without storing it on the server(if possible)

I've been suggested to use phpmail which I don't think will do the trick as I am not taking a file from the HD just the post data that's been converted to xls and send via email as attachment. If there is a way though with phpmail, can someone guide me on how to setup this particular feature.

if you want to see what it's currently doing, please visit http://moloch.ca/php/testing/form.html

Also, this is my code for the PHP file ( result.php ) if it may help:

Thanks for your help.
Brian

Code:
<?php
$agent = $_POST['agent'];
$salutation = $_POST['salutation'];
$transition = $_POST['transition'];
$loyalty = $_POST['loyalty'];
$solving = $_POST['solving'];
$rapport = $_POST['rapport'];
$footer = $_POST['footer'];
$resolution = $_POST['resolution'];
$userid = $_POST['userid'];
$lob = $_POST['lob'];
$internal = $_POST['internal'];
$procedures = $_POST['procedures'];
$spelling = $_POST['spelling'];
$grammar = $_POST['grammar'];
$punctuation = $_POST['punctuation'];
$msgcopier = $_POST['msgcopier'];
$bonus = $_POST['bonus'];
$today = date('d-M-y');
$notesq1 = $_POST['notesq1'];
$notesq2 = $_POST['notesq2'];
$notesq3 = $_POST['notesq3'];
$notesq4 = $_POST['notesq4'];
$notesq5 = $_POST['notesq5'];
$notesq6 = $_POST['notesq6'];
$notesq7 = $_POST['notesq7'];
$notesq8 = $_POST['notesq8'];
$notesq9 = $_POST['notesq9'];
$notesq10 = $_POST['notesq10'];
$notesq11 = $_POST['notesq11'];
$notesq12 = $_POST['notesq12'];

$total = $salutation + $transition + $loyalty + $solving + $rapport + $footer + $resolution + $internal + $procedures + $spelling + $grammar + $punctuation + $bonus ;


header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: filename=$agent-$userid.xls");



?>
<html>
<head><title>Grading Sheet Result</title>
</head>
<body>
<body>
<center>
<table border=1 width=1000>

<tr><td align=center bgcolor=blue colspan="4"><b><font color=yellow>CONTACT ASSESSMENT GRADING SHEET  RESULTS</font></b>
</td></tr>

<tr><td width=230><b>Name: </b></td><td width=150><?php echo $agent; ?></td>

<td width=120>
<b>Case</b>
</td>
<td><?php echo $userid; ?></td>
</td>

</tr>
<tr><td> Graded on </td><td><?php echo $today; ?></td>

<td>
<b>Business</b>
</td>
<td><?php echo $lob; ?></td>
</td>
</tr>

<tr height="50"><td align=top><b> Salutation (header): </b></td><td><b><center> <?php echo $salutation; ?></center></b></td>
<td colspan="2" align="top"> <div STYLE=" height: 50px; width:600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq1; ?></div>
</td>
</tr>

<tr height="50"><td><b> Transition: </b></td><td><b><center><?php echo $transition; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq2; ?></div>
</td>
</tr>

<tr height="50"><td><b> Loyalty close (footer): </b></td><td><b><center><?php echo $footer; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq3; ?></div>
</td>
</tr>

<tr height="50"><td><b> Delivery: </b></td><td><b><center><?php echo $delivery; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq4; ?></div>
</td>
</tr>		

<tr height="50"><td><b> Rapport Building:  </b></td><td><b><center><?php echo $rapport; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq5; ?></div>
</td>
</tr>		

<tr height="50"><td><b> Problem solving: </b></td><td><b><center><?php echo $solving; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq6; ?></div>
</td>
</tr>

<tr height="50"><td><b> One contact Resolution: </b></td><td><b><center><?php echo $resolution; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq7; ?></div>
</td>
</tr>

<tr height="50"><td><b> Internal policies and procedures: </b></td><td><b><center><?php echo $procedures; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq8; ?></div>
</td>
</tr>

<tr height="50"><td><b> Revenue (bonus points): </b></td><td><b><center><?php echo $bonus; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq9; ?></div>
</td>
</tr>

<tr height="50">
<td> <b>Spelling errors: </b></td><td><b><center><?php echo $spelling; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq10; ?></div>
</td>
</tr>

<tr height="50">
<td> <b>Grammar: </b></td><td><b><center><?php echo $grammar; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq11; ?></div>
</td>
</tr>

<tr height="50"><td><b> Punctuation: </b></td><td><b><center><?php echo $punctuation; ?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq12; ?></div>
</td>
</tr>

<tr><td><b>Total: </b></td><td><b><center><font color="blue"><?php echo $total; ?></font> / 100</center></b></td>

</tr>

<tr>
<td colspan="4" witdh="1000">
<b>Copy of message:</b>  <?php echo $msgcopier; ?>
</td>

</tr>

</table>

</body></html>

<?php ?>
Reply With Quote
  #5 (permalink)  
Old 09-28-05, 01:52 PM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
Well, you should still be able to use the class I linked to or at least take parts of it and use it.

Here is a modification that should work for you:

PHP Code:

class EPDEV_Emailer

{
    var 
$message;
    var 
$FILES;
    var 
$EMAIL;

    function 
EPDEV_Emailer($to_address$from_address$subject$reply_address=null$mailer=null$custom_header=null)
    {
        
$this->EMAIL = array(
            
"to" => $to_address,
            
"from" => $from_address,
            
"subject" => $subject,
            
"reply" => (empty($reply_address) ? $from_address $reply_address),
            
"mailer" => (empty($mailer) ? "X-Mailer: PHP/" phpversion() : $mailer),
            
"header" => (empty($custom_header) ? "" $custom_header),
            
"boundary" => "_mimeboundary_".md5(uniqid(mt_rand(), 1))
            );

        
$this->message "";

        
$this->FILES = array();
    }

    function 
addFile($filename$type=null$filecontents=null)
    {
        if (
$filecontents !== null)
        {
            
$index count($this->FILES);
            
$this->FILES[$index]['data'] = chunk_split(base64_encode($filecontents));
            
$this->FILES[$index]['name'] = basename($filename);

            if (empty(
$type))
                
$this->FILES[$index]['mime'] = mime_content_type($filename);
            else
                
$this->FILES[$index]['mime'] = $type;
        }
        else if (
file_exists($filename))
        {
            
$index count($this->FILES);
            
$this->FILES[$index]['data'] = chunk_split(base64_encode(file_get_contents($filename)));
            
$this->FILES[$index]['name'] = basename($filename);

            if (empty(
$type))
                
$this->FILES[$index]['mime'] = mime_content_type($filename);
            else
                
$this->FILES[$index]['mime'] = $type;
        }
        else
        {
            
$this->Error_Handle("File specified -- {$filename} -- does not exist.");
        }
    }



    function 
addText($text)
    {
        
$this->message .= $text;
    }


    function 
getHeader()
    {
        
$header "From: {$this->EMAIL['from']}\r\n"
                
"Reply-To: {$this->EMAIL['reply']}\r\n"
                
"X-Mailer: {$this->EMAIL['mailer']}\r\n"
                
"MIME-Version: 1.0\r\n"
                
"Content-Type: multipart/mixed; boundary=\"{$this->EMAIL['boundary']}\";\r\n";

        return 
$header;
    }
    
    function 
getEmail()
    {    
        
$content .= "--{$this->EMAIL['boundary']}\r\n"
                
"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
                
"Content-Transfer-Encoding: 7bit\r\n\r\n"
                
$this->message "\r\n";

        if (!empty(
$this->FILES))
        {
            foreach(
$this->FILES as $file)
            {
                
$content .= "--{$this->EMAIL['boundary']}\r\n"
                
"Content-Type: {$file['mime']}; name=\"{$file['name']}\"\r\n"
                
"Content-Transfer-Encoding: base64\r\n"
                
"Content-Disposition: attachment\r\n\r\n"
                
$file['data'] . "\r\n";
            }
        }

        
$content .= "--{$this->EMAIL['boundary']}--\r\n";


        return 
$content;
    }

    function 
send()
    {
        
$result mail($this->EMAIL['to'], $this->EMAIL['subject'], $this->getEmail(), $this->getHeader());

        if (!
$result)
            
$this->Error_Handle("The email failed to send.");
    }


    function 
Error_Handle($error)
    {
        die(
$error);
    }

And then example code would be:
PHP Code:

<?php


// include the class
include("mailer.php");


// your email
$recipient "myemail@mydomain.com";

// person sending it
$from "me@mydomain.com";

// subject
$subject "Subject of email";


// email message
$message "
Some email message containing whatever.

This is the text part of the email.
            "
;


// initialize email object ($to_address, $from_address, $subject, $reply_address=null, $mailer=null, $custom_header=null)
$myEmail = new EPDEV_Emailer($recipient$from$subject);


// Add the message to the email
$my_email->addText($message);

// add the file to the email ($filename, $type=null, $filecontents=null)
// NOTE: If filecontents is left out, filename is assumed to be path to file.
//         If filecontents is included, filename is only used as the name of file.
//            and filecontents is used as the content of file.
$myEmail->addFile("{$agent}-{$userid}.xls"""$FILE_CONTENTS);

// actually send out the email
$my_email->send();
With that example, you would need to define $FILE_CONTENTS as the excel file contents. It also uses the $agent, $userid variables that you were using in the header.
Reply With Quote
  #6 (permalink)  
Old 09-28-05, 01:58 PM
Moloch Moloch is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Patiek I'll try it out and give you some news. As a php learner, there's still a lot of commands that I need to learn in PHP.
Reply With Quote
  #7 (permalink)  
Old 09-28-05, 02:17 PM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
Here we go then. This is easily accomplished with output buffering.

Here is an example of how your script would look:

PHP Code:

<?php

// start output buffering.
// This will make it so no ouput goes to browser until/if we allow it to.
ob_start();

$agent $_POST['agent'];
$salutation $_POST['salutation'];
$transition $_POST['transition'];
$loyalty $_POST['loyalty'];
$solving $_POST['solving'];
$rapport $_POST['rapport'];
$footer $_POST['footer'];
$resolution $_POST['resolution'];
$userid $_POST['userid'];
$lob $_POST['lob'];
$internal $_POST['internal'];
$procedures $_POST['procedures'];
$spelling $_POST['spelling'];
$grammar $_POST['grammar'];
$punctuation $_POST['punctuation'];
$msgcopier $_POST['msgcopier'];
$bonus $_POST['bonus'];
$today date('d-M-y');
$notesq1 $_POST['notesq1'];
$notesq2 $_POST['notesq2'];
$notesq3 $_POST['notesq3'];
$notesq4 $_POST['notesq4'];
$notesq5 $_POST['notesq5'];
$notesq6 $_POST['notesq6'];
$notesq7 $_POST['notesq7'];
$notesq8 $_POST['notesq8'];
$notesq9 $_POST['notesq9'];
$notesq10 $_POST['notesq10'];
$notesq11 $_POST['notesq11'];
$notesq12 $_POST['notesq12'];

$total $salutation $transition $loyalty $solving $rapport $footer $resolution $internal $procedures $spelling $grammar $punctuation $bonus ;


?>
<html>
<head><title>Grading Sheet Result</title>
</head>
<body>
<body>
<center>
<table border=1 width=1000>

<tr><td align=center bgcolor=blue colspan="4"><b><font color=yellow>CONTACT ASSESSMENT GRADING SHEET  RESULTS</font></b>
</td></tr>

<tr><td width=230><b>Name: </b></td><td width=150><?php echo $agent?></td>

<td width=120>
<b>Case</b>
</td>
<td><?php echo $userid?></td>
</td>

</tr>
<tr><td> Graded on </td><td><?php echo $today?></td>

<td>
<b>Business</b>
</td>
<td><?php echo $lob?></td>
</td>
</tr>

<tr height="50"><td align=top><b> Salutation (header): </b></td><td><b><center> <?php echo $salutation?></center></b></td>
<td colspan="2" align="top"> <div STYLE=" height: 50px; width:600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq1?></div>
</td>
</tr>

<tr height="50"><td><b> Transition: </b></td><td><b><center><?php echo $transition?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq2?></div>
</td>
</tr>

<tr height="50"><td><b> Loyalty close (footer): </b></td><td><b><center><?php echo $footer?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq3?></div>
</td>
</tr>

<tr height="50"><td><b> Delivery: </b></td><td><b><center><?php echo $delivery?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq4?></div>
</td>
</tr>        

<tr height="50"><td><b> Rapport Building:  </b></td><td><b><center><?php echo $rapport?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq5?></div>
</td>
</tr>        

<tr height="50"><td><b> Problem solving: </b></td><td><b><center><?php echo $solving?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq6?></div>
</td>
</tr>

<tr height="50"><td><b> One contact Resolution: </b></td><td><b><center><?php echo $resolution?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq7?></div>
</td>
</tr>

<tr height="50"><td><b> Internal policies and procedures: </b></td><td><b><center><?php echo $procedures?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq8?></div>
</td>
</tr>

<tr height="50"><td><b> Revenue (bonus points): </b></td><td><b><center><?php echo $bonus?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq9?></div>
</td>
</tr>

<tr height="50">
<td> <b>Spelling errors: </b></td><td><b><center><?php echo $spelling?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq10?></div>
</td>
</tr>

<tr height="50">
<td> <b>Grammar: </b></td><td><b><center><?php echo $grammar?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq11?></div>
</td>
</tr>

<tr height="50"><td><b> Punctuation: </b></td><td><b><center><?php echo $punctuation?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq12?></div>
</td>
</tr>

<tr><td><b>Total: </b></td><td><b><center><font color="blue"><?php echo $total?></font> / 100</center></b></td>

</tr>

<tr>
<td colspan="4" witdh="1000">
<b>Copy of message:</b>  <?php echo $msgcopier?>
</td>

</tr>

</table>

</body></html>

<?php

// get all of the content output from above (remember, it hasn't been sent to browser yet). 
$FILE_CONTENTS ob_get_contents();

// USE THE FOLLOWING IF YOU DO NOT WANT TO ALLOW IMMEDIATE DOWNLOAD OF FILE:
ob_clean();

// OTHERWISE, USE THE FOLLOWING IF YOU STILL WANT TO ALLOW FOR DOWNLOAD OF FILE:

// (uncomment the two lines below and remove the ob_clean() from above).
// Remember, we can still send headers because we haven't sent anything to the browser yet.

//header("Content-Type: application/vnd.ms-excel");
//header("Content-Disposition: filename=$agent-$userid.xls");

// All that example mail stuff that I posted goes down here after this line:


// include the class
include("mailer.php");


// your email
$recipient "myemail@mydomain.com";

// person sending it
$from "me@mydomain.com";

// subject
$subject "Subject of email";


// email message
$message "
Some email message containing whatever.

This is the text part of the email.
            "
;


// initialize email object ($to_address, $from_address, $subject, $reply_address=null, $mailer=null, $custom_header=null)
$myEmail = new EPDEV_Emailer($recipient$from$subject);


// Add the message to the email
$my_email->addText($message);

// add the file to the email ($filename, $type=null, $filecontents=null)
// NOTE: If filecontents is left out, filename is assumed to be path to file.
//         If filecontents is included, filename is only used as the name of file.
//            and filecontents is used as the content of file.
$myEmail->addFile("{$agent}-{$userid}.xls"""$FILE_CONTENTS);

// actually send out the email
$my_email->send();

?>
Notice the couple extra lines of code taking care of output buffering. There is one at the top to start it, ob_start(), and then a few more after all of your output that tell php what to do with the output.

In this manner you can either:
A) Both send an email and allow for download from browser.
- or -
B) Send an email and not allow for download but instead display a message.

I also included the example code I said to use earlier with my mailer class. You will need to modify the recipient, etc. variables as you see fit. But this should give you that which you wanted.

A couple of things to keep in mind:
If you do not do anything with the output buffer then it is sent to the browser when the script ends.

In the example above, we issued a ob_clean() which deleted everything in the buffer. As a result, if you left it the way it is right now, nothing would be sent to the browser (blank page). Therefore, you may want to add a message or something at the end of the script that confirms the email was sent.

If you want to still send the file (in other words, both send the file to the browser AND email the file) then you can just delete ob_clean() and uncomment the two header(...) lines that are just below ob_clean in the code above.

Be sure to look at the php website for more output control functions.

Last edited by Patiek; 09-28-05 at 02:28 PM.
Reply With Quote
  #8 (permalink)  
Old 09-29-05, 11:36 AM
Moloch Moloch is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Pat,

I tried your mailing.php addition to my form and also took your modifications however, It does not seem to contine, I get a

File specified -- name2-23445.xls -- does not exist.

Also, I tried set it up for download the attachement directly for a try, just like u left in the instructions and the excel file that loads, only shows the same

File specified -- name2-23445.xls -- does not exist.

Then I tried to check your mailer scrip and I noticed that in the Header section of your script,

Current test form

Code:
    function getEmail()
    {    
        $content .= "--{$this->EMAIL['boundary']}\r\n"
                . "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
                . "Content-Transfer-Encoding: 7bit\r\n\r\n"
                . $this->message . "\r\n";

        if (!empty($this->FILES))
        {
            foreach($this->FILES as $file)
            {
                $content .= "--{$this->EMAIL['boundary']}\r\n"
                . "Content-Type: {$file['mime']}; name=\"{$file['name']}\"\r\n"
                . "Content-Transfer-Encoding: base64\r\n"
                . "Content-Disposition: attachment\r\n\r\n"
                . $file['data'] . "\r\n";
            }
        }

        $content .= "--{$this->EMAIL['boundary']}--\r\n";


        return $content;
Now, I don't know if the content-type needs to match the excell content or not. Like I said, I am quite noob in these commands, and am still going to google you commands, to see if It can accept the xls file.

thanks again for the help, it's been very appreciated
Reply With Quote
  #9 (permalink)  
Old 09-29-05, 03:20 PM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

No one has noticed that but you are generating an HTML file, not the Excel file! The ob_get_contents() will return you html code, not the Excel!
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #10 (permalink)  
Old 09-29-05, 03:49 PM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
I saw that but it really doesn't matter.

Excel is going to pick up the cells, etc, of the html and reformat it correctly the next time it is saved (if it is saved). If it isn't saved then it doesn't matter anyway.

There are a couple of typos in the stuff I posted.

I just fixed it and this IS working on my servers:

result.php:

PHP Code:

<?php

error_reporting
(E_ALL E_NOTICE);

// start output buffering.
// This will make it so no ouput goes to browser until/if we allow it to.
ob_start();

$agent $_POST['agent'];
$salutation $_POST['salutation'];
$transition $_POST['transition'];
$loyalty $_POST['loyalty'];
$solving $_POST['solving'];
$rapport $_POST['rapport'];
$footer $_POST['footer'];
$resolution $_POST['resolution'];
$userid $_POST['userid'];
$lob $_POST['lob'];
$internal $_POST['internal'];
$procedures $_POST['procedures'];
$spelling $_POST['spelling'];
$grammar $_POST['grammar'];
$punctuation $_POST['punctuation'];
$msgcopier $_POST['msgcopier'];
$bonus $_POST['bonus'];
$today date('d-M-y');
$notesq1 $_POST['notesq1'];
$notesq2 $_POST['notesq2'];
$notesq3 $_POST['notesq3'];
$notesq4 $_POST['notesq4'];
$notesq5 $_POST['notesq5'];
$notesq6 $_POST['notesq6'];
$notesq7 $_POST['notesq7'];
$notesq8 $_POST['notesq8'];
$notesq9 $_POST['notesq9'];
$notesq10 $_POST['notesq10'];
$notesq11 $_POST['notesq11'];
$notesq12 $_POST['notesq12'];

$total $salutation $transition $loyalty $solving $rapport $footer $resolution $internal $procedures $spelling $grammar $punctuation $bonus ;


?>
<html>
<head><title>Grading Sheet Result</title>
</head>
<body>
<body>
<center>
<table border=1 width=1000>

<tr><td align=center bgcolor=blue colspan="4"><b><font color=yellow>CONTACT ASSESSMENT GRADING SHEET  RESULTS</font></b>
</td></tr>

<tr><td width=230><b>Name: </b></td><td width=150><?php echo $agent?></td>

<td width=120>
<b>Case</b>
</td>
<td><?php echo $userid?></td>
</td>

</tr>
<tr><td> Graded on </td><td><?php echo $today?></td>

<td>
<b>Business</b>
</td>
<td><?php echo $lob?></td>
</td>
</tr>

<tr height="50"><td align=top><b> Salutation (header): </b></td><td><b><center> <?php echo $salutation?></center></b></td>
<td colspan="2" align="top"> <div STYLE=" height: 50px; width:600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq1?></div>
</td>
</tr>

<tr height="50"><td><b> Transition: </b></td><td><b><center><?php echo $transition?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq2?></div>
</td>
</tr>

<tr height="50"><td><b> Loyalty close (footer): </b></td><td><b><center><?php echo $footer?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq3?></div>
</td>
</tr>

<tr height="50"><td><b> Delivery: </b></td><td><b><center><?php echo $delivery?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq4?></div>
</td>
</tr>        

<tr height="50"><td><b> Rapport Building:  </b></td><td><b><center><?php echo $rapport?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq5?></div>
</td>
</tr>        

<tr height="50"><td><b> Problem solving: </b></td><td><b><center><?php echo $solving?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq6?></div>
</td>
</tr>

<tr height="50"><td><b> One contact Resolution: </b></td><td><b><center><?php echo $resolution?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq7?></div>
</td>
</tr>

<tr height="50"><td><b> Internal policies and procedures: </b></td><td><b><center><?php echo $procedures?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq8?></div>
</td>
</tr>

<tr height="50"><td><b> Revenue (bonus points): </b></td><td><b><center><?php echo $bonus?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq9?></div>
</td>
</tr>

<tr height="50">
<td> <b>Spelling errors: </b></td><td><b><center><?php echo $spelling?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq10?></div>
</td>
</tr>

<tr height="50">
<td> <b>Grammar: </b></td><td><b><center><?php echo $grammar?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq11?></div>
</td>
</tr>

<tr height="50"><td><b> Punctuation: </b></td><td><b><center><?php echo $punctuation?></center></b></td>
<td colspan="2"> <div STYLE=" height: 50px; width: 600px; overflow: auto;">
<b>Notes:</b> <?php echo $notesq12?></div>
</td>
</tr>

<tr><td><b>Total: </b></td><td><b><center><font color="blue"><?php echo $total?></font> / 100</center></b></td>

</tr>

<tr>
<td colspan="4" witdh="1000">
<b>Copy of message:</b>  <?php echo $msgcopier?>
</td>

</tr>

</table>

</body></html>

<?php

// get all of the content output from above (remember, it hasn't been sent to browser yet). 
$FILE_CONTENTS ob_get_contents();

// USE THE FOLLOWING IF YOU DO NOT WANT TO ALLOW IMMEDIATE DOWNLOAD OF FILE:
ob_clean();

// OTHERWISE, USE THE FOLLOWING IF YOU STILL WANT TO ALLOW FOR DOWNLOAD OF FILE:

// (uncomment the two lines below and remove the ob_clean() from above).
// Remember, we can still send headers because we haven't sent anything to the browser yet.

//header("Content-Type: application/vnd.ms-excel");
//header("Content-Disposition: filename=$agent-$userid.xls");

// All that example mail stuff that I posted goes down here after this line:


// include the class
include("mailer.php");


// your email
$recipient "useremail@domain.com";

// person sending it
$from "webmasteremail@domain.com";

// subject
$subject "Subject of email";


// email message
$message "
Some email message containing whatever.

This is the text part of the email.
            "
;


// initialize email object ($to_address, $from_address, $subject, $reply_address=null, $mailer=null, $custom_header=null)
$myEmail = new EPDEV_Emailer($recipient$from$subject);


// Add the message to the email
$myEmail->addText($message);

// add the file to the email ($filename, $type=null, $filecontents=null)
// NOTE: If filecontents is left out, filename is assumed to be path to file.
//         If filecontents is included, filename is only used as the name of file.
//            and filecontents is used as the content of file.
$myEmail->addFile("{$agent}-{$userid}.xls""application/vnd.ms-excel"$FILE_CONTENTS);

// actually send out the email
$myEmail->send();

?>

Message sent.
and mailer.php:
PHP Code:

<?php


class EPDEV_Emailer
{
    var 
$message;
    var 
$FILES;
    var 
$EMAIL;

    function 
EPDEV_Emailer($to_address$from_address$subject$reply_address=null$mailer=null$custom_header=null)
    {
        
$this->EMAIL = array(
            
"to" => $to_address,
            
"from" => $from_address,
            
"subject" => $subject,
            
"reply" => (empty($reply_address) ? $from_address $reply_address),
            
"mailer" => (empty($mailer) ? "X-Mailer: PHP/" phpversion() : $mailer),
            
"header" => (empty($custom_header) ? "" $custom_header),
            
"boundary" => "_mimeboundary_".md5(uniqid(mt_rand(), 1))
            );

        
$this->message "";

        
$this->FILES = array();
    }

    function 
addFile($filename$type=null$filecontents=null)
    {
        if (
$filecontents !== null)
        {
            
$index count($this->FILES);
            
$this->FILES[$index]['data'] = chunk_split(base64_encode($filecontents));
            
$this->FILES[$index]['name'] = basename($filename);

            if (empty(
$type))
                
$this->FILES[$index]['mime'] = mime_content_type($filename);
            else
                
$this->FILES[$index]['mime'] = $type;
        }
        else if (
file_exists($filename))
        {
            
$index count($this->FILES);
            
$this->FILES[$index]['data'] = chunk_split(base64_encode(file_get_contents($filename)));
            
$this->FILES[$index]['name'] = basename($filename);

            if (empty(
$type))
                
$this->FILES[$index]['mime'] = mime_content_type($filename);
            else
                
$this->FILES[$index]['mime'] = $type;
        }
        else
        {
            
$this->Error_Handle("File specified -- {$filename} -- does not exist.");
        }
    }



    function 
addText($text)
    {
        
$this->message .= $text;
    }


    function 
getHeader()
    {
        
$header "From: {$this->EMAIL['from']}\r\n"
                
"Reply-To: {$this->EMAIL['reply']}\r\n"
                
"X-Mailer: {$this->EMAIL['mailer']}\r\n"
                
"MIME-Version: 1.0\r\n"
                
"Content-Type: multipart/mixed; boundary=\"{$this->EMAIL['boundary']}\";\r\n";

        return 
$header;
    }
    
    function 
getEmail()
    {    
        
$content .= "--{$this->EMAIL['boundary']}\r\n"
                
"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
                
"Content-Transfer-Encoding: 7bit\r\n\r\n"
                
$this->message "\r\n";

        if (!empty(
$this->FILES))
        {
            foreach(
$this->FILES as $file)
            {
                
$content .= "--{$this->EMAIL['boundary']}\r\n"
                
"Content-Type: {$file['mime']}; name=\"{$file['name']}\"\r\n"
                
"Content-Transfer-Encoding: base64\r\n"
                
"Content-Disposition: attachment\r\n\r\n"
                
$file['data'] . "\r\n";
            }
        }

        
$content .= "--{$this->EMAIL['boundary']}--\r\n";


        return 
$content;
    }

    function 
send()
    {
        
$result mail($this->EMAIL['to'], $this->EMAIL['subject'], $this->getEmail(), $this->getHeader());

        if (!
$result)
            
$this->Error_Handle("The email failed to send.");
    }


    function 
Error_Handle($error)
    {
        die(
$error);
    }
}

I have successfully sent a file to myself and opened it up in excel.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
php form script to more then one email? burnttoast PHP 9 07-15-05 09:57 AM
PHP email form problem jcwilde1 PHP 2 06-18-05 11:19 PM
send email when sending form (asp) bmatth1 Script Requests 0 09-30-03 05:52 AM


All times are GMT -5. The time now is 06:13 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.