How to parse incoming or forwarded emails in JavaScript
Email parsing is when software extracts and works with email data to automate business procedures. The subject is best understood through an example:
Let's imagine a company has a contact form on its website that lets people send the following:
- A general inquiry
- Complaint
- Request for further information
On the server, a programmer could code this form to be sent to specific email addresses depending on the nature of the inquiry (e.g., complaints would be routed to the customer service department, requests for further info to the sales department, etc.).
By coding the email passing functions on the web server, the company is locked into using that code whenever they want to change the form or add a new type of request.
Option two would be to use an email filter in the email client. Unfortunately, simple email filters lack the additional programming power necessary to carry out more advanced functions. For example, a company might want to save all of its "Request for further information" emails in a leads database. This is impossible to do without programming.
Email parsing is the happy medium that combines programmatic power that enables you to do something with the email data with the normal email filter functions that let you route and categorize emails sensibly.
Unfortunately, too many email filter providers are written for non-coders, denying coders the ability to leverage the full power of a programming language to do what they desire with an email parsing tool.
Repeat.dev has solved that problem.
Introducing repeat.dev's JavaScript email parsing tool
Repeat.dev allows you to create powerful webhooks using JavaScript alone. You can create webhooks in minutes without worrying about the internal plumbing of logs, metrics, and other functions.
After creating this tool, we saw the potential for an email-based tool where users could use JavaScript code to forward emails, save them to storage, or send them to a different webhook for deeper processing.
Using repeat dev, creating an email parser is as easy as clicking the "New Repeat" button on the dashboard and selecting the template called "A simple email".
This then generates the basic wrapper code for parsing your email. Logging and metrics are built in; you can learn more about these in our webhook blog post.
From here, you can manipulate the email object any way you want using standard JavaScript, as well as through any package available from npm.
Currently the repeat.dev `email` object contains all the basic email properties such as `bodies` (text and HTML), `cc`, `bcc`, `to`, etc., with `attachments` and `headers` planned to be added very shortly.
The `env` object gives you programmatic access to storage, metrics, and even other webhooks that you can call programmatically.
Use case: Use email parsing when webhooks are not available
One use case of email parsing is when a company you are dealing with does not offer webhooks but instead sends an email every time some event occurs, such as when someone purchases an item on a webshop.
When you create the email parser as described above, repeat.dev automatically generates an email address for that parser:
All you would need to do is give this email address to the company that does not have webhooks. Then, when they send the email to this address, you can take several actions on that email such as:
- Extract relevant data from the email
- Forward the email to another webhook
- Forward the email to another email address
- Update your database with data from the email
This way, the email parser starts to operate like a webhook.
Forward emails to Discord using JavaScript
Discord is extremely useful when initially testing code because you can create webhooks in Discord that will take a payload and then post it as a message in a channel.
The Discord payload needs to be in a specific format. Fortunately, repeat.dev has an "Email to Discord" template that automatically configures the incoming email in the required format. All you have to do is click "New Repeat" and then select the "Email to Discord" template.
This automatically generates the necessary code to receive emails and send them to a Discord webhook.
Next, we need to generate a webhook URL in Discord to know where to send the formatted data. To do this, select the channel in your Discord server where you want the messages to be posted. Click the gear icon for the channel's settings. Then click "Integrations".
After that, click "Webhooks" on the right.
Click "New Webhook" and then give your new webhook a name.
Then click "Copy Webhook URL".
Back in repeat.dev, select "Variables" on the right, then click the pencil icon to edit the value. Paste your Discord webhook URL into this field.
You can change the name of this variable to something else more meaningful such as DISCORD_URL. You also need to change where it's referenced in the code:
Every time you send an email to the email address for this Repeat, the message will be posted to that Discord channel:
Forward emails to Slack
Similarly to the way emails are forwarded through repeat.dev to Discord, you can also call a built-in function to forward messages to a Slack webhook. This is done using the Slack webhooks function instead of the Discord one and providing the Slack payload object as the second parameter.
The Slack payload also follows a specific format.
Leverage repeat.dev to easily create email parsers
Repeat.dev makes it simple to create webhooks, cron jobs, and email parsers that leverage the full power of JavaScript. The system offers the perfect blend of user-friendliness while retaining all programming capabilities.