Generating newsletter content with the Mailcoach API

Generating Markdown on the server using the Mailcoach API can simplify repetitive tasks by automatically pulling in data from various sources to generate a newsletter. This makes managing and sending newsletters easier without manual intervention.

On this page:

For instance, let’s say you have a list of interesting links saved in your database that you want to include in every newsletter. Instead of typing them out manually, you could generate a Markdown document with these links by running an artisan command. When the command runs, the content will be generated and appear in the Mailcoach dashboard, ready to be reviewed and sent.

Another example is fetching data from an external API, such as generating a newsletter with the latest GitHub issues from the Laravel repository. Here’s how you can do it.

This guide uses Laravel & PHP as an example. However, our API can be accessed from any environment.

Create an Artisan command

First, fetch the data using the GitHub API to retrieve the latest issues. Create an artisan command to generate the newsletter. Using an Artisan command is just one way of generating content on the server, but it’s a useful way that can be automated later on.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;

class SendNewsletter extends Command
{

    protected $signature = 'app:generate-newsletter';

    protected $description = 'Fetch latest Laravel issues and generate a newsletter';

    public function handle()
    {
        $client = new \GuzzleHttp\Client();
        $response = $client->get('https://api.github.com/repos/laravel/framework/issues');
        $issues = json_decode($response->getBody()->getContents());
        $latestIssues = array_slice($issues, 0, 10);

        $markdownContent = "# 10 Latest Laravel Issues\n\n";

        foreach ($latestIssues as $issue) {
            $markdownContent .= "- [**{$issue->title}**]({$issue->html_url}) by {$issue->user->login}\n\n";
        }

        $this->sendToMailcoach($markdownContent);
        $this->info('Newsletter sent successfully!');
    }
}

This command fetches the latest 10 issues including their authors, and generates a Markdown document.

Send Markdown to Mailcoach using Mailcoach API

Next, we will create the sendToMailcoach() method which will take the generated markdown and send it to Mailcoach using the Mailcoach API:

private function sendToMailcoach($markdownContent)
{
    $apiKey = env('MAILCOACH_API_KEY');
    $name = '10 Latest Laravel Issues - ' . date('d M Y');

    Http::withToken($apiKey)
        ->acceptJson()
        ->post('https://zuzana-k.mailcoach.app/api/campaigns', [
            'name' => $name,
            'fields' => [
                'title' => 'Weekly Update',
                'content' => $markdownContent,
            ],
            'from' => 'test@zuzana-k.com',
            'from_name' => 'Zuzana',
            'subject' => 'Weekly Laravel Issues Updates',
            'email_list_uuid' => '3e736304-a6e0-4b7a-8302-03988a6d7473',
            'template_uuid' => '88172ec3-80b4-4e14-8fac-f6c96ac6ef49',
        ]);
}

We need to specify the name of the newsletter and the email_list_uuid. We also need to specify the template_uuid so that Mailcoach knows what template to use for our newsletter. This is required when using markdown. The rest of the data is optional but since we are using the power of automation, we might as well set as many fields as possible.

Let’s test our command by running php artisan app:generate-newsletter in the terminal and as you can see, the newsletter is showing up in the Mailocoach’s campaigns section.

screenshot

We can give it a quick look over and add a short introduction if we wish to, before sending it to our audience.

screenshot

Schedule the Command

The benefit of generating the newsletter with an artisan command is that we could create a scheduler to run the command for us every Monday morning at 6am. Let’s do that now:

In the console/Kernel.php file, add the command to the schedule() method. To test everything is working, you can initially schedule the command to run every minute:

$schedule->command('app:generate-newsletter')->everyMinute();

And to run the scheduler locally, run it with php artisan schedule:work. This will invoke the scheduler every minute until you terminate the command.

Now let’s have a look at our Mailcoach dashboard. We can see new newsletters, which means that the command and our scheduler are working.

screenshot

Don’t forget to set a cron job on your server to make sure this task is run in production.

By automating the generation and scheduling of newsletters, you can free up time for more creative tasks. From now on, every Monday at 6 am, the scheduler will run the generate-newsletter command, fetch the latest Laravel issues from GitHub, generate a Markdown document, and send it via the Mailcoach API to Mailcoach.