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.
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.
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.
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.
// 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.
// 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.
// 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.
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,
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
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();
// 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.
// 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;