I am working with Google apps script and would like to create a script which picks up mail from the drafts and sends them if they have label "send-tomorrow". Finding drafts with a certain label is pretty simple:
var threads = GmailApp.search('in:draft label:send-tomorrow');
However I don't see an API to send the message! The only option I see is to: - open the message - extract body/attachments/title/from/to/cc/bcc - send a new message with the above params - destroy the previous draft
which seems pretty annoying and I'm not sure would work well with embedded images, multiple attachments etc...
any hint?
I did it using the GmailMessage.forward method.
It works with upload images and attachments, but I had to set the subject to avoid the prefix "Fwd:", and the user name because it only displayed the user email to the recipients.
I didn't find a way to dispose the draft, so I just remove the label to prevent sending it again.
Script:
This is the exact topic of this blog by Amit Agarawal. His script does just what you describe, but doesn't handle inline images. For those, you can adapt the code from this article.
But you're right - what's the point of even having a draft message if you can't just send the stupid thing?!
We can use the GMail API Users.drafts: send from Google Apps Script to send a draft. The following stand-alone script does that, and handles the necessary authorization.
Script
The full script is available in this gist.
Authorization
To use Google's APIs, we need to have an OAuth2 token for the current user - just as we do for Advanced Services. This is done using
ScriptApp.getOAuthToken()
.After copying the code to your own script, open Resources -> Advanced Google Services, open the link for the Google Developers Console, and enable the Gmail API for your project.
As long as the script contains at least one GMailApp method that requires user authority, the authentication scope will be set properly for the OAuthToken. In this example, that's taken care of by
GmailApp.search()
insendDayOldDrafts()
; but for insurance you could include a non-reachable function call directly in the functions using the API.You can search through all drafts and then send that specific draft no problem.
I'm new around here and don't have enough "reputation" to comment, so couldn't comment on Mogsdad's original answer so I'm having to create a new answer:
I've adapted Mogsdad's solution to also support replying/forwarding existing threads, not just brand new messages.
To use it on existing threads, you should first create the reply/forward, and only then label the thread. My code also supports several labels and setting up those labels.
I created a new gist for it, forking Mogsdad's, here: https://gist.github.com/hadasfester/81bfc5668cb7b666b4fd6eeb6db804c3
I still need to add some screenshot links in the doc but otherwise this is ready for use, and I've been using it myself. Hope you find it useful.
Also inlining it here:
A simpler alternative is to use the gmail api instead of gmailApp:
above function example is used within a gs script at https://script.google.com. It needs the draftId (not the message Id) and the draft will be sent. Images and attachments are all OK! Info:https://developers.google.com/gmail/api/v1/reference/users/drafts/send
First,
GmailDraft
now has asend()
function you can call directly. See: https://developers.google.com/apps-script/reference/gmail/gmail-draft#send()Their code sample:
Second, may not even need it now that google has released scheduled sending.
Send