i am unable to concardinate i am getting error..

<?php
$subject = "You got a Project Invitation from ".echo $_SESSION['user_name'];

?>

Dani AI

Generated

Following and : the parse error came from using echo inside an expression. Instead you need the session value itself and to make sure the session is started and the key exists. Also guard against header-injection/newline characters when that value will go into an email subject.

<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$name = !empty($_SESSION['user_name']) ? $_SESSION['user_name'] : 'Unknown';
$name = preg_replace("/[\r\n]+/", ' ', $name);   // strip CR/LF to avoid header injection
$subject = sprintf('Project invitation from %s', $name);
?>

If you use PHP mail() directly, validate the recipient and build safe headers (charset, MIME version) so messages are not malformed or flagged as spam. Always remove newlines from user-controlled strings and validate addresses with filter_var(). For production, prefer a library that handles encoding and SMTP for you.

$to = 'recipient@example.com';
if (!filter_var($to, FILTER_VALIDATE_EMAIL)) {
    // handle invalid address
}

$headers = "From: MyApp <noreply@example.com>\r\n"
         . "MIME-Version: 1.0\r\n"
         . "Content-Type: text/plain; charset=UTF-8\r\n";

$message = "Message body here.";
$result = mail($to, $subject, $message, $headers); // check $result and server logs if not delivered

For reliability and easier UTF-8/attachment support, use PHPMailer (or similar) and send via SMTP. Minimal PHPMailer example:

use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'smtp-user';
$mail->Password = 'smtp-pass';
$mail->setFrom('noreply@example.com', 'MyApp');
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body    = $message;
$mail->send();

See the PHPMailer project for installation and full examples: PHPMailer on GitHub.

Recommended Answers

All 4 Replies

Hi,

Please remove echo statement in concate.

i.e.

$subject = "You got a Project Invitation from ". $_SESSION['user_name'];
echo $subject;

Please check and let me know.

Thanks,
Ajay

example :-

<?php
$subject = "You got a Project Invitation from ". $_SESSION['user_name'];
echo $subject;
?>

commented: Please check previous posts, you are just repeating - this is becoming a habit -3

I need to send it to mail

$subject = "You got a Project Invitation from ".echo $_SESSION['user_name'];
mail($to, $subject, $message, $headers);

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.