It is pretty straight forward to do what you have outlined with some script. There are other ways to do the e-mailing part certainly and even other ways to do the selecting and the scripting, but I think the below will work well for your needs.
Instructions
Put the script below into a script step in an action once you are happy with what it is doing and made the appropriate amendments. Iāve commented the code, but Iāll also explain the process.
Settings
At the top of the script are three settings that you should change to meet your own needs.
-
EMAIL_TO
should be set to your e-mail address.
-
EMAIL_SUBJECT
should be set to what you want the e-mail subject line to read. Iāve set the default here to "From Drafts: " followed by the current timestamp.
-
EMAIL_VIA_DRAFTS is defaulted to
trueand will let Drafts e-mail in the background. Set this to
false` if you do not wish to use the Drafts e-mail service and want to view and confirm it before sending via your own e-mail client.
-
DRAFT_SEPARATOR
is whatever you would like between your draft content in the e-mail to separate them. If you donāt want anything, just delete the content between the double quotes to set a ānullā string.
Find the Drafts to Process
The next step gets an array (a list) of all of the drafts in your inbox. There are other options here about the ordering. If you want to change that, please refer to the query()
documentation.
Get the Draft Content to Send
Next the script gets the content of each draft and builds a new array. This time it is an array of the content rather than the draft objects.
Prepare and Send the E-mail (Trashing DraftsOnly if it Worked)
Next, the script builds an e-mail to send. This utilises the settings from the start.
The last block of script attempts to send the e-mail. If it succeeds, the script will then reprocess each draft and set the isTrashed
property effectively moving the draft to the trash.
Based on the outcome of sending the e-mail, a message will be displayed either telling you how many drafts were sent, or the e-mail failure status if the sending failed.
Add This to a Script Step in Your Action
//Settings
const EMAIL_TO = "your.address@domain.com";
const EMAIL_SUBJECT = "From Drafts: " + draft.processTemplate("[[time]]");
const EMAIL_VIA_DRAFTS = true;
const DRAFT_SEPARATOR = "\n\n%%% SEPARATOR %%%\n\n";
//Get the drafts to process in the order we want to proess them
let adraftToProcess = Draft.query("", "inbox", [], [], "modified", false, false);
//Build an array of draft content
let adraftContent = [];
adraftToProcess.map(draftCurrent => adraftContent.push(draftCurrent.content));
//Configure the e-mail
let email = Mail.create();
email.toRecipients = [EMAIL_TO];
email.subject = EMAIL_SUBJECT;
email.body = adraftContent.join(DRAFT_SEPARATOR);
email.sendInBackground = EMAIL_VIA_DRAFTS;
//Send the e-mail
if (email.send())
{
//Mail sent successfully, trash the drafts
adraftToProcess.map(draftCurrent =>
{
draftCurrent.isTrashed = true;
draftCurrent.update();
});
//Display a confirmation message
app.displayInfoMessage("Drafts sent and trashed: " + adraftToProcess.length);
}
else
{
//Failed to send, display and log the status
console.log(email.status);
app.displayErrorMessage(email.status);
}
Triggering the Action Outside of Drafts
Once you create your action, in terms of running it, the simplest option would probably be to use a Drafts widget. You can set a widget target area/button to execute a Drafts action. The alternative would be to use a Shortcut and save it to your home screen, or use a Shortcuts widget. The Run Action with Text would probably be the best option, and you can pass in no text at all as the action isnāt processing content for a single draft, but rather a collection of existing drafts.
Again, there are other trigger options, but I think that one of these should suffice for your use case.
Final Points
I havenāt posted an action because this is sending information automatically, and is trashing drafts. Two things you should be acutely aware of when reading the above.
I want you to be happy with it all before running it. At the end of the day I am not responsible for what happens to your drafts, you are, and so I want you to understand what is going on rather than taking it on blind faith for this particular one. Retrieving drafts from the trash is probably no big deal, but sending off content automatically by e-mail, I would rather you were fully aware of what is going where.
Hopefully, it is everything you were after, but please just post an update on if it is a match or if there is anything that it is not doing that you expected that it would do.