1

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!

3 Answers 3

2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Worked. Got the logic.
1

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

Comments

1

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

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.