Skip to content

Write a plugin which creates attachment #7

@gilles-hemmerle

Description

@gilles-hemmerle

I'm trying to create a plugin which get all images using data url (<img="data:..." />) into attachments because gmail does not read image in data url format.
As the signature settings don't allow to attach files to the message in order to render them without the use of an external url, the goal is to create signature with image as data url and then intercept the message before it's being sent to convert these image to images like <img src="cid:${attachment.cid}"> to allow all email clients to read them properly. The same way Evolution currently does on linux.

I've been able to create the plugin but I am stuck in the process of attaching the file. I did not find in the documentation how we are supposed to create an attachment to the mail in the applyTransformsForSendng method.

import { ComposerExtension, File } from 'mailspring-exports';

export default class ReplaceBase64Extension extends ComposerExtension {

  static applyTransformsForSending({ draftBodyRootNode, draft, recipient }) {
    // code pour intercepté le mail, regarder toutes les images qui utilisent une url en base64 
    // et remplacer par des fichiers en pièce jointe
    // ...
    console.log("[BASE64]", "will intercept message", { draftBodyRootNode, draft, recipient })

    let body = draftBodyRootNode.innerHTML;
    let matches = body.match(/<img[^>]*src="data:image\/(.*);base64,([^"]*)"[^>]*>/g);

    if (!matches) {
      return;
    }

    matches.forEach(match => {
      let data = match.match(/src="data:image\/(.*);base64,([^"]*)"/);
      console.log("Attachments to add...", data);

      let type = data[1];
      let base64 = data[2];

      /****** Part which is not working
       
      let attachment = File.fromBase64(base64, `image.${type}`);
      draft.attachments.push(attachment);

      *******/

      body = body.replace(match, `<img src="cid:${attachment.cid}">`);
    });

    draftBodyRootNode.innerHTML = body;
    console.log("Replaced body", body);
  }
}

Is there any solution for a plugin to dynamically create an attachment?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions