# Mailing

Miru has some Mailing facilities on top of FluentEmail.

# Create

An easy way to create a Mailable and an email template is using a Maker from MiruCli:

miru make:mail Accounts Account Created

# Mailable

A Mailable is the object that will be passed to the Sender or to the Queue. It has the email properties like subject, body, sender, receiver, and etc.

    public class AccountRegisteredMail : Mailable
    {
        private readonly User _user;

        public AccountRegisteredMail(User user)
        {
            _user = user;
        }

        public override void Build(Email mail)
        {
            mail.To(_user.Email, _user.Name)
                .Subject("Activate Your Mong Account")
                .Template("_Registered", _user);
        }
    }

# Template

The Template is a Razor file that will be transformed into html before send the email. Markdown can be used between the tags <markdown></markdown>.

@model Mong.Domain.User

<markdown>
## Mong

Hello!

[Click here to activate your account](@Url.Full(new AccountActivate.Query { Token = Model.ConfirmationToken })

Regards,

Mong Team
</markdown>

# Sending

Send email is done using Miru.Mailing.IMailer:

var user = _db.Users.ById(id);

// send now synchronously
await _mailer.SendNowAsync(new AccountRegisteredMail(user));

// queue to send asynchronously
await _mailer.SendLaterAsync(new AccountRegisteredMail(user));