Help inserting php code in html in $message

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

    Help inserting php code in html in $message

    Greetings,
    I have not been able to find the documentation that will allow me to
    insert
    php code inside the html code in the $message block in the mail() function.
    Using the example in the help file:

    <?php
    /* recipients */
    $to = "valid@email.ad d" . ", " ; // note the comma
    $to .= $_POST["email_addr ess"];

    /* subject */
    $subject = "CTO for {$_POST["Client_Nam e"]}";

    /* message */
    $message ='
    <HTML>
    <BODY>
    <H1 ALIGN=CENTER><F ONT SIZE=7>CTO</FONT></H1>
    <HR SIZE=10 color="blue">
    <DIV ALIGN=LEFT>
    <P><STRONG> Todays Date:"<?php echo $_POST["Date"]; ?>"</STRONG></P>
    <HR SIZE=10 color="blue">
    </BODY>
    </HTML>
    ';

    /* To send HTML mail, you can set the Content-type header. */
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

    /* additional headers */
    $headers .= "To: Department <valid@email.ad d>\r\n";
    $headers .= "From: {$_POST["Sales_Pers on"]}
    <{$_POST["email_addr ess"]}>\r\n";
    $headers .= "Cc: {$_POST["Sales_Pers on"]} <{$_POST["email_addr ess"]}>\r\n";

    /* and now mail it */
    mail($to, $subject, $message, $headers);
    ?>


    The above code displays the first line "CTO", the first HR then "Todays
    Date:" and then
    just a pair of double quotes. After that, the next HR and that's it.

    The e-mail works fine, the script will pick-up the data from the html form
    for the addresses.

    Any help or direction would be appreciated.

    Thanks,
    Ray
    To reply direct, take out spam.


  • CountScubula

    #2
    Re: Help inserting php code in html in $message

    "Rainmaker" <rainman@spamma ilatmydomain.co m> wrote in message
    news:2GJJb.4311 $Dw.1006@newssv r22.news.prodig y.com...[color=blue]
    > Greetings,
    > I have not been able to find the documentation that will allow me to
    > insert
    > php code inside the html code in the $message block in the mail()[/color]
    function.[color=blue]
    > Using the example in the help file:
    >
    > <?php
    > /* recipients */
    > $to = "valid@email.ad d" . ", " ; // note the comma
    > $to .= $_POST["email_addr ess"];
    >
    > /* subject */
    > $subject = "CTO for {$_POST["Client_Nam e"]}";
    >
    > /* message */
    > $message ='
    > <HTML>
    > <BODY>
    > <H1 ALIGN=CENTER><F ONT SIZE=7>CTO</FONT></H1>
    > <HR SIZE=10 color="blue">
    > <DIV ALIGN=LEFT>
    > <P><STRONG> Todays Date:"<?php echo $_POST["Date"]; ?>"</STRONG></P>
    > <HR SIZE=10 color="blue">
    > </BODY>
    > </HTML>
    > ';
    >
    > /* To send HTML mail, you can set the Content-type header. */
    > $headers = "MIME-Version: 1.0\r\n";
    > $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    >
    > /* additional headers */
    > $headers .= "To: Department <valid@email.ad d>\r\n";
    > $headers .= "From: {$_POST["Sales_Pers on"]}
    > <{$_POST["email_addr ess"]}>\r\n";
    > $headers .= "Cc: {$_POST["Sales_Pers on"]}[/color]
    <{$_POST["email_addr ess"]}>\r\n";[color=blue]
    >
    > /* and now mail it */
    > mail($to, $subject, $message, $headers);
    > ?>
    >
    >
    > The above code displays the first line "CTO", the first HR then "Todays
    > Date:" and then
    > just a pair of double quotes. After that, the next HR and that's it.
    >
    > The e-mail works fine, the script will pick-up the data from the html form
    > for the addresses.
    >
    > Any help or direction would be appreciated.
    >
    > Thanks,
    > Ray
    > To reply direct, take out spam.
    >
    >[/color]

    thats becouse $message is a string, treat it as such, try this:
    $message ='
    <HTML>
    <BODY>
    <H1 ALIGN=CENTER><F ONT SIZE=7>CTO</FONT></H1>
    <HR SIZE=10 color="blue">
    <DIV ALIGN=LEFT>
    <P><STRONG> Todays Date:{$_POST["Date"]}</STRONG></P>
    <HR SIZE=10 color="blue">
    </BODY>
    </HTML>
    ';



    --
    Mike Bradley
    http://gzen.myhq.info -- free online php tools


    Comment

    • Rainmaker

      #3
      Re: Help inserting php code in html in $message


      "CountScubu la" <me@scantek.hot mail.com> wrote in message
      news:LtOJb.6296 $lA2.4034@newss vr25.news.prodi gy.com...[color=blue]
      > "Rainmaker" <rainman@spamma ilatmydomain.co m> wrote in message
      > news:2GJJb.4311 $Dw.1006@newssv r22.news.prodig y.com...[color=green]
      > > Greetings,
      > > I have not been able to find the documentation that will allow me to
      > > insert
      > > php code inside the html code in the $message block in the mail()[/color]
      > function.[color=green]
      > > Using the example in the help file:
      > >
      > > <?php
      > > /* recipients */
      > > $to = "valid@email.ad d" . ", " ; // note the comma
      > > $to .= $_POST["email_addr ess"];
      > >
      > > /* subject */
      > > $subject = "CTO for {$_POST["Client_Nam e"]}";
      > >
      > > /* message */
      > > $message ='
      > > <HTML>
      > > <BODY>
      > > <H1 ALIGN=CENTER><F ONT SIZE=7>CTO</FONT></H1>
      > > <HR SIZE=10 color="blue">
      > > <DIV ALIGN=LEFT>
      > > <P><STRONG> Todays Date:"<?php echo $_POST["Date"]; ?>"</STRONG></P>
      > > <HR SIZE=10 color="blue">
      > > </BODY>
      > > </HTML>
      > > ';
      > >
      > > /* To send HTML mail, you can set the Content-type header. */
      > > $headers = "MIME-Version: 1.0\r\n";
      > > $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
      > >
      > > /* additional headers */
      > > $headers .= "To: Department <valid@email.ad d>\r\n";
      > > $headers .= "From: {$_POST["Sales_Pers on"]}
      > > <{$_POST["email_addr ess"]}>\r\n";
      > > $headers .= "Cc: {$_POST["Sales_Pers on"]}[/color]
      > <{$_POST["email_addr ess"]}>\r\n";[color=green]
      > >
      > > /* and now mail it */
      > > mail($to, $subject, $message, $headers);
      > > ?>
      > >
      > >
      > > The above code displays the first line "CTO", the first HR then "Todays
      > > Date:" and then
      > > just a pair of double quotes. After that, the next HR and that's it.
      > >
      > > The e-mail works fine, the script will pick-up the data from the html[/color][/color]
      form[color=blue][color=green]
      > > for the addresses.
      > >
      > > Any help or direction would be appreciated.
      > >
      > > Thanks,
      > > Ray
      > > To reply direct, take out spam.
      > >
      > >[/color]
      >
      > thats becouse $message is a string, treat it as such, try this:
      > $message ='
      > <HTML>
      > <BODY>
      > <H1 ALIGN=CENTER><F ONT SIZE=7>CTO</FONT></H1>
      > <HR SIZE=10 color="blue">
      > <DIV ALIGN=LEFT>
      > <P><STRONG> Todays Date:{$_POST["Date"]}</STRONG></P>
      > <HR SIZE=10 color="blue">
      > </BODY>
      > </HTML>
      > ';
      >
      >
      >
      > --
      > Mike Bradley
      > http://gzen.myhq.info -- free online php tools
      >
      >[/color]

      Greetings,
      Mike, I appreciate your reply to my post.
      Unfortunately that recommendation did not work. It printed out:
      Todays Date: {$_POST["Date"]}.
      I tried putting the php script tag in, like this:
      Todays Date:<?php {$_POST["Date"]}?>
      but that printed out:
      Todays Date:

      If you or someone else has another suggestion or a place to look
      I would very much appreciate it.

      Thank you,
      Ray


      Comment

      • CountScubula

        #4
        Re: Help inserting php code in html in $message

        [color=blue]
        > Greetings,
        > Mike, I appreciate your reply to my post.
        > Unfortunately that recommendation did not work. It printed out:
        > Todays Date: {$_POST["Date"]}.
        > I tried putting the php script tag in, like this:
        > Todays Date:<?php {$_POST["Date"]}?>
        > but that printed out:
        > Todays Date:
        >
        > If you or someone else has another suggestion or a place to look
        > I would very much appreciate it.
        >
        > Thank you,
        > Ray
        >
        >[/color]

        Was "Date" set from a form that got POSTed to your page? I was mearly
        correcting the error in the string definition, not validity of it.

        if you want the date use:
        Todays Date:<?php print date("j, n, Y"); ?>


        --
        Mike Bradley
        http://gzen.myhq.info -- free online php tools


        Comment

        • Rainmaker

          #5
          Re: Help inserting php code in html in $message


          "CountScubu la" <me@scantek.hot mail.com> wrote in message
          news:b30Kb.5575 $AC5.1012@newss vr27.news.prodi gy.com...[color=blue]
          >[color=green]
          > > Greetings,
          > > Mike, I appreciate your reply to my post.
          > > Unfortunately that recommendation did not work. It printed out:
          > > Todays Date: {$_POST["Date"]}.
          > > I tried putting the php script tag in, like this:
          > > Todays Date:<?php {$_POST["Date"]}?>
          > > but that printed out:
          > > Todays Date:
          > >
          > > If you or someone else has another suggestion or a place to look
          > > I would very much appreciate it.
          > >
          > > Thank you,
          > > Ray
          > >
          > >[/color]
          >
          > Was "Date" set from a form that got POSTed to your page? I was mearly
          > correcting the error in the string definition, not validity of it.
          >
          > if you want the date use:
          > Todays Date:<?php print date("j, n, Y"); ?>
          >
          >
          > --
          > Mike Bradley
          > http://gzen.myhq.info -- free online php tools
          >
          >[/color]

          Greetings,

          Thanks again Mike for replying.

          The data, including the date, is coming from an html form. I'm using the
          mail()
          script from the php help file:

          <?php
          /* recipients */
          $to = "mary@example.c om" . ", " ; // note the comma
          $to .= "kelly@example. com";

          /* subject */
          $subject = "Birthday Reminders for August";

          /* message */
          $message = '
          <html>
          <head>
          <title>Birthd ay Reminders for August</title>
          </head>
          <body>
          <p>Here are the birthdays upcoming in August!</p>
          <table>
          <tr>
          <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
          </tr>
          <tr>
          <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
          </tr>
          <tr>
          <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
          </tr>
          </table>
          </body>
          </html>
          ';

          /* To send HTML mail, you can set the Content-type header. */
          $headers = "MIME-Version: 1.0\r\n";
          $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

          /* additional headers */
          $headers .= "To: Mary <mary@example.c om>, Kelly <kelly@example. com>\r\n";
          $headers .= "From: Birthday Reminder <birthday@examp le.com>\r\n";
          $headers .= "Cc: birthdayarchive @example.com\r\n";
          $headers .= "Bcc: birthdaycheck@e xample.com\r\n";

          /* and now mail it */
          mail($to, $subject, $message, $headers);
          ?>

          with the data from the html form being included in the $message block in
          the above script.
          I've tried your suggestions and the <script language=php> </script> escape
          tags but nothing
          works yet.

          Thank you,
          Ray


          Comment

          • Fnark

            #6
            Re: Help inserting php code in html in $message

            If you use 'single quotes' any variable e.g. $var will just display $var a
            text string not the value it references. That is why you are getting
            {$_POST["Date"]} output as a string. To use variables in a string you should
            use "double quotes" as PHP recognises variables inside double quotes. When
            you use double-quotes, however, any further use of double-quotes inside your
            string must be "escaped" using the \ character. You do not need <?php ?>
            here but put the Date in single quotes as the escape character \ does not
            seem to work here. Also you appear to need to put the braces in too (I got
            this by trial and error mainly and not entirely sure of the whys and
            wherefores and there may be other ways of doing it - I am a newcomer to PHP)

            Hence your code should read:

            $message ="
            <HTML>
            <BODY>
            <H1 ALIGN=CENTER><F ONT SIZE=7>CTO</FONT></H1>
            <HR SIZE=10 color=\"blue\">
            <DIV ALIGN=LEFT>
            <P><STRONG> Todays Date: {$_POST['Date']}</STRONG></P>
            <HR SIZE=10 color=\"blue\">
            </BODY>
            </HTML>
            ";

            Hope that works as I did not have time to test it properly.

            Mark


            "Rainmaker" <rainman@spamma ilatmydomain.co m> wrote in message
            news:2GJJb.4311 $Dw.1006@newssv r22.news.prodig y.com...[color=blue]
            > Greetings,
            > I have not been able to find the documentation that will allow me to
            > insert
            > php code inside the html code in the $message block in the mail()[/color]
            function.[color=blue]
            > Using the example in the help file:
            >
            > <?php
            > /* recipients */
            > $to = "valid@email.ad d" . ", " ; // note the comma
            > $to .= $_POST["email_addr ess"];
            >
            > /* subject */
            > $subject = "CTO for {$_POST["Client_Nam e"]}";
            >
            > /* message */
            > $message ='
            > <HTML>
            > <BODY>
            > <H1 ALIGN=CENTER><F ONT SIZE=7>CTO</FONT></H1>
            > <HR SIZE=10 color="blue">
            > <DIV ALIGN=LEFT>
            > <P><STRONG> Todays Date:"<?php echo $_POST["Date"]; ?>"</STRONG></P>
            > <HR SIZE=10 color="blue">
            > </BODY>
            > </HTML>
            > ';
            >
            > /* To send HTML mail, you can set the Content-type header. */
            > $headers = "MIME-Version: 1.0\r\n";
            > $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
            >
            > /* additional headers */
            > $headers .= "To: Department <valid@email.ad d>\r\n";
            > $headers .= "From: {$_POST["Sales_Pers on"]}
            > <{$_POST["email_addr ess"]}>\r\n";
            > $headers .= "Cc: {$_POST["Sales_Pers on"]}[/color]
            <{$_POST["email_addr ess"]}>\r\n";[color=blue]
            >
            > /* and now mail it */
            > mail($to, $subject, $message, $headers);
            > ?>
            >
            >
            > The above code displays the first line "CTO", the first HR then "Todays
            > Date:" and then
            > just a pair of double quotes. After that, the next HR and that's it.
            >
            > The e-mail works fine, the script will pick-up the data from the html form
            > for the addresses.
            >
            > Any help or direction would be appreciated.
            >
            > Thanks,
            > Ray
            > To reply direct, take out spam.
            >
            >[/color]


            Comment

            • Rainmaker

              #7
              Re: Help inserting php code in html in $message


              "Fnark" <fake@reallyisf ake.com> wrote in message
              news:p72Kb.1823 8$Od5.4555@news-binary.blueyond er.co.uk...[color=blue]
              > If you use 'single quotes' any variable e.g. $var will just display $var a
              > text string not the value it references. That is why you are getting
              > {$_POST["Date"]} output as a string. To use variables in a string you[/color]
              should[color=blue]
              > use "double quotes" as PHP recognises variables inside double quotes. When
              > you use double-quotes, however, any further use of double-quotes inside[/color]
              your[color=blue]
              > string must be "escaped" using the \ character. You do not need <?php ?>
              > here but put the Date in single quotes as the escape character \ does not
              > seem to work here. Also you appear to need to put the braces in too (I got
              > this by trial and error mainly and not entirely sure of the whys and
              > wherefores and there may be other ways of doing it - I am a newcomer to[/color]
              PHP)[color=blue]
              >
              > Hence your code should read:
              >
              > $message ="
              > <HTML>
              > <BODY>
              > <H1 ALIGN=CENTER><F ONT SIZE=7>CTO</FONT></H1>
              > <HR SIZE=10 color=\"blue\">
              > <DIV ALIGN=LEFT>
              > <P><STRONG> Todays Date: {$_POST['Date']}</STRONG></P>
              > <HR SIZE=10 color=\"blue\">
              > </BODY>
              > </HTML>
              > ";
              >
              > Hope that works as I did not have time to test it properly.
              >
              > Mark
              >
              >
              > "Rainmaker" <rainman@spamma ilatmydomain.co m> wrote in message
              > news:2GJJb.4311 $Dw.1006@newssv r22.news.prodig y.com...[color=green]
              > > Greetings,
              > > I have not been able to find the documentation that will allow me to
              > > insert
              > > php code inside the html code in the $message block in the mail()[/color]
              > function.[color=green]
              > > Using the example in the help file:
              > >
              > > <?php
              > > /* recipients */
              > > $to = "valid@email.ad d" . ", " ; // note the comma
              > > $to .= $_POST["email_addr ess"];
              > >
              > > /* subject */
              > > $subject = "CTO for {$_POST["Client_Nam e"]}";
              > >
              > > /* message */
              > > $message ='
              > > <HTML>
              > > <BODY>
              > > <H1 ALIGN=CENTER><F ONT SIZE=7>CTO</FONT></H1>
              > > <HR SIZE=10 color="blue">
              > > <DIV ALIGN=LEFT>
              > > <P><STRONG> Todays Date:"<?php echo $_POST["Date"]; ?>"</STRONG></P>
              > > <HR SIZE=10 color="blue">
              > > </BODY>
              > > </HTML>
              > > ';
              > >
              > > /* To send HTML mail, you can set the Content-type header. */
              > > $headers = "MIME-Version: 1.0\r\n";
              > > $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
              > >
              > > /* additional headers */
              > > $headers .= "To: Department <valid@email.ad d>\r\n";
              > > $headers .= "From: {$_POST["Sales_Pers on"]}
              > > <{$_POST["email_addr ess"]}>\r\n";
              > > $headers .= "Cc: {$_POST["Sales_Pers on"]}[/color]
              > <{$_POST["email_addr ess"]}>\r\n";[color=green]
              > >
              > > /* and now mail it */
              > > mail($to, $subject, $message, $headers);
              > > ?>
              > >
              > >
              > > The above code displays the first line "CTO", the first HR then "Todays
              > > Date:" and then
              > > just a pair of double quotes. After that, the next HR and that's it.
              > >
              > > The e-mail works fine, the script will pick-up the data from the html[/color][/color]
              form[color=blue][color=green]
              > > for the addresses.
              > >
              > > Any help or direction would be appreciated.
              > >
              > > Thanks,
              > > Ray
              > > To reply direct, take out spam.
              > >
              > >[/color]
              >
              >[/color]

              Greetings,

              Mark, thank you for your reply. That was indeed the problem. I switched
              to
              the double quotes to enclose the entire $message string and all the data is
              appearing
              as it should.
              Thanks to you and Mike for all your help.

              Ray


              Comment

              • CountScubula

                #8
                Re: Help inserting php code in html in $message

                "Fnark" <fake@reallyisf ake.com> wrote in message
                news:p72Kb.1823 8$Od5.4555@news-binary.blueyond er.co.uk...[color=blue]
                > If you use 'single quotes' any variable e.g. $var will just display $var a
                > text string not the value it references. That is why you are getting
                > {$_POST["Date"]} output as a string. To use variables in a string you[/color]
                should[color=blue]
                > use "double quotes" as PHP recognises variables inside double quotes. When
                > you use double-quotes, however, any further use of double-quotes inside[/color]
                your[color=blue]
                > string must be "escaped" using the \ character. You do not need <?php ?>
                > here but put the Date in single quotes as the escape character \ does not
                > seem to work here. Also you appear to need to put the braces in too (I got
                > this by trial and error mainly and not entirely sure of the whys and
                > wherefores and there may be other ways of doing it - I am a newcomer to[/color]
                PHP)

                Ah yes,

                I tend to get sooooo involved in a piece a code, I tend to miss what is
                right there in my face.

                --
                Mike Bradley
                http://www.gzentools.com -- free online php tools


                Comment

                Working...