Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help - Including "if" statements into email $message

    How do I include and "if" statement into an email $message.

    Example

    PHP Code:
    <?php
    $order01 
    $_POST["order01"];
    $order02 $_POST["order02"];
    $order03 $_POST["order03"];

    if ( 
    $order01 ) {
        echo 
    " amount of $order01 <br />";
    }

    if ( 
    $order02 ) {
        echo 
    " amount of $order02 <br />";
    }

    if ( 
    $order03 ) {
        echo 
    " amount of $order03 <br />";
    }
    ?>
    All I'm trying to do is make sure that anyone that orders only receives the information that was selected not unneeded 0's or extra echo commands that print nothing as in
    "amount of 0", or "amount of "nothing".

    I'm new to this but I thought this would be simple. Including an if state from those above into the $message of an email.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    echo won't show up in an email unless you use output buffering and capture it as a variable.
    First and foremost, use a dirty trick to get around problems with notice triggering in case you're _POST offsets are not available:
    PHP Code:
    $order01 = &$_POST["order01"];
    $order02 = &$_POST["order02"];
    $order03 = &$_POST["order03"]; 
    This way you won't need to change you're if statements to check for isset. Be aware that should you change $_POST it will be reflected in the given variables. I don't recommend using extract.
    Now, capture you're output. I'll use an array:
    PHP Code:
    $aOrder = array();
    if ( 
    $order01 ) {
        
    $aOrder[] = " amount of $order01";
    }

    if ( 
    $order02 ) {
        
    $aOrder[] = " amount of $order02";
    }

    if ( 
    $order03 ) {
        
    $aOrder[] = " amount of $order03";

    Mkay, now that we have that, you can implode the values and do whatever you want with them:
    PHP Code:
    $to 'mailto@domain.com';
    $subject 'My Subject';
    $additionalHeaders "From: myuser@mysite.com\r\n";

    if (
    false !== @mail($to$subjectimplode("\n"$aOrder), $additionalHeaders))
    {
        print 
    'Mail Sent';
    }
    else
    {
        print 
    'Mail not sent via mail command.';

    This assumes that you're order{DD} returns an integer representing how many items are ordered. It would be easier to send an html array for this, but I don't know what you're form intention is.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    ghost2012 (11-02-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •