Action

Email - Personal

Posted by ghawk.dev, Last update 1 day ago

Create and send a personal email directly from a Draft using Markdown formatting.

Email Format

Begin your draft with the following fields, each on its own line:

To: recipient@email.com
CC: another@email.com
BCC: third@email.com
Subject: Your subject

To: and Subject: are required. CC: and BCC: are optional.

Multiple recipients can be included in any field by separating email addresses with commas.

After the email headers, write your message using Markdown. The action converts the Markdown to HTML and opens the email in your mail client for review and editing before sending.

Once the email is successfully sent, the original Draft will automatically be archived. If you cancel the email or it fails to send, the Draft will be kept.

Mac Users

If you’re using Drafts on Mac, this action requires Mail Assistant, a utility provided by Drafts that allows Drafts to create emails in Apple Mail.

You can download and learn more about Mail Assistant from the Drafts website.

Steps

  • script

    // --------------------------------------------
    // Email — Personal
    //
    // Format:
    //
    // To: person@example.com
    // CC: another@example.com
    // BCC: third@example.com
    // Subject: My subject
    //
    // Email body begins after the headers.
    // Markdown is converted to HTML.
    // The Draft is archived ONLY after the
    // email is successfully sent.
    // --------------------------------------------
    
    // Get the current draft
    let content = draft.content;
    
    
    // --------------------------------------------
    // Helper function to extract a header
    // --------------------------------------------
    
    function getHeader(name) {
        let regex = new RegExp("^" + name + ":\\s*(.+)$", "mi");
        let match = content.match(regex);
        return match ? match[1].trim() : "";
    }
    
    
    // --------------------------------------------
    // Extract email fields
    // --------------------------------------------
    
    let to = getHeader("To");
    let cc = getHeader("CC");
    let bcc = getHeader("BCC");
    let subject = getHeader("Subject");
    
    
    // --------------------------------------------
    // Validate required fields
    // --------------------------------------------
    
    if (!to) {
        app.displayErrorMessage(
            "No recipient found.\n\nAdd a line like:\nTo: person@example.com"
        );
        context.fail();
    }
    
    if (!subject) {
        app.displayErrorMessage(
            "No subject found.\n\nAdd a line like:\nSubject: My subject"
        );
        context.fail();
    }
    
    
    // --------------------------------------------
    // Remove email headers from the body
    // --------------------------------------------
    
    let body = content
        .replace(/^To:\s*.+\r?\n/mi, "")
        .replace(/^CC:\s*.+\r?\n/mi, "")
        .replace(/^BCC:\s*.+\r?\n/mi, "")
        .replace(/^Subject:\s*.+\r?\n/mi, "")
        .replace(/^\s*\r?\n/, "")
        .trim();
    
    
    // --------------------------------------------
    // Convert Markdown → HTML
    // --------------------------------------------
    
    let markdown = MultiMarkdown.create();
    
    markdown.format = "html";
    markdown.completeDocument = false;
    
    let htmlBody = markdown.render(body);
    
    
    // --------------------------------------------
    // Create Mail Assistant request
    // --------------------------------------------
    
    let mail = CallbackURL.create();
    
    mail.baseURL = "mail-assistant://sendMail";
    
    mail.addParameter("to", to);
    
    if (cc) {
        mail.addParameter("cc", cc);
    }
    
    if (bcc) {
        mail.addParameter("bcc", bcc);
    }
    
    mail.addParameter("subject", subject);
    mail.addParameter("body", htmlBody);
    mail.addParameter("html", "true");
    
    
    // --------------------------------------------
    // WAIT for Mail Assistant to report back.
    //
    // This is important!
    //
    // When you click Send in Mail Assistant,
    // Drafts receives a success callback.
    //
    // If you cancel the email, it receives a
    // cancellation instead.
    // --------------------------------------------
    
    mail.waitForResponse = true;
    
    let success = mail.open();
    
    
    // --------------------------------------------
    // Archive ONLY if the email was successfully
    // sent.
    // --------------------------------------------
    
    if (success) {
    
        draft.isArchived = true;
        draft.update();
    
        app.displaySuccessMessage(
            "Email sent and draft archived."
        );
    
    } else if (mail.status == "cancelled") {
    
        app.displayInfoMessage(
            "Email cancelled. Draft was kept."
        );
    
    } else {
    
        app.displayErrorMessage(
            "The email was not sent. Draft was kept."
        );
    
    }

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.