Splitting tasks and sending each line to an email address (Trello)

I’m trying to rapidly add multiple tasks to a Trello board via email. All tasks are going to the same board.

Where I’m stuck is that I’m trying to recreate the behavior of the built-in Drafts Mail action step, and have these each send in the background. What’s happening now is that Apple Mail is opening and coming to foreground, rather than sending in background (I tried the false param but didn’t work).

Here’s my script. Thoughts? What am I missing?

// Configuration 
const TRELLO_BOARD_EMAIL = "board_email_goes_here";

// Get the current draft's content
let content = draft.content;

// Split the content into lines
let lines = content.split("\n").filter(line => line.trim()); // Remove empty lines

// Process each line
lines.forEach(line => {
    // Initialize variables for subject and body
    let subject, body;
    
    // Check if line contains the separator
    if (line.includes("|")) {
        let parts = line.split("|");
        subject = parts[0].trim();
        body = parts[1].trim();
    } else {
        subject = line.trim();
        body = ""; // Empty body if no separator found
    }
    
    // Create and configure the mail action
    let mail = Mail.create();
    mail.toRecipients = [TRELLO_BOARD_EMAIL];
    mail.subject = subject;
    mail.body = body;
    
    // Send the email in the background
    mail.send(false); // false parameter means send in background
});

// Provide feedback
if (lines.length > 0) {
    app.displayInfoMessage(`Sent ${lines.length} items to Trello`);
} else {
    app.displayErrorMessage("No content found to send");
}

The Mail.send() function does not take a boolean. Not sure where you might have seen that example. The object has a sendInBackground property you need to set to true before calling send to get backgrounding. You should also check the return value of send to know if it sent successfully. Like:

mail.sendInBackground = true
if (mail.send()) {
    // mail handed off to server successfully
}
else {
   // error sending, maybe log or fail
}

More info in the Mail object scripting reference

1 Like

Ah, thank you! This worked to send in background. But for some reason though, it’s only sending the lines that have a description (i.e. it’s not sending Tasks 4 & 8 in example below).

Task 8
Task 7 | description
Task 4
Task 6 | description here

Weird, I figured out how to fix it, but don’t understand why.

The script line that was an issue when there was no description was this:
body = ""; // Empty body if no separator found

Having body as an empty string, it would not send (or at least wasn’t ingested by Trello). Setting body = " "; i.e. a space or any character, and it worked.

I’m not sure if this was an issue with Drafts (which showed no errors in log), Mail, or Trello. But at least it’s working now. Full working script below.

Added to action directory here.

// See online documentation for examples
// https://docs.getdrafts.com/docs/actions/scripting

// Configuration - Set your Trello board's email address here
const TRELLO_BOARD_EMAIL = "<board email here>";

// Get the current draft's content
let content = draft.content;

// Split the content into lines
let lines = content.split("\n").filter(line => line.trim()); // Remove empty lines

// Process each line
lines.forEach(line => {
    // Initialize variables for subject and body
    let subject, body;
    
    // Check if line contains the separator
    if (line.includes("//")) {
        let parts = line.split("//");
        subject = parts[0].trim();
        body = parts[1].trim();
    } else {
        subject = line.trim();
        body = " "; // Empty body (space) if no separator found, empty string fails for TBD reason
    }
    
    // Create and configure the mail action
    let mail = Mail.create();
    mail.toRecipients = [TRELLO_BOARD_EMAIL];
    mail.subject = subject;
    mail.body = body;
    mail.sendInBackground = true;
    mail.send();
});

// Provide feedback
if (lines.length > 0) {
    app.displayInfoMessage(`Sent ${lines.length} items to Trello`);
} else {
    app.displayErrorMessage("No content found to send");
}

Sending an empty message is an error. It’s up to you to check for success by checking the result the send function, and you can log the value of the status of the mail object after – something like:

if (mail.send()) {
   // we good, it send
}
else { // some failure occurred, handle that...
   console.log("Mail Status: " + mail.status)
}
1 Like