How to Set Up a Mailing List with Laravel, Statamic, and Mailgun

This is a general outline for creating a mailing list with Laravel and Statamic using Mailgun and a service provider:

  1. Set up your Mailgun account and obtain your API key.

  2. Install the Mailgun package for Laravel using Composer:

                                        
                                            composer require mailgun/mailgun-php

                                        
                                    
  1. Add a new class `MailgunServiceProvider` in `app\Providers\` folder:

                                        
                                            <?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Mailgun\Mailgun;

class MailgunServiceProvider extends ServiceProvider
{
    /**
     * Register the Mailgun client with the service container.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(Mailgun::class, function ($app) {
            return Mailgun::create(env('MAILGUN_SECRET', ''));
        });
    }
}

                                        
                                    
  1. Add the Mailgun service provider to your config/app.php file:

                                        
                                            'providers' => [
    // Other service providers...
    
    App\Providers\MailgunServiceProvider::class,
],

                                        
                                    
  1. In your .env file, add your Mailgun API key and domain:

                                        
                                            MAILGUN_SECRET=your-api-key
MAILGUN_DOMAIN=your-domain
[email protected]

                                        
                                    
  1. Create a form in your Statamic site that allows users to enter their email address and submit it to your mailing list.

  2. In your Controler or your form submission handler, use the Mailgun service to add the user's email address to your mailing list:

                                        
                                            <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Mailgun\Mailgun;

class MailController extends Controller
{
    public function subscribe(Request $request, Mailgun $mgClient)
    {
        # Add a subscriber to the mailing list
        $mailing_list = env('MAILGUN_LIST', '');
        $address = $request->get('email');
        [$name] = explode('@', $address);

        try {
            $result = $mgClient->mailingList()->member()->create(
                $mailing_list,
                $address,
                $name
            );
        } catch (\Exception $e) {
            Log::error($e->getCode() . ' ' .$e->getMessage());
            return (new \Statamic\View\View)
                ->template('errors.subscribe')
                ->layout('mail');
        }

       return (new \Statamic\View\View)
            ->template('mail.subscribe')
            ->layout('mail')
            ->with(['result' => $result]);
    }
}
                                        
                                    
  1. Add your route to point to your controller:

                                        
                                            Route::post('/email/subscribe', [\App\Http\Controllers\MailController::class, 'subscribe']);
                                        
                                    
  1. Add you template:
    views ->errors->subscribe `views\errors\subscribe.antlers.html`

                                        
                                            

Thank you for subscribing!

We appreciate your support and are grateful to have you as a member of our community.

We look forward to providing you with valuable content and updates.

Please don't hesitate to reach out if you have any questions or suggestions. Thank you again for your support!

Home page

Views ->mail->subscribe views\errors\subscribe.antlers.html

Conclusion

That's the basic process for setting up a mailing list with Laravel and Statamic using Mailgun. You can also use the Mailgun API to manage your mailing list, such as getting a list of members, updating member information, and so on.