DEV Community

Cover image for Sending automated emails with Masonite Framework
Junior Gantin for Masonite

Posted on

2 2

Sending automated emails with Masonite Framework

Imagine a scenario where you want to send a weekly email to your customers (maybe a newsletter 💌). So you need to send that email at a specific day and time.

This post will walk through creating and sending automated emails with Masonite Framework.

What you’ll need

To code along with this post, you’ll need:

  • Python (I’m using Python 3.6.5);
  • Pipenv: Python Development Workflow for Humans;
  • Masonite: The Modern And Developer Centric Python Web Framework
  • Mailtrap.io: Email testing for dev teams;

Setting up your application

First, create a new directory and a Python virtualenv:

$ mkdir masonite-weekly-email
$ cd masonite-weekly-email
$ pipenv install --three
$ pipenv shell
Enter fullscreen mode Exit fullscreen mode

After that, you'll need to install masonite-cli package and crafting a new
Masonite application.

$ pipenv install masonite-cli
$ craft new masonite_weekly_email .
$ craft install
Enter fullscreen mode Exit fullscreen mode

This will create a new Masonite application inside your current directory (the use of . at the end of craft new command).

Creating and sending an Email

Masonite comes with email support out of the box 🎉. So you can easily send an email like this:

Mail.to('hello@email.com').template('mail/welcome').send()
Enter fullscreen mode Exit fullscreen mode

Task Scheduling

Now that we are done with sending email, let's schedule it.
Masonite provides a package called masonite-scheduler. It enables your app to schedule cron tasks. First, you need to run a command to install the package:

$ pipenv install masonite-scheduler
Enter fullscreen mode Exit fullscreen mode

The second command is a craft task command which will create a new task under the app/tasks directory.

$ craft task WeeklyEmail
Enter fullscreen mode Exit fullscreen mode

Before running our task weekly, let's run it every 1 minute.

from scheduler.Task import Task

class WeeklyEmail(Task):

    run_every = '1 minute'

    def __init__(self, Mail):
        self.mail = Mail

    def handle(self):
        self.mail.subject('Build your next SaaS with Masonite 🚀')\
                .to('hello@email.com').template('mail/weekly')\
                .send()

Enter fullscreen mode Exit fullscreen mode

Above, you send a email with a template located under mail folder. Create a file called weekly.html and put a random text inside.

<h2>Hello world!</h2>
<p><a href="https://github.com/MasoniteFramework/masonite">Masonite</a> is magic. ✨ </p>
<p>
    There are many benefits Masonite brings to your next SaaS project.
</p>
<img src="https://media.giphy.com/media/3o6Ztqh4JSlVqi2Z20/giphy.gif" alt="just do it">
Enter fullscreen mode Exit fullscreen mode

Let's test this before setting up a cron job. Put your Mailtrap credentials into your .env file and run this command.

$ craft schedule:run
Enter fullscreen mode Exit fullscreen mode

Let's Masonite fetch and run your task!

Alt Text

Let's put the right parameters and tell the task when it should run (remember a weekly email).

class WeeklyEmail(Task):
    run_every = '7 days'
    run_at = '17:00'

Enter fullscreen mode Exit fullscreen mode

Cron Jobs

You need to set up the Cron Jobs to run automatically our tasks. Each crontab line must start with a time at which the command should be run and then the command:
So, to run command at 17:00 every monday, you'd do:

00 17 * * 1 command
Enter fullscreen mode Exit fullscreen mode

You need to append && craft schedule:run to run your run the scheduled task.

Masonite Task Scheduling documentation page can provide further information. Just read it ✊!

If you want to contribute or interested in the development of Masonite then be sure to join the Slack channel or star the repo on GitHub.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay