Pull the verification code out of a test email
When your test needs the verification code from an email, call GET /api/smtp/message/{uuid}/codes on Buggregator. It finds numbers of 4 to 8 digits in the text and HTML parts, or any pattern you pass, and your test types the code in.
Steps
1. Take a cursor before the action
Your app must send mail to Buggregator on 127.0.0.1:1025. Remember the server time before the test triggers the email, so an old email cannot match.
CURSOR=$(curl -s http://127.0.0.1:8000/api/smtp/cursor | jq -r .cursor)2. Wait for the email and keep its uuid
Let the test ask for the code, for example by clicking "Forgot password". Then wait for the email. The call returns as soon as a matching message arrives, and 408 on timeout.
UUID=$(curl -s -G http://127.0.0.1:8000/api/smtp/messages/wait \
--data-urlencode "since=$CURSOR" \
--data-urlencode "to=alice@example.com" \
--data-urlencode "subject_contains=Reset" \
--data-urlencode "timeout=20s" | jq -r .uuid)3. Read the code
Ask for the codes in that message. Each code is listed once.
curl -s http://127.0.0.1:8000/api/smtp/message/$UUID/codes
# {"data":["482913"],"pattern":"\\b\\d{4,8}\\b"}4. Pass your own pattern for other codes
The default pattern finds numbers of 4 to 8 digits. For letters and digits, add a pattern query parameter, for example pattern=[A-Z0-9]{6}. An invalid pattern gets 400.
5. Use the link instead, if the email has one
For a confirmation link rather than a code, call GET /api/smtp/message/{uuid}/links. It lists the links from the HTML and text parts with their url, text and source.
Why it works
Buggregator stores every email your app sends, so the API can read the text part and the HTML part with the tags removed. The codes route runs a regular expression over both and returns each match. With SSO on, send the X-Auth-Token header, as for the rest of /api.
Related tips
- Mail
Wait for the confirmation email in an E2E test
Call /api/smtp/messages/wait from your E2E test. It returns the matching email as soon as it arrives, or 408 on timeout. No fixed sleeps.
- Mail
Laravel mail to a local inbox, three .env lines
Set MAIL_MAILER, MAIL_HOST and MAIL_PORT in .env and every Laravel email lands in Buggregator with HTML preview and attachments. Nothing is delivered.
- Mail
Catch dev email in docker-compose with one service
Add Buggregator as a service in docker-compose.yml and set your app's mail host to its service name. Every email lands in the UI on port 8000.
Checked on with Buggregator v2.8.0