Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying inline attachments #6

Open
pmweeks98 opened this issue Aug 29, 2022 · 2 comments
Open

Displaying inline attachments #6

pmweeks98 opened this issue Aug 29, 2022 · 2 comments

Comments

@pmweeks98
Copy link
Collaborator

How to display inline attachments using readEml? Currently displaying as a broken image with the image and the inline attachment in the attachment array

@c-harding
Copy link
Collaborator

As part of my sanitisation code using the xss library, my definition of IFilterXSSOptions (second argument to xss function) includes a safeAttrValue override, in which I replace href and src values starting with the string cid: with the corresponding base64 URL of the attachment.

@jimklonowski
Copy link

jimklonowski commented Dec 9, 2022

As part of my sanitisation code using the xss library, my definition of IFilterXSSOptions (second argument to xss function) includes a safeAttrValue override, in which I replace href and src values starting with the string cid: with the corresponding base64 URL of the attachment.

I had a similar requirement and followed the above comment to come to my solution:

import xss from 'xss'
import { readEml } from 'eml-parse-js'
...
async fetchAndParseEmail () {
  try {
    this.loading = true
    const { data: eml } = await this.$axios.get('/case/get-email?id=1234')

    // read eml data and output json
    readEml(eml, (err, json) => {
      this.parsedEml = json
    })

    // find all <img> tags in the html with regex
    const images = this.parsedEml.html?.match(/<img [^>]*src="[^"]*"[^>]*>/gm)

    // for each image, replace src cid data with base64 data taken from attachments
    images?.forEach(img => {
      const base64img = new xss.FilterXSS({
        onTagAttr: (tag, name, value) => {
          if (/^cid:/ig.test(value)) {
            const attachment = this.parsedEml.attachments?.find(x => x.id.includes(value.substr(4)))
            return attachment ? `${name}="data:image/png;base64, ${attachment.data64}"` : ''
          }
        }
      }).process(img)

      // replace the img tags in the html
      this.parsedEml.html = this.parsedEml.html.replace(img, base64img)
    })
  } catch (error) {
    console.error(error)
  } finally {
    this.loading = false
  }
}

Repository owner deleted a comment from yuanye2005king Feb 21, 2024
Repository owner deleted a comment from qywang2012 Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants