0

从“内容”字段中的邮戳附件功能发送电子邮件附件时,我不知道要执行哪种编码。

我已经尝试过以下方法将 pdf 文件转换为 base64 但找不到工作:

fs.readFileSync("./filename.pdf").toString('base64')
////////
pdf2base64("./filename.pdf").then(
        (response) => {
            base= response //cGF0aC90by9maWxlLmpwZw==
        }
    ).catch(
        (error) => {
            console.log(error); //Exepection error....
        }
    )
/////
 function base64_encode(file) {
    // read binary data
    var bitmap = fs.readFileSync(file);
    // convert binary data to base64 encoded string
    return new Buffer.from(bitmap.toString('utf-8'),'base64');
}

我尝试发送电子邮件的代码如下:

var client = new postmark.ServerClient("*****");
  client.sendEmail({
    "From": "example@abc.com",
    "To": "abc@abc.com",
    "Subject": "Test",
    "TextBody": "please find attached file of your agreement",
    "Attachments": [
      {
        "Name":  'index.pdf',        
     "Content":fs.readFileSync("./filename.pdf").toString('base64'),
       "ContentType": "application/pdf"
      }
    ]
  }).then((result) => {
    console.log("the result is :", result)
  }).catch((err) => {
    console.log("error is : ", err)
  });

我只希望它找到如何根据此电子邮件附件的要求进行编码的方法。我应该在内容字段中输入什么来发送无错误的文件

4

2 回答 2

1

您能就此联系我们的支持团队吗?他们肯定能够提供帮助。https://postmarkapp.com/contact

于 2019-07-05T14:41:51.743 回答
0

首先,您必须将文件转换为 Base64 字符串。

为此,您可以使用以下功能。

const blobToBase64 = blob => {
        const reader = new FileReader();
        reader.readAsDataURL(blob);
        return new Promise(resolve => {
          reader.onloadend = () => {
            resolve(reader.result);
          };
        });
      };

将 blob/文件转换为 Base64 字符串后,您已从 Base64 字符串中删除以下子字符串。

数据:应用程序/pdf;base64

您可以通过以下代码来做到这一点:

        const updatedBase64String = res.replace("data:application/pdf;base64,", "")

现在您可以在内容中使用该字符串。

您想要的完整代码

const blobToBase64 = blob => {
    const reader = new FileReader();
    reader.readAsDataURL(blob);
    return new Promise(resolve => {
      reader.onloadend = () => {
        resolve(reader.result);
      };
    });
  };
  
   blobToBase64(pdfConvertedToBlob).then(res => {
       const updatedBase64String =  res.replace("data:application/pdf;base64,", "")
          
       client.sendEmail({
          "From": "example@abc.com",
          "To": "abc@abc.com",
          "Subject": "Test",
          "TextBody": "please find attached file of your agreement",
          "Attachments": [
              {
                  "Name":  'index.pdf',        
                  "Content": updatedBase64String,
                  "ContentType": "application/pdf"
              }
           ]
       }).then((result) => {
           console.log("the result is :", result)
       }).catch((err) => {
           console.log("error is : ", err)
       });
    }

一件事,要记住。首先,您必须将本地 PDF 文件转换为BLOB

为此,您有两个选择。首先从 API 获取文件,并以BLOB格式获取它,如

axios({
    method: "post",
    url: deployedServer,
    data: data,
    headers: { "Content-Type": "application/json" },
    responseType: 'blob',
 })
  .then((pdfConvertedToBlob) => {
      // it is blob data
   });

其次,您可以通过输入类型文件对其进行转换,并在那里上传本地文件并使用它。您可以按如下方式使用它。

<input type="file" id="files" name="files" />

// now js code

if (window.File && window.FileReader && window.FileList && window.Blob) {
    document.getElementById('files').addEventListener('change', handleFileSelect, false);
 } else {
     alert('The File APIs are not fully supported in this browser.');
 }

 function handleFileSelect(evt) {
    let pdfConvertedToBlob = evt.target.files[0]; // FileList object

 }

希望对您有所帮助。

于 2022-02-09T13:16:31.120 回答