简体   繁体   English

xpage csjs代码未通过gmail API发送正确格式的内嵌图像和附件的电子邮件

[英]xpage csjs code not sending inline images and attachment in proper format email thru gmail api

My xpage has following fields 我的xpage具有以下字段

  1. Xpage with evo:InputRichtext ckeditor from this snippet : 来自此代码段的带有evo:InputRichtext ckeditor的Xpage:
  2. I'm using gmail api to send email. 我正在使用gmail api发送电子邮件。
  3. I'm adding formatted text, inline images and files in Ckeditor 我在Ckeditor中添加格式化的文本,嵌入式图像和文件
  4. Using button to get values from To, Subject and Ckeditor component using csjs. 使用按钮使用csjs从To,Subject和Ckeditor组件获取值。
  5. Message sent thru gmail server but not receiving wysiwyg format. 邮件通过gmail服务器发送,但未收到所见即所得格式。 The image is not displayed and only file name appears. 不显示图像,仅显示文件名。

I know it is cross platform encoding issue and I don't know what is that. 我知道这是跨平台编码问题,我不知道那是什么。

Here is button code: 这是按钮代码:

var to = XSP.getElementById("#{id:To}").value;
var subject = XSP.getElementById("#{id:Subject}").value;
var richCKEditor = CKEDITOR.instances["#{id:inputRichText1}"]; 
var dt=richCKEditor.getData();
var content = richCKEditor.dataProcessor.toHtml(dt);

 console.log(to);
 console.log(subject);
 console.log(content);

var email ="From: 'm' <"+to+">\r\n"+
"To:  "+ to +"\r\n"+
"Subject: "+subject+"\r\n"+"\r\n"+
"MIME-Version: 1.0\n"+
//"Content-Type:  text/html; charset=\"UTF-8\"\n" +
"Content-Type:  multipart/mixed; \n" +
content;
console.log(email);
auth();
send(email);

Here gmail api function I'm using with existing authentication from gmail api site 在这里我正在使用gmail api网站的现有身份验证使用的gmail api函数

function send(email) {
console.log(email);
sendMessage(email, function (response) {
                //console.log("Handed to Gmail API for sending");
                 {console.log(response)}
            });
            alert("Message sent");
        }

 function sendMessage(email, callback) {
            //auth();
            gapi.client.load('gmail', 'v1',function(){
                var base64EncodedEmail = btoa("MIME-Version: 1.0\n"+
                        "Content-Type:  text/html; charset=\"UTF-8\"\n" +
                    //  "Content-Type:  multipart/mixed; \n" +

                        //"Content-length: 5000\n" +
                        //"Content-Transfer-Encoding: message/rfc822\n"+
                        email).replace(/\//g,'_').replace(/\+/g,'-');
                 // alert("Message sending\n" + base64EncodedEmail.toString());
                console.log(base64EncodedEmail);
                  var request = gapi.client.gmail.users.messages.send({
                    'userId': 'me',
                    'resource': {
                      'raw': base64EncodedEmail
                    }
                  });
                  request.execute(callback);
            });


        }

When you send a Mime email with attachments and inline images, it is contained of many parts. 当您发送带有附件和嵌入式图像的Mime电子邮件时,它包含许多部分。

  • The HTML will be the text/html part. HTML将是text / html部分。
  • The images will be something like a image/jpeg or image/png part 图像将类似于image / jpeg或image / png部分
  • The attachments will be something like a application/pdf part 附件将类似于application / pdf部分

They are all tied together in a multi-part structure. 它们都以多部分结构捆绑在一起。

The inline images, should be located as siblings to a parent 'multipart/related' mime part. 内联图像应作为父“多部分/相关” mime部分的同级放置。 If there are attachments, they are located under a 'multipart/mixed' parent. 如果有附件,则它们位于“多部分/混合”父对象下。

If you were to send a mime email with attachments and inlines it would be in the following structure 如果要发送带有附件和内联的mime电子邮件,它将采用以下结构

  • multipart/mixed 多部分/混合
    • multipart/related 多/相关
      • text/html text / html的
      • image/jpeg 图像/ JPEG
      • image/jpeg 图像/ JPEG
    • application/pdf 应用程序/ PDF格式

When you call the getData function of the CKEditor, you are only getting the text/html mime part. 当您调用CKEditor的getData函数时,您只会得到text / html mime部分。 Within the html is some tags that are referencing an image somewhere . 在html中是一些标记,它们引用某处的图像。 And it will contain absolutely no information about the attachments. 并且它将绝对不包含有关附件的信息。

The image can be referenced 3 different ways 可以通过3种不同的方式引用该图像

  1. As a href to some location of the internet. 作为Internet某些位置的href。 ie. 即。 href="http://someserver.com/someimage.gif" If you are sending emails you probably do not want this unless you are happy with the person receiving the email to have to click 'show images in this email'. href =“ http://someserver.com/someimage.gif”如果您要发送电子邮件,则可能不希望这样做,除非您对接收电子邮件的人感到满意,必须单击“在此电子邮件中显示图像”。 And also that you can be sure the image is not a link to some intranet server that is not accessible to the email receiver. 同样,您可以确保该图像不是电子邮件接收者无法访问的某些Intranet服务器的链接。

  2. As a Data URI. 作为数据URI。 https://en.wikipedia.org/wiki/Data_URI_scheme This is where all the image data is actually located in the html within the img src tag. https://zh.wikipedia.org/wiki/Data_URI_scheme这是所有图像数据实际上位于img src标记中的html中的位置。 The CKEditor will actually allow you (in firefox) to paste in an image in this format, however If you are sending emails you do not want to allow this format either, as it is not supported by every email client. CKEditor实际上将允许您(在firefox中)以这种格式粘贴图像,但是,如果您要发送电子邮件,则也不希望允许这种格式,因为并非每个电子邮件客户端都支持这种格式。 Gmail will not show images in this format. Gmail将不会以这种格式显示图像。

  3. As an embedded image. 作为嵌入式图像。 This is where the image is stored as a sibling to the text/html in the mime structure as I have described above. 如上所述,这里是图像作为mime结构中text / html的同级存储的地方。 The image will have the content-disposition of 'inline' and it will have a Content-ID If you are sending emails this is the format that you really want as you can be sure the receiver will be able to see them. 该图像将具有“ inline”的内容配置,并且将具有一个Content-ID。如果您要发送电子邮件,则这是您真正想要的格式,因为您可以确保收件人可以看到它们。

If you must do everything client side, then you will need to find some way to get the embeddedimage and attachment data from the server to include in your call. 如果必须在客户端进行所有操作,则需要找到某种方法来从服务器获取内嵌图像和附件数据,以将其包括在呼叫中。 Otherwise you are better off trying to do this server-side but in any case you will need to get the server involved somehow. 否则,您最好尝试在服务器端进行操作,但是无论如何,您都需要以某种方式使服务器参与其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM