1

Trying to test send a HTML email to myself using PHP but the PHP code inside the HTML does not work and I don't know why. Sorry if I'm not very clear, I'm quite new to this

<?php 

$ccode = rand(1000, 9999);
$to = "[email protected]";
$from = "[email protected]";
$subject = "Confirmation Code";
echo $ccode;

//begin of HTML message 
$message = '<html>
             <head>
              <title> Confirmation Code</title>
             </head>
               <body style="background-color:#004d4d">
                <div style= "text-align:center"> <img src="example.png" alt="Logo" width="300" height="300" align="top;center"> </div>
                <h1 style="font-family:verdana; color:white; text-align:center"> This is your confirmation code:  </h1>
                <p style= "text-align:center;font-size:400%;color:#009999; font-family:arial"> 
               <b> 

                //code that isn't working
               <?php 

                $ccode = rand(1000, 9999); 
                echo $ccode; 

                ?> 
              </b>
              </p>
              <p style= "text-align:center; font-family:verdana; color:white"> Please enter this code into the application. </p>
              </body>
              </html>';
              //end of message 
$headers  = "From: $from\r\n"; 
$headers .= "Content-type: text/html\r\n";

// now lets send the email. 
mail($to, $subject, $message, $headers); 

echo "Message has been sent....!"; 
?>

The email sends as usual but the PHP part inside the email does not work and prints "$ccode" instead.

I would appreciate any kind of help or advice on this thank you!

3
  • 3
    you are already inside php tags, no need to open them again Commented Feb 2, 2018 at 7:46
  • 1
    have your server PHP configured? Commented Feb 2, 2018 at 7:48
  • 1
    @PraneethNidarshan Since this is a PHP script and OP said the mail gets sent I guess Yes Commented Feb 2, 2018 at 7:50

3 Answers 3

4
$message = '<html>
         ...
          </html>';
          //end of message 

This is a string, the code within is also considered text. You could declare your variable before assigning the string to $message. I.e.:

<?php 
$ccode = rand(1000, 9999); 
$message = '<html>
         <head>
          <title> Confirmation Code</title>
         </head>
           <body style="background-color:#004d4d">
            <div style= "text-align:center"> <img src="example.png" alt="Logo" width="300" height="300" align="top;center"> </div>
            <h1 style="font-family:verdana; color:white; text-align:center"> This is your confirmation code:  </h1>
            <p style= "text-align:center;font-size:400%;color:#009999; font-family:arial"> 
           <b>'.
          $ccode .
          '</b>
          </p>
          <p style= "text-align:center; font-family:verdana; color:white"> Please enter this code into the application. </p>
          </body>
          </html>';
          //end of message 
Sign up to request clarification or add additional context in comments.

Comments

0

Problem come with your $message variable.

It's a string, so you can not use the open php block code here.

It should be:

$message = '... <html code>' . $ccode . '... <other html code>';

Hope this help!

Comments

0

you can't put a code php when you try to declare the variable $message

but you can do a concatenation with another variable which you have declared from the beginning

so you can either do this

      <?php 
      $ccode = rand(1000, 9999);
      $message = '<html> other balises' . $ccode . 'some other balises</html>';
      ?>

or with double cote (")

      <?php 
      $ccode = rand(1000, 9999);
      $message = "<html> other balises {$ccode}  some other balises</html>";
      ?>

or the last solution

      <?php 
      $message = "<html> other balises";
      $ccode = rand(1000, 9999);
      $message .= $ccode;
      $message = "some other balises</html>";          
      ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.