简体   繁体   English

通过PHP通过电子邮件发送textarea HTML代码以显示为html

[英]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. 目前,我有一个可以正常使用的电子邮件系统,我想实施我拥有的summernote。 However when I submit the form it seems to send as html code in text so it shows all the <> and no special formatting. 但是,当我提交表单时,它似乎以文本形式的html代码发送,因此它显示所有<>并且没有特殊格式。 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. 如果需要灵活性,请使用PHPMailer。 Its so good. 它是那么好。 Check out their website: 查看他们的网站:

http://phpmailer.worxware.com/ 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. 您可能需要调整标题,出于参数的考虑,我们将仅使用PHP文档中的相同方法。

$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. \\ r \\ n必须在双引号内,因为单引号用于文字字符串。

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 ). 您的脚本按预期工作,如果您想以特殊格式发送电子邮件文本,则必须用转义字符(例如, <br />/n )替换HTML标签。 You can do it with str_replace : 您可以使用str_replace做到这一点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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