Hi there,

here is my code and I don't know how to add <br /> to get a return in the email received:

<?php
$to = "myemail@gmail.com";
$subject = "From the website";
$email = $_REQUEST['email'] ;
$yname = $_REQUEST['name'] ;
$ycompany = $_REQUEST['company'] ;
$yphone = $_REQUEST['phone'] ;
$ymessage = $_REQUEST['message'] ;
$ywebsite = $_REQUEST['website'] ;
$message = $ymessage ."<br />"
$ywebsite ."<br />"
$yname ."<br />"
$ycompany ."<br />"
$yphone;
$headers = "From: $email";

if(($email=="") or ($message=="")){
	print "Sorry, you forgot to write your email or your message. ";
}
else{
$sent = mail($to, $subject, $message, $headers) ;
}

if($sent)
{print "Your mail was sent successfully, we will get back to you soon. <br />Thank you."; }
else
{print "We encountered an error sending your mail"; }
?>

Check the $message where I add like:
$message = $ymessage ."<br />"
$ywebsite ."<br />"
$yname ."<br />"
$ycompany ."<br />"
$yphone;

This does not work, help please!

Thank you!
Bouh

Dani AI

Generated

Two ways to make visible line breaks in the mail: send plain-text and use proper newline sequences, or send HTML and include actual HTML breaks plus the right MIME header. The original post had an assembly/syntax problem as well (missing concatenation/semicolon), so the message string may never have been built correctly. 's tip pointed toward the format issue; the following fills in the details and adds safer handling.

// assemble fields and convert user newlines to HTML breaks while escaping unsafe chars
$parts = array_map('trim', [$userMsg, $website, $fullName, $company, $phone]);
$safeParts = array_map(function($v){
    return nl2br(htmlspecialchars($v, ENT_QUOTES, 'UTF-8'));
}, $parts);
$bodyHtml = implode('<br />' . PHP_EOL, $safeParts);

// required headers for HTML email
$headers = implode("\r\n", [
    'From: ' . $fromEmail,
    'MIME-Version: 1.0',
    'Content-Type: text/html; charset=UTF-8'
]);

mail($recipient, $subject, $bodyHtml, $headers);

Notes and cautions: validate the sender with filter_var(..., FILTER_VALIDATE_EMAIL) and reject inputs containing CR/LF to avoid header injection (check for "\r" or "\n"). For plain-text emails, convert message lines into SMTP-safe newlines and consider wordwrap() to keep lines under 70 characters. For anything beyond simple notifications (attachments, reliable delivery, proper header handling), use a maintained library such as PHPMailer or SwiftMailer rather than raw mail().

This expands on the thread: the immediate fix suggested by resolves the visible-break symptom, while the above shows a robust way to assemble the message, set the correct headers for HTML output, and avoid common security and formatting pitfalls mentioned implicitly by 's original code.

Recommended Answers

All 2 Replies

just use "<br/>\r\n" or only "\r\n" (if plaintext message)

Thank you so much ZZZubec, I couldn't find an answer online! It works!

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.