1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'HTML, Graphics & Programming' started by Apex, 18 Sep 2011.

  1. Apex


    Joined: 12 Feb 2006

    Posts: 13,788

    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...

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

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

    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 if ($service == "theonewewent") { ?>
    <td><i>this is the service you selected</i></td>
    <?php ?>

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

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


    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:
    if (
    $service == "theonewewent") { 
    <td><i>this is the service you selected</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
  3. Makhaira


    Joined: 23 Nov 2007

    Posts: 4,860

    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!
  4. Beansprout

    Man of Honour

    Joined: 31 Jan 2004

    Posts: 16,327

    Location: Plymouth