Member Avatar for Member #867314

I am looking to create a message with several lines, but when I put my string into the mailto statement I can't find a newline character that works.

$MESSAGE_BODY .= "Message: " . nl2br($_POST["message"]) . "\n";
			print_r($MESSAGE_BODY);
			echo "<a href=\"mailto:bearcatFulton@gmail.com?
			subject=Angus and Alfalfa Contact&
			body=$MESSAGE_BODY\">Send this message</a>

I've tried \n\r, <br>, and
, but none of these look right in the email.

Thanks for the help.

Dani AI

Generated

Short answer: mailto bodies must be percent-encoded and should contain CR+LF sequences for line breaks (clients vary, but that is the standard). was on the right track trying different break methods, but HTML breaks or raw HTML conversion won't produce real plain-text newlines inside a mail client.

A reliable workflow: normalize any incoming newlines to CR+LF on the server, then percent-encode the whole body (and the subject) before placing them in the mailto href. The percent-encoding turns the CR+LF bytes into the sequence clients expect. Using PHP's URL-encoding helper that follows RFC encoding rules avoids having spaces turned into plus signs by default.

Example pattern (PHP):

$text = $_POST['message'] ?? '';
// normalize all newline types to CRLF
$text = preg_replace('/\R/u', "\r\n", $text);
$body = rawurlencode($text);
$subjectEncoded = rawurlencode($subject);
echo '<a href="mailto:someone@example.com?subject=' . $subjectEncoded . '&body=' . $body . '">Send</a>';

Notes and cautions: the mailto scheme and newline encoding are defined in the mailto RFC; adherence gives the best cross-client behavior (RFC 6068). Use PHP’s rawurlencode for the query components so you get percent escapes rather than form-style plus signs (rawurlencode — PHP Manual). Also be aware of practical limits: very long bodies can hit URL length limits in browsers and some clients ignore body parameters or strip encoding. If you need reliable formatting, large content, or HTML emails, send mail server-side instead of relying on mailto.

Member Avatar for Member #867314

Used ascii newline character

%0A

I'm not sure if you'll need a %OD or not.

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.