[英]google apps script to gotoconnect / jive virtual fax - not finding attachment from email
我正在嘗試將 email 發送到 GAS MailApp 的虛擬傳真號碼。 email 隨附件一起發送,一切看起來都很完美,但由於某種原因它看不到附件。 我還直接從我的 gmail 發送了一個到相同的地址,它通過了。 看源碼,貌似最大的區別就是沒有X-Attachment-Id或者Content-ID。 不知道這是否有所作為。
常規 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>
來自 MailApp 的 Email
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();
}
}
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>
有誰知道這些 ID 在這種情況下扮演的角色? 這可能是原因嗎? 如果是這樣,我將如何修復它以便 MailApp 發送帶有 ID 的附件?
從您對我最好的線索的回復中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
發送 email 時,我認為 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-Id
和Content-ID
,請修改上述腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.