An AWS Lambda function to send emails.
The primary purpose of this function is to provide a server-side back-end for sending emails.
The back-end support 2 email providers:
The back-end supports the failover mechanism between email providers.
The back-end supports following features
- multiple receipts, CCs, and BCCs, though these fields accept raw email address only, i.e. [email protected]. The named email addrees, e.g. "My name <name.email.com>", is not supporting now.
- support plain text email body only
- Doesn't allow empty or invalid from field. From field accepts the raw email address only.
- Subject, and email body cannot be empty
Instead of setting up an restapi back end server on an EC2 (or other cloud platform) instance to handle email service, ueses the AWS Lambda function and API Gateway to support it.
The serveless Lambda architecture provides the easy deployment, lower cost, and auto scalable solution for the simple email service.
The limitation is that API Gateway has the maximax 30 seconds timeout limition, which means the fronend may not be able to receive the response if the email sending process is over 30 seconds.
However for a plain text email, the timeout sounds good enough. The potential issue is that if the email service will have to support huge attachment, then the timeout is an issue.
For avoiding the unresponsible http request, which can hang the whole process, the http request is monitored by a local timer with default 10s timeout.
For simplicity the current failover mechanism is looping over the 2 services, and pick the first working one. With the request timeout monitor as described above, the loop won't be hanged too long even any of the services are slow of response.
For a really system, at least following factors should be considered
- Check the status of the services at backend
- Pick the fastest one
- Balance the loadding
- Retry if both services are down
- Fork/Clone
- Install dependencies -
npm install
- Test -
npm run test
- Build -
npm run build
- Lint -
npm run lint
Note: some of the tests are marked as xit, as these tests are accessing the backend which should not be run as part of the standard alone unit tests.
Note: use node8.10 as this is the version lambda supports.
Make sure you have aws cli installed, and configured correctly.
-
Create a lambda function manully via AWS protal, and specify the lambda source is from a S3 bucket, e.g. s3://${bucketname}/emailservice.zip
-
export 2 enviroment
export EMAIL_LAMBDA_NAME=${the_name_of_lambda} export EMAIL_S3=${the_name_of_s3_bucket}
-
run
npm run deploy
-
The following env variable need to be set on lambda function MAIL_GUN_USER= MAIL_GUN_PASS= SEND_GRID_TOKEN=
Please modify the email.json file, and change the address to the right emaiil address. So far seems only gmail address are working all rightl
cd tests
chmod +x ./send.sh
./send.sh email.json
cd tests
chmod +x ./status.sh
./status.sh
- The mail gun sand box's receipts invitation email is broken. Click agree show a 404 not found page. No way to test multiple receipts, CCs, BCCs
- Seems only gmail address can receive the message. Haven't got enough time to figure out the reason. Guess that some of the free email accounts are filtering the emails form these api?
- Support named email address
- The email providers have different rules of validation the email, need find the common set which become the validation rules of the service.
- Use a validator module for input validation
- Use AWS system store to replace the env variables
- Allow the user custom the request timeout value
- Retry mechanism for the case if both of the providers are
- Specify the primary provider
- Performance test