@labnol/email-editor
v3.2.0
Published
A rich-text email editor for mail merge, with merge fields, image uploads and email-safe HTML
Maintainers
Readme
React Email Editor with Merge Fields and Image Uploads
A rich-text editor for composing email bodies in React. It writes email-safe HTML (inline styles, no classes), inserts {{merge fields}}, and uploads pasted, dropped and picked images through your own server.
It is the email editor in the Google Workspace add-ons by Digital Inspiration: Mail Merge, Document Studio, Email Google Sheets and Email Notifications for Google Forms, which together have 50 million installs.
Features
- Merge fields, fast. Type
{{to pick a field from a list that filters as you type, or use the Fields menu with search. Fields show as chips, and a field that is not in your list, usually a typo, shows in amber. - Fields stay whole. Bold, a colour or a link applied to part of
{{First Name}}covers the whole field, so a merge never leaves a tag open.<<First Name>>markers are kept too. - HTML that looks the same in the inbox. Inline styles only, and the editor's spacing is written into the HTML, so Gmail, Outlook and Apple Mail show the email as it was written.
- Images through your server. Pick, paste or drop an image; it uploads with a faded preview where it will go, and lands on a line of its own. Base64 images in pasted content are uploaded too, since email clients block them.
- An image menu. Select an image to set its size (sized for a 600 px email), alignment, alt text and link, or drag its handles to resize. The size is written as
widthandheight, which Outlook reads. - A link menu. Put the cursor in a link to see its address, open it, edit its text and address together, or remove it.
- Tables, quotes, lists, dividers, colours and alignment, all written as inline styles email clients keep.
- Right-to-left text. Arabic, Hebrew, Persian and Urdu paragraphs run right to left on their own, in a left-to-right editor too.
- A small API.
valueandonChange, with a ref to insert fields or text, read the HTML, and check for uploads still running before you send.
npm install @labnol/email-editorReact 18 or 19 is a peer dependency. Tiptap comes with the package. The toolbar uses Tabler Icons, built in under the MIT License (see licenses/).
Usage
import '@labnol/email-editor/style.css';
import { EmailEditor } from '@labnol/email-editor';
import toast from 'react-hot-toast';
const uploadImage = async (file) => {
// Send the image to your own server and resolve to its public URL
const body = new FormData();
body.append('file', file);
const response = await fetch('/api/upload', { method: 'POST', body });
if (!response.ok) {
toast.error('Sorry, the image could not be uploaded.');
return null;
}
const { url } = await response.json();
return url;
};
export const Editor = ({ html, onChange }) => (
<EmailEditor
value={html}
onChange={onChange}
onUploadImage={uploadImage}
onError={(message) => toast.error(message)}
fields={['First Name', 'Email', 'Company', 'What is your first name?']}
placeholder='Write your email. Use {{First Name}} and other fields to personalize it.'
/>
);Props
| Prop | Default | |
| --------------------------------- | -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| value | '' | The HTML to show. When it changes to something the editor did not send, the editor loads it, so an HTML source view can edit the same value. |
| onChange(value) | | Called with the content after the user stops typing for 300 ms, and right away on blur and unmount. |
| dir | 'ltr' | 'rtl' for an editor used mainly in Arabic, Hebrew, Persian or Urdu: mirrors the toolbar, and paragraphs with no letters run right to left. Every paragraph still follows its own text. Read once. |
| onUploadImage(file) | | Upload an image and resolve to its public http(s) URL, or to { url, width, height } to write its size (scaled to at most 600 px wide for Outlook), or to null when you have already told the user why it failed. If it throws, onError gets the message. Without it, the image button is hidden and pasted images are ignored. cloud-run/ has a sample upload service. |
| onError(message) | | Show a message to the user. Gets the image checks and upload failures. |
| fields | | Merge fields for the built-in Fields menu: strings, or { label, value } when the inserted name differs from the label. One toolbar button however many there are; past six it adds a search box, and long names wrap to two lines. Typing {{ in the text lists them too: keep typing to filter, Enter or Tab to insert, Escape to close. A field in the text that is not in the list shows as an amber chip, marked Unknown field; case and spaces don't count. |
| placeholder | | Shown while the editor is empty. |
| editable | true | |
| maxImageSize | 4 MB | In bytes. |
| minHeight, maxHeight | '14rem', '28rem' | The height of the empty editor below the toolbar, and the height past which the content scrolls. A CSS length, or a number of pixels. |
| className, style, ariaLabel | | On the outer element and the editable area. |
Ref
const ref = useRef(null);
<EmailEditor ref={ref} … />;
ref.current.insertMarker('Email'); // insert {{Email}} at the cursor
ref.current.insertText('Hello');
ref.current.getValue(); // the current HTML, without waiting for onChange
ref.current.flush(); // send a pending onChange now
ref.current.isUploading(); // true while an image uploads: check it before sending the email
ref.current.focus();
ref.current.editor; // the Tiptap editorMore
- The HTML it writes: links, empty lines, merge fields, images, pasted content and what is dropped
- Helpers: the functions for handling the same HTML and images outside the editor
- Tables
- Right-to-left languages
- Toolbar and tooltips
- Image upload service for Cloud Run, and the Apps Script relay that calls it
- Developing and publishing
Styling
Import @labnol/email-editor/style.css once. The editor has a fixed, neutral look and no border of its own, so put it in a container with the frame you want:
.email-body {
border: 1px solid #e5e5e5;
border-radius: 10px;
}Size
The editor is 711 kB minified, 194 kB gzipped, without React; about 70% of it is Tiptap and ProseMirror. The stylesheet is 12 kB, 3 kB gzipped.
License
MIT. The Tabler Icons are © Paweł Kuna, under the MIT License: licenses/tabler-icons.txt.
