2017-02-05 5 views
11

자바 스크립트에서 Gmail API를 사용하여 HTML 본문 및 ~ 100KB PDF 첨부 파일을 보내면 첨부 파일이 발신자의 Gmail 전송 메일에 첨부 된 것으로 올바르게 표시됩니다. 폴더에 있지만받는 사람의 메시지에는 나타나지 않습니다.Gmail API와 함께 보낸 메시지의 첨부 파일이 누락되어 있지만 수신자 만 수신

는 API 호출하는 POST에 :

https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media 

API로 전송 된 요청 본문은 다음과 같습니다

{ 
    "headers": { 
    "Authorization": "Bearer authToken-removedForThisPost" 
    }, 
    "method": "POST", 
    "contentType": "message/rfc822", 
    "contentLength": 134044, 
    "payload": "exampleBelow", 
    "muteHttpExceptions": true 
} 

이 페이로드는 모습입니다 같은 :

MIME-Version: 1.0 
To: =?utf-8?B?TWlrZSBD?=<[email protected]> 
CC: =?utf-8?B?TWlrZSBD?=<[email protected]> 
BCC: =?utf-8?B??=<[email protected]> 
From: =?utf-8?B?TWlrZSBxWXsd2lr?=<[email protected]> 
Subject: =?utf-8?B?subjectLine-removedForThisPost?= 
Content-Type: multipart/alternative; boundary=__boundary__ 

--__boundary__ 
Content-Type: text/plain; charset=UTF-8 
Content-Transfer-Encoding: base64 

base64EncodedStringHere-removedForThisPost 

--__boundary__ 
Content-Type: text/html; charset=UTF-8 
Content-Transfer-Encoding: base64 

base64EncodedStringHere-removedForThisPost 

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

base64EncodedStringHere-removedForThisPost 

--__boundary__-- 

참고 : Gmail API Uploading Attachments documentation에는 간단한 첨부 파일 (5MB 미만)을 업로드 할 때 Content-Length이 필요합니다. 내 코드가 PDF 첨부 파일 총 바이트 수의 정수 값을 생성하도록 만들었습니다. 그러나 페이로드에는 Content-Length이 포함되어 있지 않습니다.

메시지의 내용 유형을 multipart/mixed으로 변경하려고 시도했습니다. 이는 PDF 첨부 파일이받는 사람의 메시지에 올바르게 첨부되어 있지만 HTML 본문이 일반 텍스트 (HTML 태그 이 표시되어 있으며 HTML로 렌더링 된 HTML 컨텐트가 포함 된 noname.html이라는 추가 첨부 파일이 있습니다.

받는 사람의 메시지에있는 전자 메일에 HTML 렌더링 본문과 PDF 첨부 파일이 모두 포함되도록해야합니다.

업데이트 : 원시 이메일 메시지의 예는 here입니다. 메시지가 왼쪽에 있고 메시지가 오른쪽에 있습니다.

+0

당신은 ("__ 경계 __"='경계') 당신의 경계를 인용 최종 boudnary를 사용하여를 시도 ('--__ 경계 __--')? [** this ** ** (http://pastebin.com/SRZjEGfT)와 같은 무언가를 시도해보고 작동하는지 확인하십시오. – Tholle

+0

내 페이로드 DID에'--__ boundary __--'의 최종 경계가 포함되어 있음을 알았지 만 console.log 메시지가 super long 첨부 base64 문자열로 인해 잘 렸기 때문에 여기에 붙여 넣었을 때 잘립니다. 큰 따옴표에 관해서는 첫 번째 Content-Type : 줄에 추가했지만 동작을 전혀 변경하지 않았습니다. – mike

답변

1

그냥 교체 :

Content-Type: multipart/alternative; boundary=__boundary__

을 위해 :이 JS로 작성 내 전체 기능은

Content-Type: multipart/mixed; boundary=__boundary__

입니다

기능 createMimeMessage_ (MSG) {

,536,

var nl = "\ n"; var boundary = "ctrlq_dot_org";

VAR의 mimeBody =

"MIME-Version: 1.0", 
"To: "  + msg.to.email,//+ encode_(msg.to.name) + "<" + msg.to.email + ">", 
"Cc: "  + msg.cc.email, 
"Bcc: "  + msg.bcc.email, 
"From: " + msg.from.email,//+ encode_(msg.from.name) + "<" + msg.from.email + ">", 
"Subject: " + encode_(msg.subject), // takes care of accented characters 
"In-Reply-To: " + (msg.reply_to || ""), 
"References: " + (msg.reply_to || ""), 

"Content-Type: multipart/mixed; boundary=" + boundary + nl, 
"--" + boundary, 

// "Content-Type: text/plain; charset=UTF-8", 
// "Content-Transfer-Encoding: 7bit", 
// "Content-Disposition: inline" + nl, 
// msg.body.text + nl, 
// "--" + boundary, 

"Content-Type: text/html; charset=UTF-8", 
"Content-Transfer-Encoding: base64" + nl, 
new Buffer(msg.body.text).toString('base64') + nl, 

];

for (var i = 0; i < msg.files.길이; 내가 ++) {

var attachment = [ 
    "--" + boundary, 
    "Content-Type: " + msg.files[i].mimeType + '; name="' + msg.files[i].fileName + '"', 
    'Content-Disposition: attachment; filename="' + msg.files[i].fileName + '"', 
    "Content-Transfer-Encoding: base64" + nl, 
    msg.files[i].bytes 
]; 

mimeBody.push(attachment.join(nl)); 

}

mimeBody.push ("-"+ 경계 + "-"); //console.log(mimeBody);

return mimeBody.join (nl);

은}

+0

본인의 원래 질문에서 이미 시도한 바가 있습니다. 이것은 PDF 첨부 파일이받는 사람의 메시지에 올바르게 첨부되었지만 메시지의 HTML 본문이 일반 텍스트로 렌더링되었습니다 (HTML 태그가 표시됨) HTML로 렌더링 된 HTML 컨텐츠를 포함하는 noname.html이라는 추가 첨부 파일이 있습니다. – mike

+0

전체 기능을 포함 해 주셔서 감사합니다. 그러나 Amit Agarwal의 [https://ctrlq.org/code/20133-create-gmail-drafts-attachments] 페이지에서 대부분 복사 한 것처럼 보입니다.이 페이지는 원래 어디에서 왔습니까? 내 코드를 대부분 가져 왔으므로 정확히 일치합니다. – mike

+0

바로 그 사이트에서 복사했습니다. 하지만 필자가 필요로하는 부분에 맞게 작동하도록 약간의 변경을 가했다. –