Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting emails via LocalStack / AWS SES

I am trying to use LocalStack for my end to end tests but I cannot read the emails sent via LocalStack/SES. Is there a way to do that?

I want my Cypress e2e tests to invoke my backend services, the backend services compose an email containing a link and send the email via LocalStack/SES. I then want my e2e tests to wait for that email, read the link sent in it, and proceed.

I managed to invoke LocalStack's SES to send the email, and I am aware that the moto library backing LocalStack stores the sent messages in memory. Is there a way to read those messages?

like image 819
Zaphod Beeblebrox Avatar asked Jun 30 '26 07:06

Zaphod Beeblebrox


2 Answers

The sent messages can be retrieved via a service API endpoint (GET /_localstack/ses) or from the filesystem.

Messages are also saved to the data directory (DATA_DIR, see Configuration). If data directory is not available, the temporary directory (TMPDIR) is used. The files are saved as JSON in the ses/ subdirectory and organised by message ID.

Reference: https://docs.localstack.cloud/aws/ses/

like image 187
user1689987 Avatar answered Jul 02 '26 21:07

user1689987


Localstack uses Moto and Moto does expose the ability to check the sent emails. It is discussed here

https://github.com/spulec/moto/issues/1392

Taking a look at the code for localstack doesn’t look like they expose a function to access this information.

https://github.com/localstack/localstack/blob/master/localstack/services/ses/ses_starter.py

You will need to make a pull request to Localstack and add a function which exposes the ses_backend or specifically the sent_messages array.

from moto.ses import ses_backend ses_backend.sent_messages

I am not using localstack for SES I am running the E2E tests against our real Quality Assurance Testing environment (real SES). In that case you can use one of the following.

What you need is a programmatic way of reading the inbox and checking the emails by title and maybe the body too.

  1. Mailosaur https://mailosaur.com/ The API is simple to use with Java which fit our use case. In addition to that the response from their sales /support. They also have SMS services too which we are not using.

  2. MailSlurp https://www.mailslurp.com/ This was our first choice just because it’s the first one we found and it looked pretty good. But we tried to contact them and never go a reply. They still haven’t replied and we sent half a dozen emails.

  3. MailTrap https://mailtrap.io/ There was a third service which is suitable if your working with JavaScript (please use Typescript) as it’s REST based. But for Java REST APIs end up being quite verbose with the code which I am not a fan of. But if you are in JavaScript this option is suitable.

The prices of each are all comparable MailTrap has a free option too.

like image 40
Sigex Avatar answered Jul 02 '26 20:07

Sigex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!