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

Write a plugin which creates attachment #7

Open
gilles-hemmerle opened this issue Jan 30, 2023 · 0 comments
Open

Write a plugin which creates attachment #7

gilles-hemmerle opened this issue Jan 30, 2023 · 0 comments

Comments

@gilles-hemmerle
Copy link

gilles-hemmerle commented Jan 30, 2023

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?

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

1 participant