Skip to content

Commit adbe91d

Browse files
committed
wip
1 parent 7b34c50 commit adbe91d

File tree

11 files changed

+440
-1
lines changed

11 files changed

+440
-1
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
build
22
composer.lock
3-
docs
43
vendor
54
coverage
65
.phpunit.result.cache

docs/_index.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
packageName: Laravel Mailbox
3+
githubUrl: https://github.com/beyondcode/laravel-mailbox
4+
---

docs/basic-usage/_index.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Basic Usage
3+
order: 2
4+
---

docs/basic-usage/handling.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Handling Inbound Emails
2+
3+
Once one of your mailboxes matches an incoming email, you have access to the `InboundEmail` object that references this email. This class has a lot of convenience methods for you, to access the email content.
4+
5+
Under the hood, this class uses the [Mail Mime Parser](https://mail-mime-parser.org) package to parse the incoming email MIME and access it.
6+
7+
## Available methods
8+
9+
### id()
10+
11+
If your incoming email has a `Message-Id` header field, you can access the unique message ID using `$email->id()`. If no such header field exists, a random id will be generated.
12+
13+
### date()
14+
15+
Returns a `Carbon` object of the emails `Date` header value.
16+
17+
### html()
18+
19+
This method will return the emails HTML content.
20+
21+
### text()
22+
23+
This method will return the emails text content.
24+
25+
### subject()
26+
27+
Returns the subject of the email.
28+
29+
### from()
30+
31+
Returns the senders email address.
32+
33+
### fromName
34+
35+
Returns the name of the sender.
36+
37+
### to()
38+
39+
Returns an array containing all recipient emails and names.
40+
41+
### cc()
42+
43+
Returns an array containing all cc emails and names.
44+
45+
### attachments()
46+
47+
Returns an array containing all attachments.
48+
49+
For example, to store all attachments to a file on a Laravel storage:
50+
51+
```php
52+
foreach ($email->attachments() as $attachment) {
53+
$filename = $attachment->getFilename();
54+
55+
$attachment->saveContent(storage_path($filename));
56+
}
57+
```
58+
59+
### message()
60+
61+
Returns the raw `ZBateson\MailMimeParser\Message` message object, if you want to do additional tasks with the mail. Please refer to the [Mail Mime Parser documentation](https://mail-mime-parser.org).
62+
63+
## Responding to emails
64+
65+
The `InboundEmail` class also has two methods to make it easy for you to either reply or forward the incoming email in your application.
66+
67+
### Replying
68+
69+
To reply to the inbound email, you can use the `reply` method. It receives a [Laravel Mailable](https://laravel.com/docs/5.7/mail#generating-mailables) as a parameter, where you can customize the email that will be sent as a response.
70+
71+
```php
72+
Mailbox::from('[email protected]', function (InboundEmail $email) {
73+
$email->reply(new FeedbackReceived);
74+
});
75+
```
76+
77+
### Forwarding
78+
79+
In addition to replying the incoming emails, you may also want to forward the email to other recipients after your automatic processing is done:
80+
81+
```php
82+
Mailbox::from('[email protected]', function (InboundEmail $email) {
83+
$email->forward('[email protected]');
84+
});
85+
```
86+
87+
This method accepts either an array of email addresses, a user object or an email string.
88+
89+
## Storing emails
90+
91+
By default, this package stores all matched incoming emails in your database. You can define the number of days, these emails will remain in your application in the `config/mailbox.php` file.
92+
93+
To automatically remove older emails, this package provides an artisan command `mailbox:clean`.
94+
95+
Running this command will result in the deletion of all inbound emails that are older than the number of days specified in the `store_incoming_emails_for_days` setting of the config file.
96+
97+
You can leverage Laravel's scheduler to run the clean up command now and then.
98+
99+
```php
100+
//app/Console/Kernel.php
101+
102+
protected function schedule(Schedule $schedule)
103+
{
104+
$schedule->command('mailbox:clean')->daily();
105+
}
106+
```

docs/basic-usage/mailboxes.md

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Mailboxes
2+
3+
This package works by listening for incoming emails from one of the supported drivers and then "reacting" to an incoming email. This happens in custom Mailbox classes - you can think of them as custom route handlers for your emails.
4+
5+
## Defining Mailboxes
6+
7+
You can define your mailboxes in one of your Laravel service providers. For example, within the `boot` method of your `AppServiceProvider`.
8+
9+
```php
10+
use BeyondCode\Mailbox\InboundEmail;
11+
use BeyondCode\Mailbox\Facades\Mailbox;
12+
13+
class AppServiceProvider extends ServiceProvider
14+
{
15+
/**
16+
* Bootstrap any application services.
17+
*
18+
* @return void
19+
*/
20+
public function boot()
21+
{
22+
Mailbox::from('[email protected]', function (InboundEmail $email) {
23+
// Handle the incoming email
24+
});
25+
}
26+
}
27+
```
28+
29+
A single mailbox takes care of handling one specific kind of email. You can either define a closure as the second argument, or use an invokable class. This method/class will then get executed every time your application receives an incoming email that matches the mailbox pattern and subject.
30+
31+
## Invokable classes
32+
33+
Instead of the closure based approach, you can also pass a class name as the second argument of the mailbox create methods. This class will then be created and executed:
34+
35+
```php
36+
Mailbox::from('[email protected]', MyMailbox::class);
37+
38+
class MyMailbox
39+
{
40+
public function __invoke(InboundEmail $email)
41+
{
42+
// Handle the incoming email
43+
}
44+
}
45+
```
46+
47+
## Matching sender emails
48+
49+
To create a mailbox that matches a specific sender email address, you may use the `Mailbox::from` method.
50+
51+
This mailbox will be called whenever the sender of the email addresses matches.
52+
53+
```php
54+
Mailbox::from('[email protected]', MyMailbox::class);
55+
```
56+
57+
## Matching recipient emails
58+
59+
To create a mailbox that matches a specific recipient email address, you may use the `Mailbox::to` method.
60+
61+
This mailbox will be called whenever at least one of the email recipients matches.
62+
63+
```php
64+
Mailbox::to('[email protected]', MyMailbox::class);
65+
```
66+
67+
## Matching CC emails
68+
69+
Similar to matching email recipients, you may also want to restrict your mailbox to the incoming emails CC attribute. Use the `Mailbox::cc` method for this.
70+
71+
This mailbox will be called whenever at least one of the cc recipients matches.
72+
73+
```php
74+
Mailbox::cc('[email protected]', MyMailbox::class);
75+
```
76+
77+
## Matching the subject
78+
79+
Instead of checking for the email recipients or sender you can also match against the email subject using the `Mailbox::subject` method.
80+
81+
This mailbox will be called whenever the email subject matches.
82+
83+
```php
84+
Mailbox::subject('Feedback Request', MyMailbox::class);
85+
```
86+
87+
## Catch-All
88+
89+
In some cases you might want to create a mailbox that receives all incoming emails, no matter what they contain.
90+
91+
You can use the `Mailbox::catchAll` method for this. This method only receives a closure/class name that will be called every time your application receives an email.
92+
93+
```php
94+
Mailbox::catchAll(CatchAllMailbox::class);
95+
```
96+
97+
## Fallback
98+
99+
Similar to the "catch-all" mailbox, you might also want to create a fallback mailbox that will be called when none of your other mailboxes match the incoming email. While the `catchAll` mailbox will be called for **every** incoming email, the `fallback` mailbox will only be called when no other mailbox matches.
100+
101+
```php
102+
Mailbox::fallback(FallbackMailbox::class);
103+
```
104+
105+
## Using Parameters
106+
107+
In addition to using fixed strings as your mailbox matching rules, you can also use parameters in curly braces - similar to the Laravel routing.
108+
109+
Just wrap the part of the matching rule that you want to capture as a parameter in curly braces and the parameter value will be passed to your invokable class / callback method.
110+
111+
```php
112+
Mailbox::from('{username}@domain.com', function (InboundEmail $email, $username) {
113+
114+
});
115+
```
116+
117+
## Regular Expression Constraints
118+
119+
You may constrain the format of your mailbox parameters by defining a regular expression in your mailbox definition:
120+
121+
```php
122+
Mailbox::from('{username}@domain.com', function (InboundEmail $email, $username) {
123+
124+
})->where('username', '[A-Za-z]+')
125+
```

docs/drivers/_index.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Drivers
3+
order: 3
4+
---

docs/drivers/drivers.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Available drivers
2+
3+
Once you have configured your mailboxes, you need to connect your email provider with this package.
4+
These are the supported drivers.
5+
6+
You can configure your driver by specifying the `MAILBOX_DRIVER` environment variable in your `.env` file.
7+
8+
## Mailgun
9+
10+
To use Laravel Mailbox with your Mailgun account, you first need to set the `MAILBOX_MAILGUN_KEY` environment variable to your [Mailgun API key](https://help.mailgun.com/hc/en-us/articles/203380100-Where-can-I-find-my-API-key-and-SMTP-credentials-).
11+
12+
You can then set your `MAILBOX_DRIVER` to "mailgun".
13+
14+
Next you will need to configure Mailgun, to send incoming emails to your application at `/laravel-mailbox/mailgun/mime`. So if your application is at `https://awesome-laravel.com`, it would be `https://awesome-laravel.com/laravel-mailbox/mailgun/mime`.
15+
16+
See ["Receiving, Forwarding and Storing Messages"](https://documentation.mailgun.com/en/latest/user_manual.html#receiving-forwarding-and-storing-messages) in the Mailgun documentation.
17+
18+
## Postmark
19+
20+
::: warning
21+
To use Postmark with Laravel Mailbox, you will need to generate a random password and store it as the `MAILBOX_HTTP_PASSWORD` environment variable. The default username is "laravel-mailbox", but you can change it using the `MAILBOX_HTTP_USERNAME` environment variable.
22+
:::
23+
24+
You can then set your `MAILBOX_DRIVER` to "postmark".
25+
26+
Next you will need to configure Postmark, to send incoming emails to your application at `/laravel-mailbox/postmark`. Use the username and the password that you generated for the URL.
27+
28+
If your application is at `https://awesome-laravel.com`, it would be `https://laravel-mailbox:[email protected]/laravel-mailbox/postmark`.
29+
30+
See the official ["Postmark documentation"](https://postmarkapp.com/manual#configure-your-inbound-webhook-url).
31+
32+
::: tip
33+
Be sure the check the box labeled "Include raw email content in JSON payload" when setting up Postmark.
34+
:::
35+
36+
## SendGrid
37+
38+
::: warning
39+
To use SendGrid with Laravel Mailbox, you will need to generate a random password and store it as the `MAILBOX_HTTP_PASSWORD` environment variable. The default username is "laravel-mailbox", but you can change it using the `MAILBOX_HTTP_USERNAME` environment variable.
40+
:::
41+
42+
You can then set your `MAILBOX_DRIVER` to "sendgrid".
43+
44+
Next you will need to configure SendGrid Inbound parse, to send incoming emails to your application at `/laravel-mailbox/sendgrid`. Use the username and the password that you generated for the URL.
45+
46+
If your application is at `https://awesome-laravel.com`, it would be `https://laravel-mailbox:[email protected]/laravel-mailbox/sendgrid`.
47+
48+
See ["SendGrid Inbound Parse"](https://sendgrid.com/docs/for-developers/parsing-email/setting-up-the-inbound-parse-webhook/).
49+
50+
::: tip
51+
Be sure the check the box labeled "Post the raw, full MIME message." when setting up SendGrid.
52+
:::
53+
54+
## MailCare
55+
56+
You can then set your `MAILBOX_DRIVER` to "mailcare".
57+
58+
Next you will need to configure MailCare, to send incoming emails to your application at `/laravel-mailbox/mailcare`:
59+
- Activate authentication and automation features.
60+
- Create a new automation with the URL `https://your-application.com/laravel-mailbox/mailcare`
61+
- Be sure the check the box labeled "Post the raw, full MIME message."
62+
63+
See ["MailCare"](https://mailcare.io) for more information.
64+
65+
## Local development / log driver
66+
67+
When working locally, you might not want to use real incoming emails while testing your application. Out of the box, this package supports Laravel's "log" mail driver for incoming emails.
68+
69+
To test incoming emails, set both your `MAIL_DRIVER` and your `MAILBOX_DRIVER` in your `.env` file to "log".
70+
Now every time you send an email in your application, this email will appear in your `laravel.log` file and will try to call one of your configured Mailboxes.

docs/getting-started/_index.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Getting Started
3+
order: 1
4+
---

0 commit comments

Comments
 (0)