Mail
Masonite has a simple yet powerful mail feature which is used to send emails from your application.

Creating Emails

To create and send an email with Masonite, you must first built a Mailable class. This class will act as the settings for your email such as the address you are sending from, the subject, the text of the email, the html of the email, etc.
All of these settings on the mailable can be changed or overwritten when you are ready to send you email, more on this later on.
The first step of building a mailable is running the command:
1
$ python craft mailable Welcome
Copied!
This will create your mailable and it will look something like this:
1
class Welcome(Mailable):
2
def build(self):
3
(
4
self.subject("Welcome to our site!")
5
6
.view("mailables.welcome", {})
7
)
Copied!

Sending Mailables

You can send your mailable inside your controller easily by using the Mail class:
1
from masonite.mail import Mail
2
from app.mailables.Welcome import Welcome
3
4
class WelcomeController(Controller):
5
6
def welcome(self, mail: Mail):
7
mail.mailable(Welcome().to('[email protected]')).send()
Copied!
Notice at this point you can call any building options you want on the mailables to modify the behavior of it before sending.

Mail Options

You can modify the behavior of the mailable by using any one of these options
Options
Description
Specifies the user to send the email to. You may also specify the users name like to('Joseph <[email protected]>').
Specifies the address that the email should appear it is from.
A list of the addresses that should be "carbon copied" onto this email
A list of the addresses that should be "blind carbon copied" onto this email
subject('Subject of the Email')
Specifies the subject of the email.
reply_to('[email protected]')
Specifies the address that will be set if a user clicks reply to this email
text('Welcome to Masonite')
Specifies the text version of the email.
html('Welcome to Masonite')
Specifies the HTML version of the email.
view('mailables.view', {})
Specifies a view file with data to render the HTML version of the email
priority(1)
Specifies the priority of the email, values should be 1 through 5.
low_priority()
Sets the priortiy of the email to 1
high_priority()
Sets the priortiy of the email to 5
attach('MAY.pdf', 'path/invoice.pdf')
Attaches a file to the email

Sending Attachments

Sending attachments is really simply with Masonite. Simply attach the file to the mailable before sending it:
1
user = user.find(1)
2
welcome_mailable = WelcomeMailable().to(f"{user.name} <{user.email}>')
3
welcome_mailable.attach("MAY-2021-invoice.pdf", "storage/pdf/invoice.pdf")
4
mail.mailable(welcome_mailable).send()
Copied!
You will then see your attachment in the email.

Mailable Responses

When you are building your emails it might be nice to see how they look before sending them. This can save a lot of time when you're trying to get those styles just right.
You can simply return your mailable in your controller and it will return like a normal view file.
1
from app.mailables.Welcome import Welcome
2
3
class WelcomeController(Controller):
4
5
def welcome(self):
6
return Welcome()
Copied!
If you are using the view() option in your mailable then you will need to set the application on the mailable:
1
from app.mailables.Welcome import Welcome
2
from wsgi import application
3
4
class WelcomeController(Controller):
5
6
def welcome(self):
7
return Welcome().set_application(application)
Copied!

Changing Drivers

You can change the driver which sends the email by using the driver argument in the send() method:
1
mail.send(Welcome().to('[email protected]'), driver="smtp")
Copied!
Last modified 21d ago