Super Simple Laravel Newsletter Subscribers System

profile picture

Super Simple Laravel Newsletter Subscribers System

If you're building a website where you create content, or where your users post content (like a job board or something), then sometimes you might want to send an email to a list of users to let them know new content is available.

An easy solution would be use Mailchimp or any other newsletter service provider.

But now you want to simply generate a simple HTML view and send this view by email. And using those newsletter provider is often not easy.

So what you need is actually a table in your database where you very simply list email addresses of the users that want to subscribe to your newsletter.

Then you create a Scheduled Command that will trigger a job to actually send a automated email to all those email addresses.

And if, like me, you're building new projects every month, you probably want to implement this same table of "subscribers" into all your projects.

This is where my package mydnic/laravel-subscribers will help you!

Pulling this package in your project will instantly provide you a Subscriber model, with the corresponding table.

All you need to do is to put a simple form into your template. The code for this form is also provided.

Installation

You may use Composer to Install Laravel Subscribers:

composer require mydnic/laravel-subscribers

The package will automatically register itself

You then must publish the migration with:

php artisan vendor:publish --provider="Mydnic\Subscribers\SubscribersServiceProvider" --tag="subscribers-migrations"

Usage

In your view, you simply need to add a form that you can customize the way you want

<form action="{{ route('subscribers.store') }}" method="post">
    @csrf
    <input type="email" name="email">
    <input type="submit" value="submit">
</form>

@if (session('subscribed'))
    <div class="alert alert-success">
        {{ session('subscribed') }}
    </div>
@endif

Delete

Simply provide this link to your subscribers:

<a href="{{ route('subscribers.delete', ['email' => $subscriber->email]) }}">unsubscribe</a>

This will generate a link like /subscribers/delete?email=email[email protected]


Now you have everything you need!

All you need to do is to build your email template and send it to your subscribers!

package
laravel
subscribers
newsletter
mydnic

2019 My Dynamic Production SPRL All rights Reserved.