0

I want an email to be sent in HTML and inside this email i need to pass data from php form. The email is sent successfully as an html email but it doesn't display data passed from PHP VARIABLE.

$name = "PHP TEXT";
$headers = "From: " ."[email protected]" . "\r\n";
$headers .= "Reply-To: ". "[email protected]" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = "<html><body><h1>HTML TEXT <?php echo $name; ?></h1></body></html>";

$email_to = '[email protected]'; //the address to which the email will be sent
$subject = "test";
$email    =   "[email protected]";

if (mail($email_to, $subject, $message, $headers)) {
    echo 'sent'; // success      
} else {
    echo 'failed'; // failure    
}

5 Answers 5

5

This line:

$message = "<html><body><h1>HTML TEXT <?php echo $name; ?></h1></body></html>";

it should be like:

$message = "<html><body><h1>HTML TEXT $name </h1></body></html>";

or

$message = "<html><body><h1>HTML TEXT " . $name . " </h1></body></html>";

<?php and ?> is used to specify that text inside it is a PHP code, not HTML. Here are some details about that.

But you are already in PHP code block and instead of specifying another PHP block, you should use string concatenation.

Sign up to request clarification or add additional context in comments.

Comments

2

you aren't using any of the form values in your script. do you know how $_POST or $_GET work?

you are setting $name to a hardcoded value, so it will always be that value when you use it, and also, since you are creating the string with PHP, you don't need to echo php from within this line. i.e. (since you are using double quotes)

$message = "<html><body><h1>HTML TEXT $name;</h1></body></html>";

so, if you can post your HTML form, your $name assignment will probably be something like

$name  = $_POST['name']

but you are strongly encouraged to sanitize and validate all user supplied info. It should be a fairly simple Google search.

Comments

1

An alternative solution can be closing the " before like

$message = "<html><body><h1>HTML TEXT ".$name." how are you?</h1></body></html>";

No need for the starting and closing of PHP as you are already in PHP workspace.

Comments

1

Try this..

$message = "<html><body><h1>HTML TEXT".$name."</h1></body></html>";

You are assigning the html to a php variable. So you don't have to echo the variable there.. Just use proper concatination

Comments

1

The $message variable itself has been used wrongly.

$message = "<html><body><h1>HTML TEXT <?php echo $name; ?></h1></body></html>";

It should be either

$message = "<html><body><h1>HTML TEXT  $name </h1></body></html>";

or this:

$message = "<html><body><h1>HTML TEXT ".$name." </h1></body></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.