Handling email confirmation

Let say I have to test following scenario

  1. Register to the system
    User will receive register confirmation email

  2. User click on the register confirmation email

  3. Login to system from user name and password

In here I have 2 end points

POST Register and POST Login

but for the email confirmation part I do not have any end point

Since email confirmation is must to do before login I’m blocked in the middle’
how can we accommodate this scenario using postmen?

If you’re testing a non-production system, you can divert your email to a 3rd party service that has an API, and use a request to extract and simulate the “email confirmation” action.

There are several options, but I use a 3rd-party email service called MailTrap (https://mailtrap.io)

For example, this is the flow I use:

  • Register a new user POST /users with a known email
  • List emails via the MailTrap API
    • Use a Test Script to extract the link from the latest email and place it in an Environment Variable
  • Make a request to the extracted link in the Environment Variable
  • Login the user POST /auth/login

I made several assumptions about your use-case, but this is the most effective solution I’ve found thus far.
I’m also very curious to hear about any other solutions that people have come up with.

2 Likes

@parker937 I’m struggling to write the test script to extract the link from the latest email. Do you have an example of what you wrote? The url I’m trying to extract is something like https://company.com/confirm/:token thanks in advance for your help!

It’s been several months since I’ve used this script, but here’s what I had previously.

// Parse the response JSON
var response = JSON.parse(responseBody);
if (response[0] && response[0].html_body) {
    let matches = /\?confirm_token\=(\w+)/g.exec(response[0].html_body);
    postman.setEnvironmentVariable("confirm_token", matches[1]);
}

Here’s a possible modification to the regex for your example URL (this is untested):
let matches = /\/confirm\/(\w+)/g.exec(html_body);

Since posting, the MailTrap API has deprecated the html_body field and now passes a reference URL in the html_path attribute. This will require a second API call, but the same pattern as before can be used.

  • Make an API Call to MailTrap to list emails
    • Store html_path to an environment variable
  • Make an API call the the URL from the previously store environment variable
    • Use a regex to parse the token from the expected URL

Hope that helps!

thank you so much for your help! Modified it a bit to work with what I’ve got but, for anyone else who has this issue, this is what worked for me

var data = JSON.parse(responseBody);
var emailContent = JSON.stringify(data[pm.environment.get("email")].html);
var confirmationLink = /\/confirm\/(\w+)/g.exec(emailContent);
postman.setEnvironmentVariable("confirmToken", confirmationLink[0]);

probably a better way to do this but it works so I’m happy :smiley:

Does anyone knows if there is a tutorial or course how to set up postman with Mailtrap or Mailgun and how to use it in real time.

Hey @Calibraticboy

You could use this for Mailgun.

https://www.mailgun.com/blog/together-at-last-postman-meets-mailgun

Select the ‘Run In Postman’ button to import the collection locally.

The collection also contains the documentation needed for the different endpoints.

1 Like

Thanks for sharing. Will take a look at it.

1 Like