Sending a daily email with Laravel and Campaign Monitor

Published on by

Sending a daily email with Laravel and Campaign Monitor image

Here on Laravel News, we offer multiple ways of staying up to date with new content. Everything from auto-sharing to all the social media channels, a read-only Telegram channel, a weekly newsletter and last March we started offering a daily email digest.

To send the daily email we utilize the Laravel scheduler and Campaign Monitor so it’s completely automated. In this tutorial let’s look at how its all setup and how you can easily add this to your site to start sending out automated emails.

Installation

Before we jump in you will need to install the Campaign Monitor SDK and thanks to Composer its simple. Run the composer require command:

composer require campaignmonitor/createsend-php

Next, add the following three items to your .env file:

CAMPAIGN_MONITOR_API_KEY=
CAMPAIGN_MONITOR_CLIENT_ID=
CAMPAIGN_MONITOR_DAILY_LIST_ID=

You can find the first two values by visiting admin/account/apikeys in your Campaign Monitor account. For the daily list ID, visit the “Lists & Subscribers” and either create a new list or edit an existing. As long as you are an admin on the account you’ll find the “API Subscriber List ID” here.

Take all three of those and save them in the .env file and now we are ready to create the command to send the email.

Creating the Console Command

Now that we have the required pieces in place its time to create a console command that we can hook into the Laravel Scheduler.

php artisan make:command SendDailyEmail

That Artisan command will create the scaffolding of the class and you should open the new file and fill out the signature and description properties:

protected $signature = 'ln:daily';
protected $description = 'Send the daily email';

Next, inside the handle method is where we are going to put the logic. Because it’s a daily email there will be some days where we don’t post a new article. For example holidays, and weekends. So those should be avoided.

To account for this we can do a simple “if” check to make sure at least one post was published in the past 24 hours. Here is how I set it up to check the last email sent:

public function handle()
{
$posts = Post::active()->where('publishes_at', '>', Carbon::parse('yesterday 3pm'))->get();
if (count($posts) > 0) {
return $this->email($posts);
}
}

Now the final portion of this class if the “email()” method that is referenced above. Here is the full code for it:

protected function email($posts)
{
$auth = ['api_key' => config('services.campaign-monitor.key')];
$cm = new CS_REST_Campaigns(null, $auth);
 
$draft = $cm->create(config('services.campaign-monitor.client_id'), [
'Subject' => $posts->first()->title,
'Name' => 'Daily Email ('.date("Y-m-d").')',
'FromName' => 'Laravel News',
'FromEmail' => 'hello@example.com',
'ReplyTo' => 'hello@example.com',
'HtmlUrl' => 'https://site.com/dailyTemplate',
'ListIDs' => [config('services.campaign-monitor.daily_id')],
]);
 
$cm->set_campaign_id($draft->response);
 
$result = $cm->send(array(
'ConfirmationEmail' => 'hello@example.com',
'SendDate' => 'immediately'
));
}

This is where almost all of the magic happens and this is a two-step process. First, we set up a draft campaign using $cm->create and pass in an array of all the required settings. The one unique item here is the HtmlUrl and it is the source of the email.

Next, we set the campaign ID and then we send it immediately through the $cm->send method.

Creating the HTML Email Source

In that last section, we told Campaign Monitor to use a web route as the HtmlUrl and this allows us to create the template with Laravel Blade and have it routable so we can take a look at it before its sent off.

To create this it’s just a standard route, controller, and view, but the view will need to use HTML tables since it will be an email. I used Zurb Foundation as a base, but there are lots of other options available and you can see what others are recommending by looking at the responses to this Tweet.

Scheduling the Command

The final step is to register our Command in the app/Console/Kernel.php file and then set it on the schedule we want.

//...
 
protected $commands = [
SendDailyEmail::class,
];
 
//...
 
protected function schedule(Schedule $schedule)
{
$schedule->command('ln:daily')->daily()->at('15:00');
}

As long as you have your cron setup to call the scheduler it should now all be happening automatically.

Want to view the end results? Subscribe to the daily Laravel News digest and you’ll see it right in your inbox.

Eric L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Bluesky notification channel for Laravel image

Bluesky notification channel for Laravel

Read article
Sprout Multitenancy Package for Laravel image

Sprout Multitenancy Package for Laravel

Read article
Creating a CLI Application With Laravel and Docker image

Creating a CLI Application With Laravel and Docker

Read article
Laravel Herd Adds Native MongoDB Support image

Laravel Herd Adds Native MongoDB Support

Read article
How to Create Tech Videos for YouTube with Josh Cirre image

How to Create Tech Videos for YouTube with Josh Cirre

Read article
Integrate Unsplash in Your Laravel Application image

Integrate Unsplash in Your Laravel Application

Read article