Automate obtaining email verification code

Hi guys,

Here’s my issue I would like to solve: in order to sign into the online portal used for work, we have to enter a verification code emailed to us.

So, the process is currently this:

  1. I go to the URL of the online portal, and enter my username and password, manged by 1Password. Nice and easy.
  2. An email is then sent to my work email address containing an 8 digit code.
  3. I manually have to go to my mail client (I use Mac Mail) and copy the code.
  4. Back to my browser and paste.
  5. And I’m in.

I want to automate stages 2, 3 and 4.

I would like the automation to trigger whenever the verification email is received and automatically copy the code to my clipboard and (ideally) delete the email, all without me leaving the browser. I would then receive a notification saying the automation has completed and I could paste into the relevant box.

The good news is that the emails always come from the same email address and always follow the same format, but I’m struggling to think how to actually achieve it (if, indeed, it is possible).

It only takes me 15 seconds to do this manually, but as a true fan of automation, I am quite prepared to spend hours setting it up. It is so annoying to me and I have to do it 4-5 times a day.

I have KBM, BTT and Shortcuts at my disposal.

Any help or guidance is gratefully received!

Thanks for reading.

Presuming you could get the contents of the email (eg copy it to the clipboard), then the rest is relatively easy.

The hard part is getting the contents of the email, which will depend a lot on how your email is set up (both on the server and what client you are using).

If you have control of the server, then you could potentially redirect such email to a forward script which could parse the email for the code, then trigger a macro containing the code using a Keyboard Maestro Remote trigger.

If the email account has POP access, then you could potentially run a script that uses the POP access to get the last email and parse it for the code. Probably also possible with IMAP access, but I don’t really know much about IMAP.

Again if you have access to the server and server filtering, you could potentially forward the email from that address to a special account and then you could use any email client to read the email and then find a way to extract it and process it.

Otherwise (or as well) you’ll have to control your local email client to download the email, and then find a way to extract the email message. Mail is fairly scriptable, but I don’t know whether it can do this or not (asking on the Keyboard Maestro forum would probably get you an answer for that).

Those would be my starting points.

I have a similar issue and need to start pondering solutions for as several services I use are moving to “passwordless login” where them email you a link or code or whatever to login, but I don’t have my primary email on my phone so that means I can’t login when I’m away from my desk which is a pain. So I’m wondering about some sort of method of extracting such emails and forwarding them to my phone.

I thought this was a built-in feature to fill one-time codes from Messages and Mail? (System Settings > General > Autofill & Passwords) Otherwise you could set up a Mail Rule to run an AppleScript that extracts the code with a regex copies to clipboard?