php: long variables with other variables which needs to be sent by email

Joined
12 Feb 2006
Posts
17,216
Location
Surrey
i'm trying to make a nice looking email for when someone submits a quote. currently it's sent in plain text but i'd like to make it in html and add some very basic css.

i've got the email designed fine, however i'm struggling to fill the info with the unique data that the user would have input.

at the moment when the user fills out a form and it gets sent to them it's sent like this...

PHP:
if($job1>0) { $body .= "job1: <b>$job1</b>"; }
if($job2>0) { $body .= "job2: <b>$job2</b>"; }
if($job3>0) { $body .= "job3: <b>$job3</b>"; }

mail($your_email, "subject here", $body);

etc

and when the user fills out the form they're shown the form online like this (though this is only tiny part of the form it's much busier/longer then this)

PHP:
<?php if ($service == "theonewewent") { ?>
<tr>
<td><i>this is the service you selected</i></td>
<td></td>
<td><i>Free</i></td>
</tr>
<?php } ?>


<tr>
<td colspan='3' class='newLine'><hr /></td>
</tr>

<tr class='finalArea'>
<td colspan='2'>Sub Total:</td>
<td>&pound;<?= $quote_subtotal; ?></td>
</tr>
<tr class='finalArea <?php if ($discount_total>0) { echo " discountStyle "; } ?>'>
<td colspan='2'> Total Discount:</td>
<td>&pound;<?= $discount_total; ?> </td>
</tr>
<tr class='finalArea totalPrice'>
<td colspan='2'>Total Price:</td>
<td>&pound;<?= $quote_total; ?></td>
</tr>

what i am hoping to do is be able to save what the user sees online under 1 long variable, and then get this sent like the $body is sent by email. the trboule i'm having is understanding how i'll get what is shown online into one variable as there is so much code already.

it may be that i need to restart and simplify the whole thing so i'm open to suggestions on what is the best way for this.
 
Associate
Joined
9 May 2005
Posts
121
Location
Yorkshire
If I've understood correctly, you're just looking to avoid code duplication between your html form page and your email generator code?

If this is the case then one option would be to encapsulate the above code in a function that returns a string that can then be outputted to generate your html page or joined to your email $body.

You'd have to convert the above code so that it is within a string, ie:
PHP:
<?php 
$output="";
if ($service == "theonewewent") { 
$output="<tr> 
<td><i>this is the service you selected</i></td> 
<td></td> 
<td><i>Free</i></td> 
</tr> ";
 }

$output.= "<tr> 
<td colspan='3' class='newLine'><hr /></td> 
</tr> ......."
?>

I'm assuming that your familiar with PHP functions, if this isn't the case then have a look here: http://www.w3schools.com/php/php_functions.asp
 
Soldato
Joined
23 Nov 2007
Posts
4,939
Location
Lancashire, UK
What Dave said...

Have your page structure captured already in a variable or variables, and output this later on. You can quite happily embed masses of HTML within a PHP variable, you just need to remember to escape any speech marks. Job's a good 'un!
 
Back
Top Bottom