how to create an wysiwyg html editor in <textarea name="sender_message"></textarea> is it possible when send an email from a php form?

Dani AI

Generated

Short answer: yes — replace the textarea with a client-side WYSIWYG and submit its HTML the same way you submit any form field. Popular editors (TinyMCE, CKEditor, Quill) all support form workflows and make the textarea editable or provide an easy way to copy editor HTML into a form field. (tiny.cloud)

Practical integration notes and examples:

  • TinyMCE and CKEditor can be initialized to replace a textarea and will sync to the textarea so your existing form submit works. See their docs for selector and form integration. (tiny.cloud)
<!-- TinyMCE example -->
<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>
<textarea id="sender_message" name="sender_message"></textarea>
<script>
  tinymce.init({ selector: '#sender_message' });
</script>
  • Quill uses a content DIV, so copy its HTML into a hidden input before submit (example in Quill quickstart). (quilljs.com)

Security and storage:

  • Never trust editor output on the server. Sanitize before saving or emailing. For PHP, HTMLPurifier is a robust, standards-aware option; for client-side filtering DOMPurify is useful (but server-side filtering is required for real safety). Also store text as UTF-8 and validate length/types. ()
require_once '/path/to/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
$clean = $purifier->purify($_POST['sender_message']);

Email-sending tips:

  • HTML email needs inlined CSS and client testing. Use inliners/pipelines (Premailer, etc.) and check feature support on CanIEmail before trusting advanced CSS. Also follow ’s reminder about using the proper headers when sending HTML mail, and test messages in several clients. (premailer.github.io)

Notes: If the goal is simply to let users compose formatted messages then send them, the flow is: editor -> sanitize server-side -> inline styles for email (if sending) -> send/store. This complements ’s and ’s thread points without changing your current form structure.

Recommended Answers

All 4 Replies

Can you give a little more detail?

I'm not sure if you're wanting to fill in an HTML email with data from a form, or use a field in a form to create an HTML document, or use some kind of drag and drop HTML editor.

You could do something like:
(thisfile.php)

<!-- put your own header stuff above this line, use your imagination -->
<body>
<?php
if (isset($_POST['submit']))
 {
 echo $_POST['sender_message'];
 }
if (isset($_POST['post']))
 {
 // put your email code here.
 }
?>
<hr /><hr />
<form action='thisfile.php' method='post'>
<textarea name="sender_message">$message</textarea>
<input type='submit' name='submit' value="View" />
<input type='submit' name='post' value="Post Email" />
</body>

I don't know, anyone else have a better solution?

David

Oops, I forgot a major part of that.

In order to send HTML as email you have to put some additional info in your email headers.

$headers = "From: no-reply@placelogo.com \r\n";  // replace with your email address
  $headers .= "Reply-To: no-reply@placelogo.com \r\n";
  $headers .= "MIME-Version: 1.0 \r\n";
  $headers .= "Content-Type: text/html; charset=ISO-8859-1 \r\n";
  $emailto = "addressee@whereever.com";            // replace with the addressee
  $subject = "Subject of email";                   // replace with the subject line
  mail($emailto,$subject,$message,$headers);

David

Wrong:

<textarea name="sender_message">$message</textarea>

Right:

<textarea name="sender_message"><?php echo $message; ?></textarea>

thanks David its help me.

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.