Email to Discord Webhook with Amazon SES and AWS Lambda
tl;dr
- create discord webhook
- save incoming e-mails to amazon S3 using SES
- trigger lambda function for each new e-mail, send it to discord
This is a poor man’s solution for an email supported feedback system feeding into a discord channel using Amazon SES and AWS Lambda to handle e-mails and move them around.
If you want something quick and easy, there are ways to connect e-mail to discord with automation tools like Zapier, IFTTT, Integromat and so on. But hey, they cost money and we’re cheap. Right? At some point I need a cup of coffee and can’t spend it on another service.
Let’s get to it. This is the flow:
Incoming email -> S3 -> lambda -> discord
- save incoming e-mail in S3
- for each new object (e-mail) in S3, a lambda function will be triggered
- the lambda function does some stuff and sends the e-mail to discord
- lifecycle rule to clean up old e-mails
E-Mails usually end up in my IMAP inbox and not in S3, to keep things this way I’ve set up a forwarding rule on my e-mail server/provider to forward all incoming e-mails to a secondary e-mail address I set up for AWS SES.
Prepare AWS stuff
This is no step-by-step guide but rather a guide on what you need to do on the AWS side, I’ll try to link all important sections. AWS changes the interface every so often, you’d need to go through the docs anyway. But they’re pretty good with keeping links online.
- create an S3 bucket
- validate / verify a domain and/or e-mail address for aws ses
- allow SES to store e-mails in S3, follow this if you know AWS or this for a more step-by-step solution
- create a new lambda function
Give the lambda function a name, such as email to discord and use python 3.8 (at the time of this writing) as the runtime. Newer versions of python will probably work as well since there’s really nothing special to it.
Create Discord Webhook
- create a new webhook for your channel
- copy the url
Configure Lambda function
Now go back into your lambda function and copy the code below, just paste the whole thing in there. If, for some reason you don’t have/see a file name, use lambda_function.py
.Let’s configure the hook url and s3 bucket to fetch data from. Depending on when you’re reading this, the whole interface changed probably, so have a look here how to get to the lambda environment variable section and create three environment variables.
HOOK_DOMAIN
HOOK_PATH
BUCKET_NAME
- set
HOOK_DOMAIN
todiscord.com
- set
HOOK_PATH
to the path of your webhook (the stuff after discord.com), for example:/api/adfjas8dfjsnafu
- set
BUCKET_NAME
to the S3 bucket name you created earlier for incoming emails
Save it and send a test e-mail. If all went well, you should see the e-mail in your Discord channel. Otherwise, let me know and/or read up on lambda debugging.
get the code
Lifecycle rule to clean up old e-mails
The lambda function could take care of this directly by deleting the e-mail, but I like to keep it in there for a few of days since it’s just plain text usually and they don’t take up a lot of space, so costs can be ignored. I delete all e-mails after 7 days from the whole bucket.