Scheduled Emails

Send emails at any time in the future with RunAt.dev API


In the below example, we'll create a tool that allows users to schedule emails by integrating RunAt.dev with an email sending service.

  1. Signup for an account if you do not already have one.
  2. Using your new account, create an API key with Write permissions.
  3. For this example, we'll be using MailChannel's API. A normal request to send an email with MailChannels looks like:
 curl -X POST \
 -H "Accept: application/json" \
 -H "Content-Type: application/json" \
 "https://api.mailchannels.net/tx/v1/send" \
 -d '{
  "personalizations": [
    { "to": [{"email": "[email protected]", "name": "Test Recipient"}] }
  ],
  "from": {
      "email": "[email protected]",
      "name": "Test Sender",
  },
  "subject": "Test Subject",
  "content": [{
      "type": "text/plain",
      "value": "Test message content",
  }],
}'
  1. Instead of making this direct request to MailChannels, we'll instead make a very similar request to RunAt's schedule API:
 curl -X POST \
 -H "Accept: application/json" \
 -H "Content-Type: application/json" \
 -H "X-RunAt-API-Key: <API key here>"
 "https://api.runat.dev/v1/schedule?url=https%3A%2F%2Fapi.mailchannels.net%2Ftx%2Fv1%2Fsend&in_ms=69000" \
 -d '{
  "personalizations": [
    { "to": [{"email": "[email protected]", "name": "Test Recipient"}] }
  ],
  "from": {
      "email": "[email protected]",
      "name": "Test Sender",
  },
  "subject": "Test Subject",
  "content": [{
      "type": "text/plain",
      "value": "Test message content",
  }],
}'

Making a modified request like this is all you need to schedule tasks! Executing the above cURL command will schedule an email to be sent 6.9s in the future.

  1. Now, let's make this request user-facing by creating a web page to take user input, then send this time-delayed email:
<!DOCTYPE html>
<html>
  <head>
    <title>Send Email</title>
  </head>
  <body>
    <h1>Send Email</h1>
    <form id="emailForm">
      <label for="recipient">Recipient:</label>
      <input
        type="email"
        id="recipient"
        name="recipient"
        required
      /><br /><br />
 
      <label for="seconds">Seconds in the Future:</label>
      <input type="number" id="seconds" name="seconds" required /><br /><br />
 
      <label for="body">Email Body:</label>
      <textarea id="body" name="body" rows="5" required></textarea><br /><br />
 
      <button type="submit">Send Email</button>
    </form>
 
    <script>
      const form = document.getElementById("emailForm");
      form.addEventListener("submit", async (event) => {
        event.preventDefault();
 
        const recipient = form.elements.recipient.value;
        const seconds = form.elements.seconds.value;
        const body = form.elements.body.value;
 
        const apiUrl = `https://api.runat.dev/v1/schedule?url=https%3A%2F%2Fapi.mailchannels.net%2Ftx%2Fv1%2Fsend&in_ms=${
          seconds * 1000
        }`;
 
        const requestBody = {
          personalizations: [
            { to: [{ email: recipient, name: "Test Recipient" }] },
          ],
          from: {
            email: "[email protected]",
            name: "Test Sender",
          },
          subject: "Test Subject",
          content: [
            {
              type: "text/plain",
              value: body,
            },
          ],
        };
 
        try {
          const response = await fetch(apiUrl, {
            method: "POST",
            headers: {
              Accept: "application/json",
              "Content-Type": "application/json",
            },
            body: JSON.stringify(requestBody),
          });
 
          if (response.ok) {
            alert("Email scheduled successfully!");
            form.reset();
          } else {
            alert("An error occurred while scheduling the email.");
          }
        } catch (error) {
          alert("An error occurred while scheduling the email.");
          console.error("Error:", error);
        }
      });
    </script>
  </body>
</html>

The above HTML takes user input for recipient, amount of time to delay, and message contents. You can try this simple tool here.

You're now ready to schedule your APIs - congrats!