简体   繁体   English

HTML电子邮件中的PHP代码不起作用

[英]PHP code inside of HTML email not working

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. 尝试使用PHP向自己发送HTML电子邮件,但HTML内的PHP代码无法正常工作,我也不知道为什么。 Sorry if I'm not very clear, I'm quite new to this 抱歉,如果我不太清楚,我对此很陌生

<?php 

$ccode = rand(1000, 9999);
$to = "example@gmail.com";
$from = "example2@gmail.com";
$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. 电子邮件照常发送,但是电子邮件中的PHP部分不起作用,而是显示“ $ ccode”。

I would appreciate any kind of help or advice on this thank you! 感谢您提供的任何帮助或建议!

$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. 您可以在将字符串分配给$ message之前声明变量。 Ie: 即:

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

you can't put a code php when you try to declare the variable $message 当您尝试声明变量$ message时,您不能放置代码php

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>";          
      ?>

Problem come with your $message variable. 问题来自您的$message变量。

It's a string, so you can not use the open php block code here. 这是一个字符串,因此您不能在此处使用开放的php块代码。

It should be: 它应该是:

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

Hope this help! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM