1

我正在尝试从 GAS MailApp 向虚拟传真号码发送电子邮件。电子邮件随附件一起发送,一切看起来都很完美,但由于某种原因,它看不到附件。我还直接从我的 gmail 发送了一个到相同的地址,它通过了。看源码,貌似最大的区别就是没有X-Attachment-Id或者Content-ID。不知道这是否有所作为。

定期电子邮件

Content-Type: application/pdf; name="000106.pdf"
Content-Disposition: attachment; filename="000106.pdf"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_kjyo04nj0
Content-ID: <f_kjyo04nj0>

来自 MailApp 的电子邮件

Content-Type: application/pdf; name="000106.pdf"
Content-Disposition: attachment; filename="000106.pdf"
Content-Transfer-Encoding: base64

代码.gs

function doGet() {
  return HtmlService.createTemplateFromFile('forms').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME)
  .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

function sendFax(data, file, faxto) {
  try {
    var contentType = data.substring(5, data.indexOf(';')),
      bytes = Utilities.base64Decode(data.substr(data.indexOf('base64,') + 7)),
      blob = Utilities.newBlob(bytes, contentType, file);
    
    MailApp.sendEmail(faxto + "@virtualfaxaddress.com", "faxaccesscode", "", {
                      attachments: blob
  });
    return 'Sent!';
  } catch (f) {
    return f.toString();
  }
}

表单.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script src="https://code.jquery.com/jquery.min.js"></script>
  </head>
  <body>
    <body>
<div class="center">
<div class="fax-form">
<form method="post">

<label for="faxto">Fax to #:</label><br>
<input type="tel" name="faxto" id="faxto"><br><br>

<label for="upload">Choose a file to upload:</label>
<input type="file" name="upload" id="upload" accept=".pdf, .jpg, .tiff, .png, .bmp, .gif, .rtf, .txt, .doc, .docx, .xls, .xlsx, .ppt, .pptx"><br><br>

<input type="button" value="Submit" id="submit" onclick="submitForm()">
</form>
<p id="progress"></p>
</div>
</body>
  </body>
  <script>
    var file,
      reader = new FileReader();

    // Upload the file to Google Drive
    reader.onloadend = function (e) {
      google.script.run
        .withSuccessHandler(showMessage)
        .sendFax(
          e.target.result,
          file.name,
          $('input#faxto').val()
        );
    };

    // Read the file on form submit
    function submitForm() {
      file = $('#upload')[0].files[0];
      showMessage('Uploading file..');
      reader.readAsDataURL(file);
    }

    function showMessage(e) {
      $('#progress').html(e);
    }
</script>
</html>

有谁知道这些 ID 在这种情况下扮演的角色?这可能是原因吗?如果是这样,我将如何修复它以便 MailApp 发送带有 ID 的附件?

4

1 回答 1

1

您的回复来看My best lead is the X-Attachment-ID and Content-ID, which I am guessing the Virtual Fax Machine is not seeing and therefore not recognizing the attachment.,当您想通过手动添加X-Attachment-Id和发送电子邮件时Content-ID,我认为 Gmail API 可以用作我的评论

在这种情况下,下面的示例脚本怎么样?

示例脚本:

请按如下方式修改您的脚本。X-Attachment-Id在此修改中,附件文件以和的值发送Content-ID。在这种情况下,请在高级 Google 服务中启用 Gmail API。

function sendFax(data, file, faxto) {
  var contentType = data.substring(5, data.indexOf(';')),
  bytes = Utilities.base64Decode(data.substr(data.indexOf('base64,') + 7)),
  blob = Utilities.newBlob(bytes, contentType, file);
  // MailApp.sendEmail(faxto + "@virtualfaxaddress.com", "faxaccesscode", "", {attachments: blob});

  var toEmail = faxto + "@virtualfaxaddress.com";
  var subject = "faxaccesscode";
  var textBody = '';
  var attachmentFile = Utilities.base64Encode(blob.getBytes());
  var attachmentId = "sampleId";

  var requestBody = `MIME-Version: 1.0\r\n` +
    `To: ${toEmail}\r\n` +
    `From: ${fromEmail}\r\n` +
    `Subject: ${subject}\r\n` +
    `Content-Type: multipart/mixed; boundary=boundaryboundary01\r\n\r\n` +
    `--boundaryboundary01\r\n` +
    `Content-Type: multipart/alternative; boundary=boundaryboundary02\r\n\r\n` +
    `--boundaryboundary02\r\n` +
    `Content-type: text/plain; charset=UTF-8\r\n\r\n` +
    `${textBody}\r\n\r\n` +
    `--boundaryboundary02--\r\n` +
    `--boundaryboundary01\r\n` +
    `Content-Type: ${contentType}; name="${file}"\r\n` +
    `Content-Disposition: attachment; filename="${file}"\r\n` +
    `Content-Transfer-Encoding: base64\r\n` +
    `X-Attachment-Id: ${attachmentId}\r\n` +
    `Content-ID: <${attachmentId}>\r\n\r\n` +
    `${attachmentFile}\r\n` +
    `--boundaryboundary01--\r\n`;

  var res = Gmail.Users.Messages.send({raw: Utilities.base64EncodeWebSafe(requestBody)}, "me");
  console.log(res)
}

结果:

运行上述脚本时,在附件文件中获得以下结果。

Content-Type: application/pdf; name="sample.pdf"
Content-Disposition: attachment; filename="sample.pdf"
Content-Transfer-Encoding: base64
X-Attachment-Id: sampleId
Content-ID: <sampleId>

笔记:

  • 如果您需要使用特定X-Attachment-IdContent-IDAPI,请修改上面的脚本。

参考:

于 2021-01-19T02:05:54.273 回答