Чтение сообщений из Gmail с вложением - Node.js - PullRequest
0 голосов
/ 13 июля 2020

Мне было трудно читать сообщения из Gmail API.

Я могу получить тело каждого письма, используя mail.Payload.Parts [0] .Body.Data, который содержит тело сообщения в base64 .

Дело в том, что когда получаемое мной письмо имеет вложение, parts [0] MimeType является "multipart / alternate", а его тело содержит только пустые поля.

Как можно Я получаю тело письма, если в нем есть вложения? Код velow хорошо работает для писем без вложений, но ничего не получает для писем с вложениями

Пожалуйста, посоветуйте. Спасибо!

function getRecentEmail(auth) {
  const gmail = google.gmail({version: 'v1', auth});
  // Only get the recent email - 'maxResults' parameter
  gmail.users.messages.list({auth: auth, userId: 'me', maxResults: 13 ,}, function(err, response) {
      if (err) {
          console.log('The API returned an error: ' + err);
          return;
      }

    // Get the message id which we will need to retreive tha actual message next.
    var message_id = response['data']['messages'][0]['id'];

    // Retreive the actual message using the message id
    gmail.users.messages.get({auth: auth, userId: 'me', 'id': message_id}, function(err, response) {
        if (err) {
            console.log('The API returned an error: ' + err);
            return;
        }
      // console.log(response['data']);
       console.log('xxxxxxxxxxxxxxxxxxxxxxxxx');
       console.log(data.payload.parts);
       console.log(data.payload);
       message_raw = response.data.payload.parts[0].body.data;
       data = message_raw;  
       buff = new Buffer.from(data, 'base64');  
       text = buff.toString();
       console.log(text);
    });
     });
}
/*Below is the console output.
The part [0].body returns null while part [1].body has the attachment. Cannot see the email message part anywhere. Below is the console output.

  {
    partId: '0',
    mimeType: 'multipart/alternative',
    filename: '',
    headers: [ [Object] ],
    body: { size: 0 },
    parts: [ [Object], [Object] ]
  },
  {
    partId: '1',
    mimeType: 'image/jpeg',
    filename: '3.JPG',
    headers: [ [Object], [Object], [Object], [Object], [Object] ],
    body: {
      attachmentId: 'ANGjdJ80EeMqROfRj4fMz751dKoqD4OW1rW6qu483Fo2vZw1FG2YT7AOJcV
UsnO5cbeHoSWiftO5Z_OAtPghlftkLdpMTUFzGSYgcJ3fLR2nnNoMbvJxl-BCsFCPndRlL2G5-OUKmO0
mhO8knKAYtDrdAcaMb_9lbQcG379jtXIYQEpnKADEbLFy1i_ZAmyAg3prC-P0QNBQE-FTi4x26qRUeTM
hv8quTt9LMOhwnA',
      size: 44199
    }
  }
*/
...