Reputation: 29
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!
Upvotes: 1
Views: 208
Reputation: 798
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>";
?>
Upvotes: 0
Reputation: 668
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!
Upvotes: 0
Reputation: 1721
$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
Upvotes: 4