Jump to content

Archived

This topic is now archived and is closed to further replies.

Help!

php in the html generated by php? :)

Recommended Posts

hi

I want to know if it's possible to insert inline php code in an html messege send by a php form e-mail script.  I got the beef of this script online (it's not the final one I'll use I'm just using it to help learn php)
but I want to send an html e-mail but I want to be able to echo variables in the html.  this is what i've tried so far.  also am I doing the POST wrong because that doesn't show in the e-mail either.

[code]<?php
  $to="me@e-mail.com";
  $from=$_POST["email"];
  $subject="My first HTML E-mail";
  $msg=$_POST["message"];
  $message=
 
  '<h1>HTML E-mail</h1>
  <p>This is an <b>HTML</b> e-mail.</p>
  <p>try to put php in the html php page?!?!? <?=$subject?> </p>
  ';


  $headers = "MIME-Version: 1.0\r\n";
  $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
  $headers.= "From: $from\r\n";
  if (mail($to, $subject, $message, $headers))
      echo "Message Sent!";
  else
      echo "Failed to send message.";
?>[/code]
I got the $subject in the html now becasue the POST won't work (do I need a single quote instead of a double?)  but I eventually want to send what people write in a comment box.

the html works but the php in the html doesn't show

the main reason for this is formatting so It's pretty and easy to read when I get the results of a form
thanks

Share this post


Link to post
Share on other sites
php runs on the server, not the client, where do you expect this php to be parsed?

You need to turn you php variables into html output on the server before you send it to the client.

Share this post


Link to post
Share on other sites
inserting the variables are the same, if it doesn't work break up the message, end a section with " . variable . "begin message again
it will add them in there, in the proper places, it wouldn't be that hard you should be able to substitute them like this anyway
{$variable}
or if it's post or get you do the same thing, but when you are enterpolating those you leave off the single quotes
normally its
$_POST['variable']
$_GET['variable']
when you are interpolating it's
$POST[variable]
$_GET[variable]
Be careful of html mail, because php always sends the mail 2 times, atleast what php.net says, it ALWAYS sends 2 emails so if it's a mailing list, they get 2 emails.  

Share this post


Link to post
Share on other sites
Also you can ONLY interpolate with double quotes, here is an example

[code]$message = "
  <h1>HTML E-mail</h1>
  <p>This is an <b>HTML</b> e-mail.</p>
  <p>try to put php in the html php page" . $variable1 . "</p>
  <p>Then you continue your message here</p> 
  <p>Then you try to add more here" . $variable2 . "</p>
  <p>Then you continue down here even more</p>";[/code]

  Also keep in mind this will look retarded, atleast in the email it will be like this
say variable1 is here, and variable 2 is there
it will look like this
[quote]HTML E-Mail
This is an html email
try to put php int he html php pagehere
then continue your message here
then try to add more herethere
then you continue down here even more[/quote]
You see how they run together, the way to fix this, one way is that hte end of each one
leave a blank line like this

[code]$message = "
  <h1>HTML E-mail</h1>
  <p>This is an <b>HTML</b> e-mail.</p>
  <p>try to put php in the html php page " . $variable1 . "</p>
  <p>Then you continue your message here</p> 
  <p>Then you try to add more here " . $variable2 . "</p>
  <p>Then you continue down here even more</p>";[/code]
That should fix your problem, if you are sending more than 20-30 emails at once, drop the html email with php, and use the pear package, it opens up a constant stream unstead of killing server power by opening up 20-2000 streams, one for each email.  You can check that at php.net  just search pear

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.