generated from alpas/starter
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
249 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,9 @@ | |
- [Debugging Emails](#debugging-emails) | ||
- [Custom Mail Driver](#custom-mail-driver) | ||
|
||
Alpas uses [Simple Java Mail](https://github.com/bbottema/simple-java-mail) library and sprinkles it with some of | ||
its own APIs to make it easy for you to send emails from within your app. You can start with an SMTP driver or a | ||
local file driver or write one of your own drivers. | ||
Alpas uses [Simple Java Mail](https://github.com/bbottema/simple-java-mail) library and sprinkles it | ||
with some of its own APIs to make it easy for you to send emails from within your app. You can | ||
start with an SMTP driver or a local file driver or write one of your own drivers. | ||
|
||
<a name="available-drivers"></a> | ||
### [Available Drivers](#available-drivers) | ||
|
@@ -24,28 +24,32 @@ Currently, Alpas comes bundled with 2 mail drivers: | |
<a name="getting-started"></a> | ||
### [Getting Started](#getting-started) | ||
|
||
If you open `configs/MailConfig.kt`, you'll notice that Alpas has lazily loaded two drivers for you `smpt` for SMTP | ||
Driver and `local` for the Local Mail Driver. You can get an instance of one of these drivers during the runtime | ||
by calling `driver()` method on `MailConfig` class and optionally passing the name of the driver. You can decide | ||
to always use a specific driver by setting `MAIL_DRIVER` variable to one of the driver names. | ||
If you open `configs/MailConfig.kt`, you'll notice that Alpas has lazily loaded two drivers for you—`smpt` for SMTP | ||
Driver and `local` for the Local Mail Driver.You can get an instance of one of these drivers during the runtime | ||
by calling `driver()` method on `MailConfig` class. You can pass the name of a driver or use the default driver. | ||
You can decide to always use a specific driver by setting `MAIL_DRIVER` variable to one of the driver names. | ||
|
||
<a name="composing-and-sending-emails"></a> | ||
### [Composing and Sending Emails](#composing-and-sending-emails) | ||
|
||
To compose an email create an instance of `MailMessage` class and set the properties such as `to`, `subject`, | ||
To compose an email, create an instance of `MailMessage` class and set the properties such as `to`,`subject`, | ||
`message` etc. Once the mail is composed, send it via one of the mail drivers' `send()` method. | ||
|
||
<span class="line-numbers" data-start="5"> | ||
|
||
```kotlin | ||
|
||
fun send(call: HttpCall) { | ||
//... | ||
|
||
val mail = MailMessage().apply { | ||
to = "[email protected]" | ||
subject = "Hello There!" | ||
message = "Just want to say hi!" | ||
} | ||
call.config<MailConfig>().driver().send(mail) | ||
|
||
//... | ||
} | ||
|
||
``` | ||
|
@@ -55,8 +59,8 @@ fun send(call: HttpCall) { | |
<a name="using-view-templates"></a> | ||
### [Using View Templates](#using-view-templates) | ||
|
||
While composing a mail, instead of using plain text, you could also call `view()` method and pass the name of | ||
the view template and an optional argument map to render the email's content. | ||
While composing an email, instead of using plain text, you can also call `view()` method and pass | ||
the name of the view template, and an optional argument map to render the email's content. | ||
|
||
<span class="line-numbers" data-start="5"> | ||
|
||
|
@@ -82,32 +86,31 @@ fun send(call: HttpCall) { | |
<a name="debugging-emails"></a> | ||
### [Debugging Emails](#debugging-emails) | ||
|
||
During development, it is very convenient to save email messages locally for debugging. Alpas supports saving all | ||
your email messages to `storage/mails` folder by using `LocalMailDriver`. To use this driver, make sure the value of | ||
`MAIL_DRIVER` is set to `local` in your `.env` file. | ||
During development, it is very convenient to save email messages locally for debugging. Alpas supports | ||
saving all your email messages to `storage/mails` folder by using `LocalMailDriver`. To use this | ||
driver, make sure the value of `MAIL_DRIVER` is set to `local` in your `.env` file. | ||
|
||
If you really want to send emails across the wire for real testing during development, you could use a service | ||
like [Mailtrap](https://mailtrap.io/), which collects all your emails in a dummy demo inbox. | ||
If you really wish to send emails across the wire for real testing during development, you can use a | ||
service like [Mailtrap](https://mailtrap.io/), which collects all your emails in a dummy demo inbox. | ||
|
||
<a name="custom-mail-driver"></a> | ||
### [Custom Mail Driver](#custom-mail-driver) | ||
|
||
It is easy to add a custom driver. All you have to do is create a class that implements | ||
`dev.alpas.mailing.drivers.MailDriver` interface and override `send(mail: MailMessage)` method. Eventually, register | ||
this new driver in `MailConfig` class with a name. | ||
`dev.alpas.mailing.drivers.MailDriver` interface and override `send(mail: MailMessage)` | ||
method. Eventually, register this new driver in `MailConfig` class under a name. | ||
|
||
Let's see and example of how to write your own [SparkPost](https://sparkpost.com) driver by wrapping their | ||
official [Java client API](https://github.com/SparkPost/java-sparkpost). | ||
Let's see and example of how to write your own [SparkPost](https://sparkpost.com) driver by | ||
wrapping their official [Java client API](https://github.com/SparkPost/java-sparkpost). | ||
|
||
<div class="ordered-list"> | ||
|
||
1. Create the driver | ||
1. Create the driver: | ||
|
||
<span class="line-numbers" data-start="2"> | ||
<span class="line-numbers" data-start="2" data-file="SparkPostMailDriver.kt"> | ||
|
||
```kotlin | ||
|
||
// SparkPostMailDriver.kt | ||
import dev.alpas.mailing.MailMessage | ||
import dev.alpas.mailing.drivers.MailDriver | ||
|
||
|
@@ -122,13 +125,12 @@ class SparkPostDriver(val apiKey: String, defaultFromAddress: String) : MailDriv | |
|
||
</span> | ||
|
||
2. Register the driver | ||
2. Register the driver: | ||
|
||
<span class="line-numbers" data-start="3"> | ||
<span class="line-numbers" data-start="3" data-file="configs/MailConfig.kt"> | ||
|
||
```kotlin | ||
|
||
// configs/MailConfig.kt | ||
class MailConfig(env: Environment) : BaseConfig(env) { | ||
init { | ||
// ... | ||
|
@@ -144,19 +146,23 @@ class MailConfig(env: Environment) : BaseConfig(env) { | |
|
||
</span> | ||
|
||
3. Use the driver | ||
3. Use the driver: | ||
|
||
<span class="line-numbers" data-start="5"> | ||
|
||
```kotlin | ||
|
||
fun send(call: HttpCall) { | ||
//... | ||
|
||
val mail = MailMessage().apply { | ||
to = "[email protected]" | ||
subject = "Hello There!" | ||
message = "Just want to say hi!" | ||
} | ||
call.config<MailConfig>().driver("sparkpost").send(mail) | ||
|
||
//... | ||
} | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.