PHP variables aren't expanded when inside HTML which is by itself inside PHP
I transfer emails from my Right To Left (RTL) contact form → to my local email box (powered by Roundcube).
Emails reach my email box (inbox) just fine but I have a problem were PHP variables aren't expanded inside the string of the $message
variable so I get the literal variables instead their values sent via the form → although I have tried various ways to escape them as PHP in HTML which is by itself in PHP (I might have made a mistake somewhere along the way).
PHP code
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$topic = $_POST['topic'];
$date = $_POST['date'];
$time = $_POST['time'];
$notes = $_POST['notes'];
$to = '[email protected]';
$subject = 'הודעת אימייל חדשה מאת: ' . $name;
$message = '
<html>
<body dir="rtl" style="text-align: right;">
<b>שם: </b> $name <br>
<b>אימייל: </b> $email <br>
<b>טלפון: </b> $phone <br>
<b>פנייה בנושא: </b> $topic <br>
<b>יום רצוי לפגישה מרחוק (אם יש): </b> $date <br>
<b>שעה רצויה לפגישה מרחוק (אם יש): </b> $time <br>
<b>הערות (אם יש): </b> $notes <br>
</body>
</html>
';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=utf-8';
mail($to, $subject, $message, implode("\r\n", $headers));
?>
Current Output
שם: $nameאימייל: $email
טלפון: $phone
פנייה בנושא: $topic
יום רצוי לפגישה מרחוק (אם יש): $date
שעה רצויה לפגישה מרחוק (אם יש): $time
הערות (אם יש): $notes
Desired output:
שם: שם_רלוונטיאימייל: אימייל_רלוונטי
טלפון: טלפון_רלוונטי
פנייה בנושא: נושא_רלוונטי
יום רצוי לפגישה מרחוק (אם יש): SOME_DATE
שעה רצויה לפגישה מרחוק (אם יש): SOME_HOUR_AND_MINUTE
הערות (אם יש): הערה_רלוונטית
My question
As a non PHP programmer I ask how would you suggest to solve that problem?
1 answer
In PHP the single quotation mark ('x'
) is reading the content as raw values. This means, that escape sequences, such as \n
, or variable interpolation, such as $x
, is not supported.
There are three possible ways to resolve that issue:
-
String concatenation. You can concatenate strings with the
.
operator:$name = 'JohnDoea'; 'hello ' . $name == 'hello JohnDoea'
-
Double-quote string. Instead of using a single-quotation mark, you can use double-quoted strings (
"x"
). They support escape sequences and variable interpolation:$name = 'JohnDoea'; "hello $name" == 'hello JohnDoea'
In this syntax, you need to keep in mind, though, to escape the double quotes in the HTML with a backslash (
\"
). -
Heredoc syntax. PHP also has a string format called Heredoc. This allows you to easily include long strings with escape sequences and variable interpolation:
$name = 'JohnDoea'; $string = <<<LABEL hello $name LABEL; $string == 'hello JohnDoea'
1 comment
Your help is appreciated, I took an heredoc approach. I would gladly publish how I have implemented it for the form (giving credit to you, of course).
3 comments
@Art_Of_Code, my message isn't formatted correctly, the source code should indicate the RTLness in both
Current output
andDesired output
. — JohnDoea 14 days agoHave you tried using a heredoc string? https://stackoverflow.com/a/1848974 — Alexei 14 days ago
Yes, according to the answer by luap42 and your comment I went in that direction and it worked ! Thanks, — JohnDoea 14 days ago