# Handling email confirmation

**URL:** <https://community.postman.com/t/handling-email-confirmation/1328>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [March 29, 2018, 8:58am UTC](https://community.postman.com/t/handling-email-confirmation/1328 "2018-03-29T08:58:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nuwazz](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@Nuwazz](https://community.postman.com/u/Nuwazz)\
**Post date:** [March 29, 2018, 8:58am UTC](https://community.postman.com/t/handling-email-confirmation/1328/1 "2018-03-29T08:58:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![parker937](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@parker937](https://community.postman.com/u/parker937)\
**Post date:** [April 17, 2018, 8:02pm UTC](https://community.postman.com/t/handling-email-confirmation/1328/2 "2018-04-17T20:02:08Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![hliulka15](https://avatars.discourse-cdn.com/v4/letter/h/fbc32d/32.png) [@hliulka15](https://community.postman.com/u/hliulka15)\
**Post date:** [October 24, 2018, 2:52pm UTC](https://community.postman.com/t/handling-email-confirmation/1328/3 "2018-10-24T14:52:55Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![parker937](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@parker937](https://community.postman.com/u/parker937)\
**Post date:** [October 24, 2018, 7:41pm UTC](https://community.postman.com/t/handling-email-confirmation/1328/4 "2018-10-24T19:41:27Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![hliulka15](https://avatars.discourse-cdn.com/v4/letter/h/fbc32d/32.png) [@hliulka15](https://community.postman.com/u/hliulka15)\
**Post date:** [October 25, 2018, 3:54pm UTC](https://community.postman.com/t/handling-email-confirmation/1328/5 "2018-10-25T15:54:16Z")

</div>

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

```auto
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 😃

---

<div class="post-metadata">

**Author:** ![Calibraticboy](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@Calibraticboy](https://community.postman.com/u/Calibraticboy)\
**Post date:** [May 14, 2019, 6:53am UTC](https://community.postman.com/t/handling-email-confirmation/1328/6 "2019-05-14T06:53:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [May 14, 2019, 1:37pm UTC](https://community.postman.com/t/handling-email-confirmation/1328/7 "2019-05-14T13:37:52Z")

</div>

Hey @Calibraticboy

You could use this for Mailgun.

[https://www.mailgun.com/blog/together-at-last-postman-meets-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.

 ![12](https://us1.discourse-cdn.com/getpostman/original/2X/f/f282cfc40ab110373bfff962c9c1cd6b41a23bc8.png)

---

<div class="post-metadata">

**Author:** ![Calibraticboy](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@Calibraticboy](https://community.postman.com/u/Calibraticboy)\
**Post date:** [May 15, 2019, 6:27am UTC](https://community.postman.com/t/handling-email-confirmation/1328/8 "2019-05-15T06:27:25Z")

</div>

Thanks for sharing. Will take a look at it.

---

<div class="post-metadata">

**Author:** ![mkane](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mkane/32/2762_2.png) [@mkane](https://community.postman.com/u/mkane)\
**Post date:** [September 10, 2019, 9:32pm UTC](https://community.postman.com/t/handling-email-confirmation/1328/9 "2019-09-10T21:32:18Z")

</div>

Hi there, I am actually the author of that blog post at Mailgun! Did you ever figure out how to get Mailgun’s APIs + Postman cooperating together?

---

<div class="post-metadata">

**Author:** ![Calibraticboy](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@Calibraticboy](https://community.postman.com/u/Calibraticboy)\
**Post date:** [September 12, 2019, 6:59am UTC](https://community.postman.com/t/handling-email-confirmation/1328/10 "2019-09-12T06:59:39Z")

</div>

Hi @mkane

No, i did not figure it out how Mailgun and Postman work togheter. The issue was on our side because none of us (developers and testers) knew how this work.  
A better explanation is needed, Maybe a video tutorial.

Best regard  
CR

---

<div class="post-metadata">

**Author:** ![leticat91](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@leticat91](https://community.postman.com/u/leticat91)\
**Post date:** [September 18, 2019, 3:38pm UTC](https://community.postman.com/t/handling-email-confirmation/1328/11 "2019-09-18T15:38:04Z")

</div>

Hi All,

I can recommend MailSlurp for that use-case, see [https://app.mailslurp.com](https://app.mailslurp.com). I will quickly explain how I use the MailSlurp API ([https://api.mailslurp.com/swagger-ui.html#](https://api.mailslurp.com/swagger-ui.html#)). I will register an email provided by MailSlurp in a random web portal. Every email has an inbox connected to it. After that I will call the /waitForLatestEmail endpoint from the MailSlurp API. The response is in this case the registration mail in JSON format. You can then e.g. extract the “verification token” from the response or check the email-sender, etc…  
I think it’s a great service to test mail delivery.

---

<div class="post-metadata">

**Author:** ![sentient](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sentient/32/9739_2.png) [@sentient](https://community.postman.com/u/sentient)\
**Post date:** [September 9, 2021, 5:28pm UTC](https://community.postman.com/t/handling-email-confirmation/1328/12 "2021-09-09T17:28:09Z")

</div>

Michael,

Do you have an updated collection for this? The collection on the shared link is not supported anymore on postman

Thanks

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 24, 2023, 1:48pm UTC](https://community.postman.com/t/handling-email-confirmation/1328/13 "2023-08-24T13:48:38Z")

</div>

Closed on August 24, 2023, 1:48pm UTC


