Log in

View Full Version : is it possible to send e-mail w/ php in it?



cow_manuver
05-02-2006, 03:57 PM
Hi, I'm kinda new at this, so forgive me if there is an easy way to do this. I have been searching the internet high and low for the past 3 days trying to find out how I might be able to do this, but I find no references about sending a PHP e-mail.

I have the working script to generate a quote result. The php basically checks to see if there is a value and if not, it won't print that table, if yes, then it will print the table.

I'm wanting to know if I can send php in a html e-mail from the form. I basically want the e-mail to look like the web-page generated.

Here is the form:

http://www.enwassociates.org/php/quote-form2.html

Twey
05-02-2006, 04:16 PM
No. However, you can send the HTML output of the PHP.

djr33
05-02-2006, 07:17 PM
PHP must be INTERPRETED server side. You can't have that happen in an email.

Send a link, or, as Twey says, outputted html from php.


Arguably, you could use an iframe or something, but I doubt that would be compatible with many or any email viewers.

cow_manuver
05-02-2006, 09:16 PM
No. However, you can send the HTML output of the PHP.


Twey is this a simple procedure? like a one line code thing? Can you point me in the right direction please? I'm not lazy, I just need the right info, I don't know what to search for on that... but i'll try looking, I wasn't sure before, but now that I know, maybe I can find something...

Twey
05-02-2006, 09:58 PM
Not unless you post the PHP you would try to send, no.

djr33
05-02-2006, 10:59 PM
PHP outputs html. That's WHAT it does... nothing more, nothing less. (Plus, I guess, cookies and serverside stuff.)


What you do is store *whatever you want* into a variable.

Then use the mail() command.

Then as the body of the message, include that variable.

You're done.

php.net > search for "mail".

Twey
05-02-2006, 11:02 PM
PHP outputs html. That's WHAT it does... nothing more, nothing less. (Plus, I guess, cookies and serverside stuff.)PHP is a highly versatile language; whole programs can be written in it, it's not only for web backends.

djr33
05-02-2006, 11:58 PM
Well.. ok. I just meant that it's a method to output html. It can do other stuff, but all it usually does it output html, and that's the only thing that can be sent within an email from it.
You're right, on the bigger scale, though.

cow_manuver
05-03-2006, 12:36 AM
PHP outputs html. That's WHAT it does... nothing more, nothing less. (Plus, I guess, cookies and serverside stuff.)


What you do is store *whatever you want* into a variable.

Then use the mail() command.

Then as the body of the message, include that variable.

You're done.

php.net > search for "mail".

I understand all this plain and clear. I have successfully sent an html email. But the problem is, I have a form that the user inputs the amount they have, and I want the email to display ONLY the items that the customer has, and discard the rest, instead of displaying a table that has a ****load of cells that say QTY: 0.

my output is a .PHP file even though it is generating the html.

So I guess at this point my questions is, what command (if there is one) am I looking for that will convert the generated html into an actual .html file? I was looking this up earlier and haven't found anything yet that shows me how to do this. I was seeing something that suggested creating a site cache... but I don't think all that will be necessary if there is a simple way to create a .html file out of each generated page.

xeno
05-03-2006, 12:46 AM
You could try writing to a php file and sending that file as an attachment.

cow_manuver
05-03-2006, 12:52 AM
You could try writing to a php file and sending that file as an attachment.

i'll try that!

djr33
05-03-2006, 05:49 AM
Umm... no. I'm puzzled here. If you send an email, there is no 'extension' to it... it's just data (text) sent as part of the message body. In that case, won't matter what 'format' it is in... it'll just be interpreted as html/plain text/viewer default/etc.

Attaching a .php file will just cause errors... they cannot be run locally unless you happen to have a php server setup on your home computer, which I doubt your customers will have.
Even if there is no php code, I'm unsure as to whether they can view the page as html.
If you do require changing the name, there are functions listed on php.net under 'file handling' and/or 'strings', depending on what your specific needs may be.


You ask for a method to send them 'only specific things'... sure. USE PHP to OUTPUT as HTML. The outputted html can, as programmed into the php, contain only those 'specific things'.
Then, assuming the email viewer has support for html, and your mail function has a properly coding html encoding setting (not used to this myself... sounds like you've managed to email html before... that's good), then it should display as a form.

forms aren't php. they are html. plain and simple.

In your form, use this:
<form ... action="nextpage.php" target="_blank">
Which will
1. send to a php page to interpret the data (data can only be interpreted serverside AFTER it is sent TO the server.)
2. open in a blank new window... so the email's website's frames and such don't have problems. I am not sure how/if this will function in programs like outlook... but I would assume, I suppose, that it would be as a link would... open a browser window... default browser.

the other option would be to JUST send a LINK to a page on your site.
That link could be like this:
<a href="http://site.com/page.php?ordernumber=2349032940>click</a>
or something like that, where the php (using $_GET['ordernumber']) would be able to get the number of the order (or another ID code/username/etc etc) and display the correct form, info, etc. AND be running from your server WITH php functioning properly.
The downside to this method is that you would have to store the data in the form, etc, until the user came to view it (and, maybe, after). This could be done using a database, or, even, html pages (php?) GENERATED on your server via the php code in the first place... but this is kinda complex. Or you could use that info sent to dynamically generate a new page that has the right info, but you'd have to send a lot of info with get; probably too much.

Twey
05-03-2006, 07:43 AM
Output buffers! Terribly handy things.
At the start of your page:
<?php ob_start(); ?>Then, before the mail call (presuming you've already output all the HTML you want in the email):
<?php
$page = ob_get_contents();
ob_end_flush();
?>$page will now contain the page's HTML.

Even if there is no php code, I'm unsure as to whether they can view the page as html.Only if they have that set up as an association, or if they're smart enough to open it in a browser themselves.

djr33
05-03-2006, 07:48 AM
Right.... I guess it's possible to view php locally with no code, IF they know to open... makes sense. But "general customers" might not, and definitely shouldn't be expected/assumed/required to.


So... that ob_start/end thing stores everything that is output into $page?

Interesting... useful, indeed.

Twey
05-03-2006, 07:50 AM
But "general customers" might not, and definitely shouldn't be expected/assumed/required to.Quite right.
Interesting... useful, indeed.Indeed so. Especially when you want to include() a local PHP page and make a few adjustments to the output before displaying.

djr33
05-03-2006, 08:22 AM
But... no.... include outputs right away, yes?

Oh... buffer... meaning it delays output till you say? Or does it also output?

If it delays... that's a really nice feature... I just thought it logged.

Twey
05-03-2006, 08:27 AM
Oh... buffer... meaning it delays output till you say? Or does it also output?Nope, it doesn't actually output anything to the client until you call ob_flush() or ob_end_flush().

djr33
05-03-2006, 08:30 AM
what's the difference there? (sorry... being lazy... could look it up.. but you're explaining well) :)

Twey
05-03-2006, 08:38 AM
what's the difference there? (sorry... being lazy... could look it up.. but you're explaining well)ob_flush() simply prints the current contents of the buffer; ob_end_flush() does so and then destroys the output buffer as well. Antonym: ob_end_clean(), which simply destroys the buffer.

cow_manuver
05-24-2006, 12:16 AM
hey, I just wanted to say thanks Twey, that OB stuff is exactly what I was looking for =] worked out quite nice.