Mailisk Rust Client
Typed async Rust client for the Mailisk REST API.
Mailisk is an email, SMS, outbound email, and TOTP testing platform. This crate mirrors the Mailisk Node and Python clients: the same endpoints, the same search defaults, and request/response structs whose fields match the API's JSON fields.
Installation
cargo add mailisk
cargo add tokio --features macros,rt-multi-thread
The client is async and built on reqwest, so it needs a Tokio runtime.
TLS uses rustls by default. To use the platform TLS library instead:
= { = "0.1", = false, = ["native-tls"] }
The minimum supported Rust version is 1.88.
Create A Client
Get your API key from Mailisk and pass it directly when creating a client:
use MailiskClient;
let mailisk = new?;
The library does not read .env files or environment variables itself. Load
configuration however your application prefers, then pass it to the client.
Use the builder to target another API URL or to supply your own reqwest::Client:
let mailisk = builder
.base_url
.build?;
MailiskClient is cheap to clone; clones share one connection pool.
Quick Start
use ;
async
Request parameter structs implement Default, so you only set the fields you
need and finish with ..Default::default().
Defaults
search_inbox and search_sms_messages match the Node and Python client defaults:
- wait for at least one matching message unless
waitisSome(false) - ignore messages older than 15 minutes unless a lower bound is provided
- use a 5 minute timeout while waiting
- allow a high redirect count, because the API implements waiting by redirecting back to the same URL
Pass a different timeout with the _with_options variants:
use Duration;
use ;
mailisk
.search_inbox_with_options
.await?;
If you supply your own reqwest::Client, configure its redirect policy to
allow enough redirects to cover a full wait, for example
reqwest::redirect::Policy::limited(MailiskClient::DEFAULT_MAX_REDIRECTS).
Search the inbox:
let response = mailisk
.search_inbox
.await?;
let emails = response.data;
List namespaces:
let namespaces = mailisk.list_namespaces.await?;
for namespace in namespaces.data
Download an attachment:
let attachment_id = &emails.attachments.id;
let content: = mailisk.download_attachment.await?;
Outbound Email
Send an outbound email:
use ;
let email = mailisk
.send_email
.await?;
println!;
Send with an attachment. Attachment contents are base64 encoded, for example
with the base64 crate:
use Engine;
use OutboundEmailAttachment;
let content = read?;
mailisk
.send_email
.await?;
Fetch delivery details:
let detail = mailisk.get_outbound_email.await?;
println!;
Reply to an inbound email:
use ReplyToEmailParams;
let reply = mailisk
.reply_to_email
.await?;
Forward an inbound email:
use ForwardEmailParams;
let forwarded = mailisk
.forward_email
.await?;
SMS
Search SMS messages:
use SearchSmsMessagesParams;
let messages = mailisk
.search_sms_messages
.await?;
List SMS numbers:
let numbers = mailisk.list_sms_numbers.await?;
Send a virtual SMS:
use SendVirtualSmsParams;
mailisk
.send_virtual_sms
.await?;
TOTP
Create a saved TOTP device:
use CreateTotpDeviceParams;
let device = mailisk
.create_totp_device
.await?;
Generate a TOTP code from a shared secret:
use GetTotpOtpParams;
let otp = mailisk
.get_totp_otp_by_shared_secret
.await?;
println!;
Generate a TOTP code for a saved device:
let otp = mailisk
.get_totp_otp_by_device_id
.await?;
Delete a saved TOTP device:
mailisk.delete_totp_device.await?;
Errors
Every method returns mailisk::Result<T>. A non-success API response becomes
Error::Api with the status code and the raw response body:
match mailisk.list_namespaces.await
Types
Request and response types are exported from the crate root, for example
SendEmailParams, SearchInboxResponse, OutboundEmail,
OutboundEmailDetail, and TotpOtpResponse. JSON fields named type are
exposed as kind. Status enums include an Unknown variant so that values
added to the API later do not break deserialization.