Hi,

Below code gives me this error: "syntax error, unexpected" in line 5

<?php
$to = "";
$subject = "test fname, email,phone,nn";
$Sender = $_POST["email"];
$message = Name: echo $_POST["fname"] . "\r\n". ///////////////////////line 5
Email Address: <?php echo $_POST["email"] . ?> "\r\n".
Phone No: echo $_POST["phone"] . "\r\n" ;
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

$headers = "From: ".$Sender . "\r\n" .
'Reply-To: '.$Sender. "\r\n".
'X-Mailer: PHP/' . phpversion();

mail($to,$subject,$message);
?>


pls advise.
Thx.

Dani AI

Generated

Quick diagnosis: the parse error comes from a mix of unescaped quotes and invalid PHP syntax. The $to line in 's post embeds an HTML anchor (with double quotes) inside a double-quoted PHP string, and the $message line tries to mix literal text, echo and even a <?php tag inside an assignment. Those cause the PHP parser to fail. There is also a security problem: putting raw $_POST values directly into headers (From/Reply‑To) opens the door to header injection.

Concise fixes to apply:

  • Keep the recipient ($to) as a plain email string (or "Name "), not an HTML anchor.
  • Build the message as one string (concatenation, interpolation or heredoc) — do not place echo or <?php inside an assignment.
  • Validate and sanitize inputs: filter_var for email, trim()+htmlspecialchars() for output, and reject any CR/LF characters in header-sourced values to prevent header injection.
  • Assemble headers in an array and implode("\r\n", $headers) for the fourth mail() parameter.
  • For HTML emails use proper HTML (paragraphs or <br/>) and the Content-Type: text/html header. was right about quote issues and was right about not opening PHP inside PHP; 's point about HTML breaks is valid for HTML bodies.

Example (safe, minimal pattern):

<?php
$fname = isset($_POST['fname']) ? trim($_POST['fname']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { exit; }
foreach ([$fname, $email, $phone] as $v) {
    if (preg_match("/[\r\n]/", $v)) { exit; } // prevent header injection
}

$safeName  = htmlspecialchars($fname, ENT_QUOTES, 'UTF-8');
$safePhone = htmlspecialchars($phone, ENT_QUOTES, 'UTF-8');

$message = <<<HTML
<p>Name: {$safeName}</p>
<p>Email Address: <a href="mailto:{$email}">{$email}</a></p>
<p>Phone No: {$safePhone}</p>
HTML;

$to = 'safiullah12@hotmail.com';
$subject = 'test fname, email, phone';
$headers = [
  'MIME-Version: 1.0',
  'Content-type: text/html; charset=UTF-8',
  'From: ' . $email,
  'Reply-To: ' . $email,
];

mail($to, $subject, $message, implode("\r\n", $headers));
?>

For anything beyond simple sends (attachments, authentication, robust header handling), use a library such as PHPMailer or SwiftMailer and test with a local mail-catcher to avoid delivery surprises.

Recommended Answers

All 4 Replies

i am trying to get a output as below:

Name: abc
Email Address:
Phone No: 11111111


Thanks again.

Hi,

Below code gives me this error: "syntax error, unexpected" in line 5

<?php
$to = "";
$subject = "test fname, email,phone,nn";
$Sender = $_POST["email"];
$message = Name: echo $_POST["fname"] . "\r\n". ///////////////////////line 5
Email Address: <?php echo $_POST["email"] . ?> "\r\n".
Phone No: echo $_POST["phone"] . "\r\n" ;
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

$headers = "From: ".$Sender . "\r\n" .
'Reply-To: '.$Sender. "\r\n".
'X-Mailer: PHP/' . phpversion();

mail($to,$subject,$message);
?>


pls advise.
Thx.

I can't say for sure, but I believe that you need to use single quotes inside the instead of the double quotes.

Your code is a syntactical nightmare.
Strings in PHP have to be enclosed in single or double quotes. ($message = Name)
You cannot open a <?php tag inside an already opened one.
A tip: do not mix HTML and PHP code. Write code like yours this as pure PHP without ever closing the PHP bracket.

Hello,

This might work:

<?php 
$to = "safiullah12@hotmail.com";
$subject = "test fname, email,phone,nn";
$Sender = $_POST["email"];
$message = " Name: ".$_POST['fname']."\r\n <br /> Email Address: ".$_POST['email']."\r\n <br /> Phone No: ".$_POST["phone"]."\r\n";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0 \r\n Content-type:text/html;charset=iso-8859-1 \r\n From: ".$Sender."\r\n Reply-To: ".

mail($to,$subject,$message,$headers);
?>

I added <br/> because if you're sending as html you will need it.

Calum

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.