Setup Daily Emails using Laravel

Most of the company sends 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.

Installing Campaign Monitor

Let’s start the process of installation. At first, we need to install package campaignmonitor/createsend-php. We can easily install this using composer.

Command below is for installing Campaign Monitor:

composer require campaignmonitor/createsend-php

After the above command fully executes, add these values in your .env file:

CAMPAIGN_MONITOR_API_KEY=
CAMPAIGN_MONITOR_CLIENT_ID=
CAMPAIGN_MONITOR_DAILY_LIST_ID=

To get value of API key and Client Id visit ‘admin/account/apikeys‘ in Campaign Monitor account. As for daily list Id, visit “List and Subscriber” and create new list or edit existing one. Insert all these values in their respective placeholder.

Creating Console Command for daily mail

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 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 mails in holidays and weekends.

To check if at least an 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);
    }
}

And 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 News',
        '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 setup a draft campaign and then pass array which consists of all the important setting like subject, sender, receiver and other components. HtmlUrl component of this array denotes source of email. After that, we set campaign ID.

Then, we send it the email immediately afterwards.

HTML Email Source

Here, we configure Campaign Monitor to use web route as HtmlUrl which allows to create template with Laravel Blade. The template is made rout-able so we can see the email before it is sent. The template consists of simple route, controller and view.

Config schedule of daily email

We register command in 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 email with Campaign Monitor. Keep Coding. 🙂

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share