Sending automated emails with Masonite Framework
Tags: python, framework, masonite
Published: 2018-11-02 12:11:45

---
title: Sending automated emails with Masonite Framework
published: true
description: A practical guide showing how to send automated emails using Masonite Framework.
tags: python,framework,masonite
cover_image: https://thepracticaldev.s3.amazonaws.com/i/5p1qhm06zy80sh4548hy.png
---
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](https://github.com/MasoniteFramework/masonite).

# 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](https://github.com/MasoniteFramework/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:
```bash
$ mkdir masonite-weekly-email
$ cd masonite-weekly-email
$ pipenv install --three
$ pipenv shell
```
After that, you'll need to install `masonite-cli` package and crafting a new 
[Masonite](https://github.com/MasoniteFramework/masonite) application.

```bash
$ pipenv install masonite-cli
$ craft new masonite_weekly_email .
$ craft install
```
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: 
```python
Mail.to('hello@email.com').template('mail/welcome').send()
```

# Task Scheduling
Now that we are done with sending email, let's schedule it. 
Masonite provides a package called [`masonite-scheduler`](https://docs.masoniteproject.com/useful-features/task-scheduling). It enables your app to schedule cron tasks. First, you need to run a command to install the package:

```bash
$ pipenv install masonite-scheduler
```
The second command is a `craft task` command which will create a new task under the `app/tasks` directory.

```bash
$ craft task WeeklyEmail
```
Before running our task weekly, let's run it every 1 minute.

```python
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()

```

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

```html
<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">
```

Let's test this before setting up a cron job. Put your [Mailtrap](https://mailtrap.io/) credentials into your `.env` file and run this command.

```python
$ craft schedule:run
```
Let's Masonite fetch and run your task!

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/wxs20gtlvzsz0cidi2xo.gif)

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

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

```
# 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
```
You need to append `&& craft schedule:run` to run your run the scheduled task.

[Masonite Task Scheduling](https://docs.masoniteproject.com/useful-features/task-scheduling) documentation page can provide further information. Just read it ✊!


If you want to contribute or interested in the development of [Masonite](https://github.com/MasoniteFramework/masonite) then be sure to join the [Slack](http://slack.masoniteproject.com/) channel or star the repo on [GitHub](https://github.com/MasoniteFramework/masonite).