TheDevMan
TheDevMan

Reputation: 5954

Insert PHP inside HTML

I am trying out the php mail service getting the variable from DB and then insert the value to HTML to test I am was trying out something like this:

<?php
include("db/dbvalue.php");
include("emailfun/email.php");

    $response = array();
    $emailaddress = "[email protected]";

    $dt = new DateTime();
    echo $dt->format('m-d');
    $m =  $dt->format('m');
    $d =  $dt->format('d');

    $result = mysql_query("SELECT * FROM nameValue WHERE type = 'Boys' AND month = '$m' AND date = '$d'");    

  if (mysql_num_rows($result) > 0) 
  {  
    while ($row = mysql_fetch_array($result)) 
    {
        $name = $row["name"];
        $to= $emailaddress;
        $subject ="Congratulations";
        $message="
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>Name goes here: <?php echo $name; ?> </p> 
</body>
</html>
"; 
        $from = "[email protected]";
        $fromname = "Mail Sender";
        send_html_mail($to, $subject, $message, $from, $fromname);

    }
  } 
  else
  {
    echo("NOTHING TODAY");  
  }
?>

I am receiving the mail but just with "Name goes here:"

I am not able to get the variable inserted in the html at all.

Can somebody help me fix this?

Thanks!

Upvotes: 1

Views: 240

Answers (3)

Omar Ali
Omar Ali

Reputation: 8617

You can either interpolate or concatenate. Find out more on this SO question: PHP variable interpolation vs concatenation

Upvotes: 1

Change

<p>Name goes here: <?php echo $name; ?> </p> 

to

<p>Name goes here: $name </p> 

and it'll work.

Switching to php mode by inserting <?php ... ?> makes no sense, because...

  1. you're already in php mode at that point and
  2. the tag won't get recognized by the php interpreter, because it is embedded into a string literal.

Upvotes: 2

pavel
pavel

Reputation: 27092

The line with passing name into email message should be:

<p>Name goes here: " . $name . "</p> 

Full $message content will be:

$message="
    <html>
    <head>
    <title>HTML email</title>
    </head>
    <body>
    <p>Name goes here: " . $name . "</p> 
    </body>
    </html>
"; 

Upvotes: 1

Related Questions