02.11.2018       Выпуск 254 (29.10.2018 - 04.11.2018)       Статьи

Отправляем email'ы с помощью 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

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

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

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:

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

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

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

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

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'

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:

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.

dev.to

is where software developers stay in the loop and avoid career stagnation.

Signing up (for free!) is the first step.


Лучшая Python рассылка




Разместим вашу рекламу

Пиши: mail@pythondigest.ru

Нашли опечатку?

Выделите фрагмент и отправьте нажатием Ctrl+Enter.

Система Orphus