简体   繁体   中英

Email textarea HTML code through PHP to display as html

Currently I have a fully working email system and I wanted to implement summernote which I have. However when I submit the form it seems to send as html code in text so it shows all the <> and no special formatting. So I thought. Is it sending to the script correctly so I echoed it out and it shows the code without the formatting.

Send:

<?php require '../settings.php';
/* Check all form inputs using check_input function */
$name= check_input($_POST['name'], "Enter your name");
$email= check_input($_POST['email']);
$message= $_POST['message'];
$subject= check_input($_POST['subject']);
/* From who and Reply to Who. */
$headers = 'From: '.$name.'\r\n';
$headers .= 'Reply-To: '.$administrationemail.'\r\n';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
/* Send the message using mail() function */
mail($email, $subject, $message, $headers);
/* Redirect visitor to the thank you page */
header('Location: /mail/');
exit();
}
?>

Form:

<form action="send.php" method="post">
<b>Your Name:</b><input class="form-control" type="text" name="name">
<b>Subject:</b> <input class="form-control" type="text" name="subject">
<b>Recipient E-mail:</b> <input class="form-control" type="text" name="email">
<b>Your Message:</b>
<textarea class="summernote" id="message" name="message"></textarea>
<input name="submit" type="submit" class="btn btn-theme" value="Send it!">
</form>

Any help figuring out this issue would be great. Thanks in advance.

What I am wanting to show in email:

Hello John Smith,

I have recieved your invoice and will pay shortly.

From Your Friend: Sir Smith John.

What I am getting shown:

<p><span style="font-weight: bold;">Hello John Smith,</span></p>
<p>I have recieved your invoice and will pay shortly.</p>
<p><span style="font-weight: bold;">From Your Friend:</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-style: italic;">Sir Smith John.</span><span style="font-weight: bold;"><br></span></p>

Function:

function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = preg_replace($wordlist, '****', $data);
if ($problem && strlen($data) == 0)
{
    show_error($problem);
}
return $data;
}

Use PHPMailer if you want flexibility. Its so good. Check out their website:

http://phpmailer.worxware.com/

You may need to adjust your headers, for arguments sake we'll just use the same method that is in the PHP docs.

$headers  = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $administrationemail . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Your \\r\\n must be inside double quotes as single quotes are for literal strings.

Your script work as intended, if you want to send your email text with special formatting you have to replace HTML tags with escape characters (ex. <br /> to /n ). You can do it with str_replace :

$htmlTags = array('<br />', '<br/>');
$escapeCharacter = '/n';
$final_message = str_replace($htmlTags, $escapeCharacter, $message);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM