DEV Community

Cover image for A Mailing List for Laravel Rust, and All Five Packages on Laravel Rust 0.2
Vitaly Ax
Vitaly Ax

Posted on Edited on Originally published at axvi.dev

A Mailing List for Laravel Rust, and All Five Packages on Laravel Rust 0.2

Laravel Rust 0.2 came out today, and all five of my packages for it have new versions that run on it. One of them is new on this blog: laravel-mailing-list, the newsletter my blog needs. Below is what it does, and what changes in your own application when you move it to 0.2.

laravel-mailing-list: your list, in your database

People subscribe and confirm, you send campaigns through your own mailer, and they leave with one click. There's no service charging per subscriber, and the addresses stay in your database.

Declare the lists in src/app.rs:

MailingListServiceProvider::new()
    .list("blog", List::local().name("AxVi blog"))
    .list("product", List::local().name("Product news"))
✨ อัปเดตล่าสุด: axvidev/a mailing list for laravel rust and all five packages on laravel rust - สรุปดราม่าล่าสุด ✨ อัปเดตล่าสุด: axvidev/a mailing list for laravel rust and all five packages on laravel rust - สรุปดราม่าล่าสุด

The package registers a route for subscribe forms, so a plain HTML form is enough. It answers an ordinary form with a redirect, htmx with a fragment, and an API client with JSON:

<form method="post" action="/mailing-list/blog/subscribe">
    {{ csrf_field() }}
    <input type="email" name="email" required>
    <button>Subscribe</button>
</form>
✨ อัปเดตล่าสุด: axvidev/a mailing list for laravel rust and all five packages on laravel rust - สรุปดราม่าล่าสุด ✨ อัปเดตล่าสุด: axvidev/a mailing list for laravel rust and all five packages on laravel rust - สรุปดราม่าล่าสุด

A subscription starts pending and becomes real when the person clicks the link in the confirmation email. Each one keeps where it came from, the IP address, and when it was asked for and confirmed. Asking again resends the email at most every 10 minutes, so nobody can use your form to flood someone's inbox.

A campaign is Markdown or HTML with the subscriber's name and their own unsubscribe link:

let campaign = lists::get("blog")
    .await?
    .campaign("New on the blog")
    .markdown("Hi {{ subscriber.name }}, there's a new post on the blog.")
    .create()
    .await?;

campaign.send().await?;
✨ อัปเดตล่าสุด: axvidev/a mailing list for laravel rust and all five packages on laravel rust - สรุปดราม่าล่าสุด ✨ อัปเดตล่าสุด: axvidev/a mailing list for laravel rust and all five packages on laravel rust - สรุปดราม่าล่าสุด

create renders the campaign once for a sample subscriber, so a mistake in the template fails there, with the line number, and not halfway through the send.

Three things I care about in a newsletter tool, and the package does them for you:

  • Nobody gets a campaign twice. Each subscriber's copy is a row a worker claims before it mails it. A crash, a deploy, a job that runs twice or two workers at once never send the same copy again.
  • Unsubscribing takes one click. Every email carries the List-Unsubscribe headers that Gmail and Yahoo require from bulk senders. The link in the footer opens a page with a button, so mail scanners that open every link unsubscribe no one.
  • Your mail server isn't flooded. Sending is rate limited across all workers, 10 emails a second by default.

If a list already lives in Mailchimp, declare it as List::mailchimp("audience-id"): the same calls and the same subscribe route add, read and remove members there. And with laravel-template-engine, the people who run the business edit the confirmation email and write campaigns themselves, in every language, without a deploy.

Moving your application to Laravel Rust 0.2

Until 1.0, a new minor version of Laravel Rust may change the API, and Cargo treats 0.1 and 0.2 as incompatible. An application that asks for laravel-rust = "0.1" stays on 0.1 until you change that line. That's why every package got a new version, not a patch:

Package Laravel Rust 0.1 Laravel Rust 0.2
laravel-nestedset 0.2 0.3
laravel-comments 0.2 0.3
laravel-maintenance 0.2 0.3
laravel-template-engine 0.4 0.5
laravel-mailing-list 0.1 0.2

The one change most applications need is where the migrations are declared. The migrations! macro now defines a function at module level, which lets rust-analyzer see your migration files:

// Before
Application::configure()
    .with_migrations(laravel::db::migrations!("database/migrations"))

// After
laravel::db::migrations!(fn migrations = "database/migrations");

Application::configure()
    .with_migrations(migrations())
✨ อัปเดตล่าสุด: axvidev/a mailing list for laravel rust and all five packages on laravel rust - สรุปดราม่าล่าสุด ✨ อัปเดตล่าสุด: axvidev/a mailing list for laravel rust and all five packages on laravel rust - สรุปดราม่าล่าสุด

A package's service provider changes the same way, to app.migrations().add(migrations()). Two behaviour changes are worth a look too: with read replicas, a request that wrote to the database now keeps reading from the writer, and a zero DECIMAL(10, 2) now prints as 0.00, not 0. Check your JSON snapshots for the second one.

All five packages pass their tests on 0.2 with SQLite, PostgreSQL and MySQL.

Links

All five are open source, MIT or Apache-2.0. If something is missing or doesn't work, open an issue on GitHub.

If you'd like to see how to build a complete application on Laravel Rust, from the first command to deployment, that's what my book Laravel Rust in Practice is about.

Top comments (0)