Vue Letter Email Display Component
Vue-letter is a Vue.js component designed for displaying HTML or plain text email messages within Vue applications. It functions as a direct port of the `react-letter` library, offering features like wrapping content in an iframe, rewriting external resource and link URLs, and specifying allowed URL schemas. The package helps developers render email bodies while addressing potential security and styling concerns. Currently at an early version (0.2.1), it is under active development, implying its API and features are still evolving. Its main purpose is to simplify the secure rendering of email content, particularly targeting rendering compatibility similar to Gmail's approach.
Common errors
-
Failed to resolve component: Letter
cause The `Letter` component was not correctly imported or registered in your Vue application. This typically happens in Single File Components (SFCs) if the import path is wrong or in non-SFC setups if `app.component('Letter', Letter)` is missing.fixEnsure `import { Letter } from 'vue-letter';` is present in your `<script setup>` block or in the component's script section. If not using `<script setup>`, register it globally with `app.component('Letter', Letter)` or locally in your component's `components` option. -
Property 'html' / 'text' does not exist on type 'IntrinsicAttributes & ...'
cause This TypeScript error indicates that you might be passing properties to the `Letter` component that are not explicitly defined in its type declarations, or there's a type mismatch for the `html` or `text` prop.fixVerify that you are passing `html` and `text` props as `String` types, as specified in the component's attributes. Ensure your TypeScript setup correctly recognizes the `vue-letter` types. If you're on an older Vue/TypeScript version, updating might resolve the issue, or you might need to cast the prop type temporarily.
Warnings
- breaking The package is at an early version (0.2.1), which means its API is highly unstable and subject to significant breaking changes in minor or even patch releases. Developers should pin to exact versions and review changelogs closely for updates.
- gotcha Despite its `react-letter` origin mentioning 'automatic sanitization', `vue-letter`'s README explicitly labels the `html` and `text` props as taking 'Unsanitized e-mail HTML contents'. This creates a critical security ambiguity. Users MUST assume the component does not perform sufficient sanitization and are responsible for pre-sanitizing any untrusted HTML or text input to prevent Cross-Site Scripting (XSS) vulnerabilities.
- gotcha The `useIframe` prop, when set to `true`, wraps the email content in an `<iframe>`. While this provides strong style isolation, it can introduce challenges for responsive design, inter-frame communication, and accessing elements within the iframe from the parent application.
- gotcha The `allowedSchemas` prop defaults to `['http', 'https', 'mailto']`. Depending on the application's security requirements, this list might be too permissive or too restrictive, potentially allowing unintended protocols or blocking necessary ones.
Install
-
npm install vue-letter -
yarn add vue-letter -
pnpm add vue-letter
Imports
- Letter
const Letter = require('vue-letter');import { Letter } from 'vue-letter';
Quickstart
<script setup>
import { Letter } from 'vue-letter';
</script>
<template>
<div id="app">
<Letter html="<p>Hello <strong>World</strong>!</p><img src='https://example.com/logo.png'>" />
<Letter text="This is a plain text email. Line breaks should be preserved." />
<Letter
:html="`<p style='color: red;'>Malicious script: <script>alert('XSS')</script></p>`"
:useIframe="true"
iframeTitle="Email Subject"
:rewriteExternalLinks="(url) => `https://example.com/redirect?url=${encodeURIComponent(url)}`"
:allowedSchemas="['http', 'https']"
/>
</div>
</template>