Most of the company send mail to notify their users about something or to promote their products. So, one ideal situation could be setting up daily emails.
To address that problem, we can use the Laravel scheduler and campaign monitor. Using these we can send emails automatically to desired receivers.
In this tutorial, we will learn how to set up fully functional daily emails using Laravel and schedule to send emails daily.
Let’s start the process of installation. First, we need to install the package campaignmonitor/createsend-php. We can easily install this using Composer.
– The command below is for installing Campaign Monitor:
composer require campaignmonitor/createsend-php
– After the above command fully executes, add these values to your .env
file:
CAMPAIGN_MONITOR_API_KEY=
CAMPAIGN_MONITOR_CLIENT_ID=
CAMPAIGN_MONITOR_DAILY_LIST_ID=
– To get the value of the API key and Client ID visit “admin/account/apikeys” in the Campaign Monitor account. As for the daily list ID, visit “List and Subscriber” and create a new list or edit the existing one. Insert all these values in their respective placeholder.
After the successful installation of Campaign Monitor, we create a console command which we will link to Laravel Scheduler.
php artisan make:command SendDailyEmail
The above command creates a structure of a class in which we should fill up the signature and description which is blank by default.
protected $signature = 'ln:daily';
protected $description = 'Send the daily email';
After the completion of the above process, we configure the handle method. In this method, we put in our business logic to prevent sending mail on holidays and weekends.
– To check if at least one email was sent in the last 24 hours we can use the following logic:
public function handle()
{
$posts = Post::active()->where('publishes_at', '>', Carbon::parse('yesterday 5pm'))->get();
if (count($posts) > 0) {
return $this->email($posts);
}
}
Now here is the final and most important section of this class i.e. email()
. We can use the code below for this method:
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 Project',
'FromEmail' => '[email protected]',
'ReplyTo' => '[email protected]',
'HtmlUrl' => 'https://site.com/dailyTemplate',
'ListIDs' => [config('services.campaign-monitor.daily_id')],
]);
$cm->set_campaign_id($draft->response);
$result = $cm->send(array(
'ConfirmationEmail' => '[email protected]',
'SendDate' => 'immediately'
));
}
– In the above method, using $cm->create
we set up a draft campaign and then pass the array which consists of all the important settings like subject, sender, receiver, and other components. The htmlUrl
component of this array denotes the source of an email. After that, we set the campaign ID.
Then, we send the email immediately afterward.
– Here, we configure Campaign Monitor to use a web route HtmlUrl
that allows to creation template with Laravel Blade.
– The template is made routable so we can see the email before it is sent.
– The template consists of a simple route, controller, and view.
We register commands app/Console/Kernel.php
and configure the schedule of email.
protected $commands = [
SendDailyEmail::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('ln:daily')->daily()->at('17:00');
}
So this is all there is to sending daily emails with Campaign Monitor.
More information about Laravel 11 Send Mail with Attachments.
All Comments