最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to retrieve the ID of the sent e-mail via GmailApp? - Stack Overflow

programmeradmin0浏览0评论

Here is a sample code

GmailApp.sendEmail("[email protected]", "mail subject", "mail body");

after the call I can see a sent message in my gmail account and that has a ID like this 147f7e77a4efed11 I want to retrieve this ID for the sent mail via the above code

sendEmail method returns an instance of GmailApp which is useful for chaining actions but no way to get the ID of the sent email.

what I am doing now is to perform a search and take the first message. But I am sure that is not a reliable way.

example

var message = GmailApp.search("to:[email protected]", 0, 1)[0].getMessages()[0];

now I can get the ID of the message and perform the desired actions.

Is it possible to retrieve the message or the ID of the message without an unreliable search?

Here is a sample code

GmailApp.sendEmail("[email protected]", "mail subject", "mail body");

after the call I can see a sent message in my gmail account and that has a ID like this 147f7e77a4efed11 I want to retrieve this ID for the sent mail via the above code

sendEmail method returns an instance of GmailApp which is useful for chaining actions but no way to get the ID of the sent email.

what I am doing now is to perform a search and take the first message. But I am sure that is not a reliable way.

example

var message = GmailApp.search("to:[email protected]", 0, 1)[0].getMessages()[0];

now I can get the ID of the message and perform the desired actions.

Is it possible to retrieve the message or the ID of the message without an unreliable search?

Share Improve this question edited Aug 21, 2014 at 10:26 pushpraj asked Aug 21, 2014 at 9:54 pushprajpushpraj 13.7k3 gold badges36 silver badges52 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 3

After building your service, you can use the messages.list and then filter them like you would when searching in GMail, you can also use labels to get only sent mails etc.

messages = gmail_service.users().messages().list(userId='me', q="subject:mail subject deliveredto:[email protected]", maxResults=1).execute()
if messages['messages']:
    for message in messages['messages']:
        print 'message ID: %s' % (message['id'])

If you pass through the email you're searching for and other "unique" references as arguments in a Python function it makes it a lot more versatile too.

Edit: After talking with you I think that creating your own personal ID/reference for each email sent would be the most prudent method of retrieval. I remend using faker, they have a javascript version and I use faker for all of my data needs. There's plenty of documentation for it and then you can filter your list according to how you set your ID.

create a draft first, then send it

function sendEmail(opts) {
  let draft = GmailApp.createDraft(recipient=opts.recipient, subject=opts.title, {
      htmlBody: opts.msg,
  });
  draft.send()
  let ret = {
    id: draft.getId(),
    messageId: draft.getMessageId(),
  }
  console.log('sendEmail results: ', ret )
  return ret
}

you can use the new gmail api directly. you will need a manual oauth flow to get the token, then use urlFetch to make the call.
https://developers.google./gmail/api/v1/reference/users/messages/send
it returns a message resource with its id. might also be possible to do this with advanced services but i havent tried. i have done it with urlfetch and worked ok.

Can you set the Message-Id header on the email? (Not sure if the apps scripts allows that or overwrites it or not.) If so, I'd generate a unique Id that way and you can look it up using a search "rfc822msgid:".

2022 Update

GmailDraft.send() returns a GmailMessage you can use that to label the sent message.

Example:

const draft = GmailApp.createDraft('Foo <[email protected]>','Lorem Ipsum','Dolor Sit Amet');
const label = GmailApp.getUserLabelByName('Test');
const msg = draft.send();
const thrd = msg.getThread();
label.addToThread(thrd);

google-apps-script solution working for sent messages with methods: GmailApp, MailApp

  1. enable Gmail service
  2. use the code:

  var messages = Gmail.Users.Messages.list(
    "me", {q: "subject:Test Me", maxResults: 1}
  ).messages;

  console.log(messages[0].id)
发布评论

评论列表(0)

  1. 暂无评论