sending the contents of a php array in a html email

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian N

    sending the contents of a php array in a html email

    Hi,

    I'm creating a site that posts orders to an email address when they're
    submitted, these orders need to be in the form of official invoices so
    i'm using HTML to format them.

    I already have a shopping basket system which adds all of the totals
    up and puts them into a nice looking invoice.

    What's the best way to take this HTML and put it into an email?
    I'm really looking for a way of sending the page via email, like you
    can do in IE by selecting File > Send > Page by Email.

    The basket script is quite large and i'm trying to avoid running it
    more times than neccesary.

    Any advice would be greatly appreciated.
  • TekWiz

    #2
    Re: sending the contents of a php array in a html email

    Ian N wrote:[color=blue]
    > Hi,
    >
    > I'm creating a site that posts orders to an email address when they're
    > submitted, these orders need to be in the form of official invoices so
    > i'm using HTML to format them.
    >
    > I already have a shopping basket system which adds all of the totals
    > up and puts them into a nice looking invoice.
    >
    > What's the best way to take this HTML and put it into an email?
    > I'm really looking for a way of sending the page via email, like you
    > can do in IE by selecting File > Send > Page by Email.
    >
    > The basket script is quite large and i'm trying to avoid running it
    > more times than neccesary.
    >
    > Any advice would be greatly appreciated.[/color]

    This depends on a few things. Will your cart be printing each invoice
    to a static HTML file, or will it be displaying a dynamic page?

    This is the basics for doing it from a static page:

    -if it's in a static HTML file. Use file() function to read the file
    into an array by line number.
    -Use foreach() statement to iterate the array into a string
    -use the mail() function to send the email.
    <?php
    $message = "";
    foreach(@file($ fileLocation) as $lineText) {
    $message .= $lineText . "\n";
    }
    mail($to,$subje ct,$message,$ad ditional_header s) or die("Message
    didn't send");
    ?>

    The basics of doing it from a dynamic page:

    -set up your cart to write the invoice to a string variable. This can
    be quite difficult to do, but you'll have to read through the code, and
    then use that string variable in the mail() function.

    References:
    Filesystem Functions => http://www.php.net/manual/en/ref.filesystem.php
    mail() Function => http://www.php.net/manual/en/function.mail.php

    --TekWiz

    Comment

    • NC

      #3
      Re: sending the contents of a php array in a html email

      Ian N wrote:[color=blue]
      >
      > I already have a shopping basket system which adds all of
      > the totals up and puts them into a nice looking invoice.
      >
      > What's the best way to take this HTML and put it into an email?[/color]

      There's is a quick two-step fix for that:

      1. Insert ob_start() before the first output statement.
      2. After the last output statement, insert something like
      this:

      $html = ob_get_clean();

      Now the entire output of your script is captured in the
      $html variable. To output it to the client, you can

      echo $html;

      To e-mail it... well, that depends on your mail setup.
      If you have PHPMailer and you send mail via mail(), all
      you need to do is:

      require("class. phpmailer.php") ;
      $mail = new PHPMailer();
      $mail->IsMail();
      $mail->From = "you@yourdomain .com";
      $mail->FromName = "Mailer";
      $mail->AddAddress("th ey@theirdomain. com", "Firstname Lastname");
      $mail->IsHTML(true) ;
      $mail->Subject = "Invoice";
      $mail->Body = $html;
      if(!$mail->Send()) {
      // write an entry to an error log...
      }

      For more information on PHPMailer, see:



      Cheers,
      NC

      Comment

      • TekWiz

        #4
        Re: sending the contents of a php array in a html email

        NC wrote:[color=blue]
        > Ian N wrote:
        >[color=green]
        >>I already have a shopping basket system which adds all of
        >>the totals up and puts them into a nice looking invoice.
        >>
        >>What's the best way to take this HTML and put it into an email?[/color]
        >
        >
        > There's is a quick two-step fix for that:
        >
        > 1. Insert ob_start() before the first output statement.
        > 2. After the last output statement, insert something like
        > this:
        >
        > $html = ob_get_clean();
        >
        > Now the entire output of your script is captured in the
        > $html variable. To output it to the client, you can
        >
        > echo $html;
        >
        > To e-mail it... well, that depends on your mail setup.
        > If you have PHPMailer and you send mail via mail(), all
        > you need to do is:
        >
        > require("class. phpmailer.php") ;
        > $mail = new PHPMailer();
        > $mail->IsMail();
        > $mail->From = "you@yourdomain .com";
        > $mail->FromName = "Mailer";
        > $mail->AddAddress("th ey@theirdomain. com", "Firstname Lastname");
        > $mail->IsHTML(true) ;
        > $mail->Subject = "Invoice";
        > $mail->Body = $html;
        > if(!$mail->Send()) {
        > // write an entry to an error log...
        > }
        >
        > For more information on PHPMailer, see:
        >
        > http://phpmailer.sourceforge.net/
        >
        > Cheers,
        > NC
        >[/color]

        I'm glad to now know about the Output Buffering functions. That's a
        good thing to know.

        But what if PHP is embedded into HTML code? How can that HTML code be
        obtained?

        --TekWiz

        Comment

        • NC

          #5
          Re: sending the contents of a php array in a html email

          TekWiz wrote:[color=blue]
          > NC wrote:[color=green]
          > > Ian N wrote:
          > >[color=darkred]
          > > >What's the best way to take this HTML and put it into an email?[/color]
          > >
          > > There's is a quick two-step fix for that:
          > >
          > > 1. Insert ob_start() before the first output statement.
          > > 2. After the last output statement, insert something like
          > > this:
          > >
          > > $html = ob_get_clean();
          > >
          > > Now the entire output of your script is captured in the
          > > $html variable. To output it to the client, you can
          > >
          > > echo $html;[/color]
          >
          > But what if PHP is embedded into HTML code?[/color]

          Doesn't matter. Output buffering captures ALL output,
          regardless of whether it was done via PHP output statements
          or by switching out of PHP into HTML.

          Try this simple script:

          <?php ob_start(); ?>

          <html>
          <head>
          <title>Test</title>
          </head>
          <body>
          <p><?php echo 'Today is ', date('F d, Y'); ?></p>
          </body>
          </html>

          <?php
          $html = ob_get_clean();
          echo '<pre>', htmlentities($h tml), '</pre>';
          ?>

          and you will see that HTML was captured just like the
          output of echo()...

          Cheers,
          NC

          Comment

          • Ian N

            #6
            Re: sending the contents of a php array in a html email

            "NC" <nc@iname.com > wrote in message news:<111129041 5.289536.16570@ z14g2000cwz.goo glegroups.com>. ..[color=blue]
            > TekWiz wrote:[color=green]
            > > NC wrote:[color=darkred]
            > > > Ian N wrote:
            > > >
            > > > >What's the best way to take this HTML and put it into an email?
            > > >
            > > > There's is a quick two-step fix for that:
            > > >
            > > > 1. Insert ob_start() before the first output statement.
            > > > 2. After the last output statement, insert something like
            > > > this:
            > > >
            > > > $html = ob_get_clean();
            > > >
            > > > Now the entire output of your script is captured in the
            > > > $html variable. To output it to the client, you can
            > > >
            > > > echo $html;[/color]
            > >
            > > But what if PHP is embedded into HTML code?[/color]
            >
            > Doesn't matter. Output buffering captures ALL output,
            > regardless of whether it was done via PHP output statements
            > or by switching out of PHP into HTML.
            >
            > Try this simple script:
            >
            > <?php ob_start(); ?>
            >
            > <html>
            > <head>
            > <title>Test</title>
            > </head>
            > <body>
            > <p><?php echo 'Today is ', date('F d, Y'); ?></p>
            > </body>
            > </html>
            >
            > <?php
            > $html = ob_get_clean();
            > echo '<pre>', htmlentities($h tml), '</pre>';
            > ?>
            >
            > and you will see that HTML was captured just like the
            > output of echo()...
            >
            > Cheers,
            > NC[/color]

            Hi Thanks for the responses, unfortunatly the server i'm using is
            running PHP version 4.12 and this function is only valid for 4.30 +
            accoring to the php.net site, any other suggestions?

            Comment

            Working...