我正在嘗試使用Gmail API在CMS上創建郵件功能,但我在創建數組時遇到了問題。定義多維數組
$Message = {};
$Message['Date'] = getHeader(message.payload.headers, 'Date');
$Message['From'] = getHeader(message.payload.headers, 'From');
$Message['Subject'] = getHeader(message.payload.headers, 'Subject');
$Message['Reply-to'] = getHeader(message.payload.headers, 'Reply-to');
$Message['Body'] = getBody(message.payload);
$Message['ID'] = message.id;
$PartCount = 0;
for (var _ in message['payload']['parts']) $PartCount++;
if($PartCount > 2) {
$Message['hasAttachment'] = 'true';
} else {
$Message['hasAttachment'] = 'false';
}
if($PartCount > 2) {
$AttachmentCount = 1;
while($AttachmentCount <= $PartCount) {
$LocalAttachmentCount = ($AttachmentCount - 1);
if($Message.Subject == "AttachmentTest") {
console.log($Message);
console.log(message);
}
$Message['Attachment'] = {};
$Message['Attachment'][($LocalAttachmentCount)] = {};
$Message['Attachment'][($LocalAttachmentCount)]['FileName'] = message['payload']['parts'][$AttachmentCount]['filename'];
$AttachmentCount++;
}
}
我收到的錯誤是如下
getgmail:551 Uncaught TypeError: Cannot read property 'filename' of undefined
appendMessageRow
@ getgmail:551(anonymous function)
@ cb=gapi.loaded_0:628(anonymous function)
@ cb=gapi.loaded_0:601(anonymous function)
@ cb=gapi.loaded_0:167_.T.E.__cb
@ cb=gapi.loaded_0:99g
@ cb=gapi.loaded_0:93c
@ cb=gapi.loaded_0:84
,因爲我此刻轉圈圈任何建議,將不勝感激。
非常感謝,盧克。
編輯:我附上了我正在使用的數據的圖像。這是寫在jQuery的,但我的主要語言是PHP :)
原始JSON
{"id":"155e987feb707c32","threadId":"155e987feb707c32","labelIds":["SENT","INBOX","IMPORTANT"],"snippet":"Test for getting attachments in Expert.","historyId":"873165","internalDate":"1468501851000","payload":{"mimeType":"multipart/mixed","filename":"","headers":[{"name":"MIME-Version","value":"1.0"},{"name":"Received","value":"by 10.237.32.135 with HTTP; Thu, 14 Jul 2016 06:10:51 -0700 (PDT)"},{"name":"Date","value":"Thu, 14 Jul 2016 14:10:51 +0100"},{"name":"Delivered-To","value":"[email protected]"},{"name":"Message-ID","value":"<[email protected]om>"},{"name":"Subject","value":"AttachmentTest"},{"name":"From","value":"Sender <[email protected]>"},{"name":"To","value":"Receiver <[email protected]>"},{"name":"Content-Type","value":"multipart/mixed; boundary=001a113e81f03989b70537983b20"}],"body":{"size":0},"parts":[{"mimeType":"multipart/alternative","filename":"","headers":[{"name":"Content-Type","value":"multipart/alternative; boundary=001a113e81f03989aa0537983b1e"}],"body":{"size":0},"parts":[{"partId":"0.0","mimeType":"text/plain","filename":"","headers":[{"name":"Content-Type","value":"text/plain; charset=UTF-8"}],"body":{"size":41,"data":"VGVzdCBmb3IgZ2V0dGluZyBhdHRhY2htZW50cyBpbiBFeHBlcnQuDQo="}},{"partId":"0.1","mimeType":"text/html","filename":"","headers":[{"name":"Content-Type","value":"text/html; charset=UTF-8"}],"body":{"size":62,"data":"PGRpdiBkaXI9Imx0ciI-VGVzdCBmb3IgZ2V0dGluZyBhdHRhY2htZW50cyBpbiBFeHBlcnQuPC9kaXY-DQo="}}]},{"partId":"1","mimeType":"text/plain","filename":"text.txt","headers":[{"name":"Content-Type","value":"text/plain; charset=US-ASCII; name=\"text.txt\""},{"name":"Content-Disposition","value":"attachment; filename=\"text.txt\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc263q1"}],"body":{"attachmentId":"ANGjdJ-R1qc49sxpIHWRJpj6_zPF70sshHhNsKvCAqxHkYzltyxfwme_or6vK069GaKpDJzjHrtVsZqtQkHjTUSzSYg5yUOqM5y618mmOOIJUL_NnnOvrmTWVh0fKfMY1FAMGy1RJeUkb7vrbdv6lYXnAHYwojPMVx1aIG6DPWqqieQdBdE9p0704yxvbfoUu9XjeueV1FRs4b1B7x9mcS8Jj_7-uKo7a4l9nVr2HfrdnL4RS8D1JwHe1-DZiF4xiEX2j4Gre-36FooEcOnuZ_1RQryecIh5Fq_kobtFco-4_jd-w8gIMZYSHVDzUiw","size":12}},{"partId":"2","mimeType":"image/png","filename":"White.png","headers":[{"name":"Content-Type","value":"image/png; name=\"White.png\""},{"name":"Content-Disposition","value":"attachment; filename=\"White.png\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc1so00"}],"body":{"attachmentId":"ANGjdJ-6dRpb4OteV9kWWbffdzDBmaG-9uVd8HR3Ws7SmkrmxDN3x4kFxOzoZ7g_fHiEyTYxr4ntUDr9BkKd3ID4n4PXW9kCIdbJW_YxKPLF_9qo3Nkb9pphENvrk-sIt_uw7oB2GugjahjGclTdA4o4OmD99mWQ_A7a-b3ltDQc3qiek8KrhJ-lEi8nNo3PGu1PdRqLpNWqz3wgPE12NuhZRJngjNKOuymBw_DzSieO9qBKTNYHgVHyI0n7CQCTbT5C89Brdg3zsY_mQcME5EzzXc2AKMRvqj--pX_FSrEcvWez3aKrm0HJxkgWciQ","size":238}},{"partId":"3","mimeType":"image/jpeg","filename":"Black.jpg","headers":[{"name":"Content-Type","value":"image/jpeg; name=\"Black.jpg\""},{"name":"Content-Disposition","value":"attachment; filename=\"Black.jpg\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc2wi72"}],"body":{"attachmentId":"ANGjdJ8vs2lX1yfy9JPaxb7Ugy1wROlbSMCwgJRsWrNeKlDDdVrIpePkfreijftYoi5uQGHAFYIyblwcDfFH4v9vuNXAbT-JpozA9RFHtpHdsIq-98wp7sW6hvqirNphvzzCD7TCg4fG-emzxsqPFvQF6Oc85EEZ0in7EMagtunwkd-z9VAcqzQd-i3u8134B7y4YiY_lDI2gtnjv6ioVJONbDFzYiAVxELEVuvNFJwlb4ZrCdAVExA4OZO0Xqgjvm1RfsiPNiHu3PNbXhX4lrdJLKImmdDkIpQMU2CbOjmiSyPcxy47St4mC1USr68","size":74292}}]},"sizeEstimate":76042,"result":{"id":"155e987feb707c32","threadId":"155e987feb707c32","labelIds":["SENT","INBOX","IMPORTANT"],"snippet":"Test for getting attachments in Expert.","historyId":"873165","internalDate":"1468501851000","payload":{"mimeType":"multipart/mixed","filename":"","headers":[{"name":"MIME-Version","value":"1.0"},{"name":"Received","value":"by 10.237.32.135 with HTTP; Thu, 14 Jul 2016 06:10:51 -0700 (PDT)"},{"name":"Date","value":"Thu, 14 Jul 2016 14:10:51 +0100"},{"name":"Delivered-To","value":"[email protected]"},{"name":"Message-ID","value":"<[email protected]om>"},{"name":"Subject","value":"AttachmentTest"},{"name":"From","value":"Sender <[email protected]>"},{"name":"To","value":"Receiver <[email protected]>"},{"name":"Content-Type","value":"multipart/mixed; boundary=001a113e81f03989b70537983b20"}],"body":{"size":0},"parts":[{"mimeType":"multipart/alternative","filename":"","headers":[{"name":"Content-Type","value":"multipart/alternative; boundary=001a113e81f03989aa0537983b1e"}],"body":{"size":0},"parts":[{"partId":"0.0","mimeType":"text/plain","filename":"","headers":[{"name":"Content-Type","value":"text/plain; charset=UTF-8"}],"body":{"size":41,"data":"VGVzdCBmb3IgZ2V0dGluZyBhdHRhY2htZW50cyBpbiBFeHBlcnQuDQo="}},{"partId":"0.1","mimeType":"text/html","filename":"","headers":[{"name":"Content-Type","value":"text/html; charset=UTF-8"}],"body":{"size":62,"data":"PGRpdiBkaXI9Imx0ciI-VGVzdCBmb3IgZ2V0dGluZyBhdHRhY2htZW50cyBpbiBFeHBlcnQuPC9kaXY-DQo="}}]},{"partId":"1","mimeType":"text/plain","filename":"text.txt","headers":[{"name":"Content-Type","value":"text/plain; charset=US-ASCII; name=\"text.txt\""},{"name":"Content-Disposition","value":"attachment; filename=\"text.txt\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc263q1"}],"body":{"attachmentId":"ANGjdJ-R1qc49sxpIHWRJpj6_zPF70sshHhNsKvCAqxHkYzltyxfwme_or6vK069GaKpDJzjHrtVsZqtQkHjTUSzSYg5yUOqM5y618mmOOIJUL_NnnOvrmTWVh0fKfMY1FAMGy1RJeUkb7vrbdv6lYXnAHYwojPMVx1aIG6DPWqqieQdBdE9p0704yxvbfoUu9XjeueV1FRs4b1B7x9mcS8Jj_7-uKo7a4l9nVr2HfrdnL4RS8D1JwHe1-DZiF4xiEX2j4Gre-36FooEcOnuZ_1RQryecIh5Fq_kobtFco-4_jd-w8gIMZYSHVDzUiw","size":12}},{"partId":"2","mimeType":"image/png","filename":"White.png","headers":[{"name":"Content-Type","value":"image/png; name=\"White.png\""},{"name":"Content-Disposition","value":"attachment; filename=\"White.png\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc1so00"}],"body":{"attachmentId":"ANGjdJ-6dRpb4OteV9kWWbffdzDBmaG-9uVd8HR3Ws7SmkrmxDN3x4kFxOzoZ7g_fHiEyTYxr4ntUDr9BkKd3ID4n4PXW9kCIdbJW_YxKPLF_9qo3Nkb9pphENvrk-sIt_uw7oB2GugjahjGclTdA4o4OmD99mWQ_A7a-b3ltDQc3qiek8KrhJ-lEi8nNo3PGu1PdRqLpNWqz3wgPE12NuhZRJngjNKOuymBw_DzSieO9qBKTNYHgVHyI0n7CQCTbT5C89Brdg3zsY_mQcME5EzzXc2AKMRvqj--pX_FSrEcvWez3aKrm0HJxkgWciQ","size":238}},{"partId":"3","mimeType":"image/jpeg","filename":"Black.jpg","headers":[{"name":"Content-Type","value":"image/jpeg; name=\"Black.jpg\""},{"name":"Content-Disposition","value":"attachment; filename=\"Black.jpg\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc2wi72"}],"body":{"attachmentId":"ANGjdJ8vs2lX1yfy9JPaxb7Ugy1wROlbSMCwgJRsWrNeKlDDdVrIpePkfreijftYoi5uQGHAFYIyblwcDfFH4v9vuNXAbT-JpozA9RFHtpHdsIq-98wp7sW6hvqirNphvzzCD7TCg4fG-emzxsqPFvQF6Oc85EEZ0in7EMagtunwkd-z9VAcqzQd-i3u8134B7y4YiY_lDI2gtnjv6ioVJONbDFzYiAVxELEVuvNFJwlb4ZrCdAVExA4OZO0Xqgjvm1RfsiPNiHu3PNbXhX4lrdJLKImmdDkIpQMU2CbOjmiSyPcxy47St4mC1USr68","size":74292}}]},"sizeEstimate":76042}}
我的JSON
{"Date":"Thu, 14 Jul 2016 14:10:51 +0100","From":"Sender <[email protected]>","Subject":"AttachmentTest","Reply-to":"","Body":"<div dir=\"ltr\">Test for getting attachments in Expert.</div>\r\n","ID":"155e987feb707c32","hasAttachment":"true"}
EDIT2:
這是代碼已經爲我工作,attachment1用於調試目的
$Message = {};
$Message['Date'] = getHeader(message.payload.headers, 'Date');
$Message['From'] = getHeader(message.payload.headers, 'From');
$Message['Subject'] = getHeader(message.payload.headers, 'Subject');
$Message['Reply-to'] = getHeader(message.payload.headers, 'Reply-to');
$Message['Body'] = getBody(message.payload);
$Message['ID'] = message.id;
$Message['Attachment1'] = {};
$Message['Attachment'] = {};
for (let key in message['payload']['parts']) {
if(message['payload']['parts'][key]['filename'] != "") {
$Message['Attachment1'][key] = message['payload']['parts'][key];
console.log(message['payload']['parts'][key]['filename']);
$Message['Attachment'][key] = {};
$Message['Attachment'][key]['FileName'] = message['payload']['parts'][key]['filename'];
}
}
那是PHP .. –
@IsmailRBOUH - 我不這麼認爲,但OP呢?似乎喜歡用美元符號加前綴變量名稱。也許這是一個jQuery的東西。 – evolutionxbox
提供重現問題的數據示例。 – charlietfl