Skip to content

Commit 7b5c0a3

Browse files
committed
init project
1 parent 162601a commit 7b5c0a3

File tree

9 files changed

+244
-2
lines changed

9 files changed

+244
-2
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/github.laravel-postal-driver.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "php-monstaers/laravel-postal-driver",
3+
"description": "Laravel mail transport driver for Postal API",
4+
"type": "library",
5+
"require": {
6+
"php": "^8.0",
7+
"guzzlehttp/guzzle": "^7.0"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"PhpMonsters\\LaravelPostalDriver\\": "src/"
12+
}
13+
},
14+
"extra": {
15+
"laravel": {
16+
"providers": [
17+
"PhpMonsters\\LaravelPostalDriver\\PostalTransportServiceProvider"
18+
]
19+
}
20+
},
21+
"minimum-stability": "dev",
22+
"prefer-stable": true
23+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace PhpMonsters\LaravelPostalDriver;
4+
5+
use Illuminate\Mail\Transport\Transport;
6+
use Swift_Mime_SimpleMessage;
7+
use GuzzleHttp\Client;
8+
9+
class PostalTransport extends Transport
10+
{
11+
protected string $key;
12+
protected string $endpoint;
13+
protected Client $client;
14+
15+
public function __construct(string $key, string $endpoint)
16+
{
17+
$this->key = $key;
18+
$this->endpoint = rtrim($endpoint, '/');
19+
$this->client = new Client();
20+
}
21+
22+
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null): int
23+
{
24+
$to = array_keys($message->getTo())[0];
25+
$from = array_keys($message->getFrom())[0];
26+
$subject = $message->getSubject();
27+
$html = $message->getBody();
28+
$plain = strip_tags($html);
29+
30+
$response = $this->client->post($this->endpoint . '/api/v1/send/message', [
31+
'headers' => [
32+
'X-Server-API-Key' => $this->key,
33+
'Content-Type' => 'application/json'
34+
],
35+
'json' => [
36+
'to' => $to,
37+
'from' => $from,
38+
'subject' => $subject,
39+
'plain_body' => $plain,
40+
'html_body' => $html
41+
]
42+
]);
43+
44+
return $this->numberOfRecipients($message);
45+
}
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace PhpMonsters\LaravelPostalDriver;
4+
5+
use Illuminate\Mail\MailManager;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class PostalTransportServiceProvider extends ServiceProvider
9+
{
10+
public function boot(): void
11+
{
12+
$this->app->make(MailManager::class)->extend('postal', function ($config) {
13+
return new PostalTransport(
14+
$config['key'],
15+
$config['endpoint'] ?? 'https://postal.yourdomain.com'
16+
);
17+
});
18+
}
19+
}

README.md

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,107 @@
1-
# Laravel-Postal-Driver
2-
laravel-postal-driver
1+
# Laravel Postal Mail Driver
2+
3+
A custom Laravel mail transport driver to send emails via the [Postal](https://postal.atech.media) mail server using its **HTTP API** instead of SMTP.
4+
5+
---
6+
7+
## 📦 Features
8+
9+
- Send transactional emails via Postal HTTP API
10+
- Uses Laravel's native `Mail::to()->send()` interface
11+
- Clean, framework-native mail transport driver
12+
- Compatible with Laravel version 8.0+
13+
14+
---
15+
16+
## 🚀 Installation
17+
18+
### Step 1: Require the package (from local path or Git)
19+
20+
If using local development:
21+
22+
```bash
23+
composer require php-monsters/laravel-postal-driver
24+
```
25+
26+
Or add to your composer.json:
27+
28+
```json
29+
"repositories": [
30+
{
31+
"type": "path",
32+
"url": "./packages/PhpMonsters/LaravelPostalDriver"
33+
}
34+
],
35+
"require": {
36+
"php-monsters/laravel-postal-driver": "*"
37+
}
38+
```
39+
Then run:
40+
```bash
41+
composer update
42+
```
43+
44+
## ⚙️ Configuration
45+
### Step 2: Set up .env variables
46+
```dotenv
47+
MAIL_MAILER=postal
48+
POSTAL_API_KEY=your_postal_api_key_here
49+
POSTAL_API_ENDPOINT=https://postal.yourdomain.com
50+
```
51+
52+
### Step 3: Configure config/mail.php
53+
Add to the mailers array:
54+
```php
55+
'mailers' => [
56+
'postal' => [
57+
'transport' => 'postal',
58+
'key' => env('POSTAL_API_KEY'),
59+
'endpoint' => env('POSTAL_API_ENDPOINT'),
60+
],
61+
],
62+
```
63+
64+
## ✉️ Usage
65+
Use Laravel's standard Mail facade:
66+
```php
67+
use App\Mail\TestMail;
68+
use Illuminate\Support\Facades\Mail;
69+
70+
Mail::to('[email protected]')->send(new TestMail());
71+
```
72+
Your Mailable class should work as usual.
73+
74+
## 📐 Under the Hood
75+
This driver makes HTTP POST requests to:
76+
```bash
77+
POST /api/v1/send/message
78+
```
79+
With headers:
80+
```http request
81+
X-Server-API-Key: {your_api_key}
82+
Content-Type: application/json
83+
```
84+
Payload:
85+
86+
```json
87+
{
88+
89+
"from": "[email protected]",
90+
"subject": "Subject Line",
91+
"plain_body": "Text version",
92+
"html_body": "<p>HTML version</p>"
93+
}
94+
```
95+
96+
## 🛠 Requirements
97+
- Laravel 8 or newer
98+
- Postal mail server with API access
99+
- PHP 8.0+
100+
- Guzzle 7+
101+
102+
## 📝 License
103+
This project is open-sourced under the MIT license.
104+
105+
## 🤝 Credits
106+
Made with ❤️ by Aboozar Ghaffari
107+
Postal by [Atech Media](https://postal.atech.media)

0 commit comments

Comments
 (0)