1

I am attempting to send an email to a virtual fax number from GAS MailApp. The email sends with the attachment and everything looks perfect, but for some reason it doesn't see the attachment. I also sent one to the same address from my gmail directly and it goes through. Look at the source, it looks like the biggest difference is that there is no X-Attachment-Id or Content-ID. No idea if this makes a difference.

Regular email

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>

Email from MailApp

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

Code.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();
  }
}

forms.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>

Does anyone know the role these IDs play in a situation like this? Could this be the cause? If so, how would I go about fixing it so that MailApp sends attachment with IDs?

NMALM
  • 378
  • 2
  • 19
  • Can you provide the detail specification of `virtual fax` you want to use? – Tanaike Jan 16 '21 at 00:50
  • Sorry, do you mean the virtual fax address and fax access code? If so, I, unfortunately, can't disclose those. Here is the info on the virtual faxing for the Jive network: https://support.goto.com/connect/help/how-do-i-send-a-virtual-fax-gotoconnect-send-virtual-fax – NMALM Jan 16 '21 at 01:16
  • Thank you for replying. I have to apologize for my poor English skill. About `do you mean the virtual fax address and fax access code?`, it's no. I thought that when the specification of `virtual fax` you want to use can be known, the reason of your issue might be able to be known. So I would like to check the document you provided `https://support.goto.com/connect/help/how-do-i-send-a-virtual-fax-gotoconnect-send-virtual-fax`. – Tanaike Jan 16 '21 at 01:19
  • Thank you, Tanaike. Your English is perfect and your help is always appreciated. I wish I could tell further which side the error is on. My best guess right now is that is has to do with the way Gmail sends the attachment from the MailApp.sendEmail function. – NMALM Jan 16 '21 at 01:26
  • When I saw the document you provided, unfortunately, I couldn't find about `X-Attachment-Id` and `Content-ID`. I deeply apologize for this. So, as a test, when you modify `MailApp.sendEmail` to `GmailApp.sendEmail`, what result will you obtain? Because when the method is changed, the headers are also changed. But I'm not sure whether this is the direct solution, I apologize for this. And, when this was not the solution, how about sending the email using Gmail API? – Tanaike Jan 16 '21 at 01:42
  • I've tested out the GmailApp, but it didn't work either. When I checked the original message in Gmail, it also didn't have the X-Attachment-ID or Content-ID. I'm not sure what part these play, but so far they're the only thing that appears significantly different from sending an email directly from Gmail. Looking into Gmail API now. Thank you, Tanaike! – NMALM Jan 18 '21 at 17:41
  • Thank you for replying. I deeply apologize my comment was not useful for your situation. – Tanaike Jan 19 '21 at 00:39
  • At this point, I can't determine where the issue is. 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. I will continue to experiment and follow up with any information I find. I have not ruled out the Gmail API and may switch this to a python project. Thank you for your help. – NMALM Jan 19 '21 at 00:55
  • Thank you for replying. From your replying, I proposed a sample script as an answer. Could you pleased confirm it? If that was not the direct solution of your issue, I apologize. – Tanaike Jan 19 '21 at 02:05

1 Answers1

1

From your replying of 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., when you want to send the email by manually adding X-Attachment-Id and Content-ID, I think that Gmail API can be used as my comment.

In this case, how about the following sample script?

Sample script:

Please modify your script as follows. In this modification, the attachment file is send with the values of X-Attachment-Id and Content-ID. In this case, please enable Gmail API at Advanced Google services.

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)
}

Result:

When above script is run, the following result is obtained at the attachment file.

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

Note:

  • If you are required to use the specific X-Attachment-Id and Content-ID for the API you want to use, please modify above script.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165