In this example, we will see how we can send an email with Masonite smtp configuration. Masonite provides an inbuilt mail configuration for sending emails.
Masonite Installation
mkdir masonite-mail
cd masonite-mail
Activating Our Virtual Environment
python -m venv venv
source venv/bin/activate
Install Masonite
pip install masonite
Start project
project start .
Start Development Server
python craft serve
This is it! We have the new Masonite Project running now!
env Configuration
In first step, we have to add mail configuration with mail driver, mail host, mail port, mail username, mail password so Masonite will use those sender configuration for sending email. For this tutorial, we will use mailtrap.io but it is the similar for any other mail provider. So you can simply add the following in .env
.
MAIL_DRIVER=smtp
MAIL_FROM=<from_email_address>
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=25
MAIL_USERNAME=<mailtrap username>
MAIL_PASSWORD=<mailtrap password>
Create Mailable Class
In this step we will create mail class SendEmail for email sending. So let's run bellow command.
python craft mailable SendEmail
SendEmail.py file will be created in the following path app/mailables
with the below default configuration.
app/mailables/SendEmail.py
from masonite.mail import Mailable
class SendEmail(Mailable):
def build(self):
return (
self.to("user@gmail.com")
.subject("Masonite 4")
.from_("admin@gmail.com")
.text("Hello from Masonite!")
.html("<h1>Hello from Masonite!</h1>")
)
Create Controller
Let's create the controller that we will use to send the email.
python craft controller SendEmailController
- app/controllers/SendEmailController.py
from masonite.controllers import Controller
from masonite.views import View
from masonite.mail import Mail
from app.mailables.SendEmail import SendEmail
class SendEmailController(Controller):
def sendEmail(self, mail: Mail):
mail.mailable(SendEmail().to('test@example.com')).send()
return "Email sent!"
Create Routes
In this step, we need to create the route for sending email. So we will open our "routes/web.py" file and add following route.
from masonite.routes import Route
ROUTES = [Route.get("/", "WelcomeController@show"),
Route.get("/sendemail", "SendEmailController@sendEmail"),]
Now everything is in place so we can visit the following url to send the email.
http://127.0.0.1:8000/sendemail
And it works!
Advanced example
Let's see a more advanced example about sending email. We are going to create a html template, pass some data to it and send it.
First of all we need to modify the SendEmailController.py
as follow:
app/controllers/SendEmailController.py
from masonite.controllers import Controller
from masonite.views import View
from masonite.mail import Mail
from app.mailables.SendEmail import SendEmail
class SendEmailController(Controller):
def sendEmail(self, mail: Mail):
mail.mailable(SendEmail().to('test@example.com')).send()
return "Email sent!"
and the SendEmail.py
as follow:
app/mailables/SendEmail.py
from masonite.mail import Mailable
class SendEmail(Mailable):
def build(self):
data={
'title': 'Hello World',
'body': 'This is a test email.'
}
return (
self.subject("Welcome to popolo.dev!")
.view("mailables.email", {'data': data})
)
Now we will create the html file with name email.html
in a new folder templates/mailables
.
templates/mailables/email.html
<!DOCTYPE html>
<html>
<head>
<title>popolo.dev</title>
</head>
<body>
<h1>{{ data['title'] }}</h1>
<p>{{ data['body'] }}</p>
<p>Thank you</p>
</body>
</html>
Now everything is in place so we can visit the following url to send the email.
http://127.0.0.1:8000/sendemail
And it works!