Download the PHP package 2am.tech/laravel-mail-api without Composer
On this page you can find all versions of the php package 2am.tech/laravel-mail-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download 2am.tech/laravel-mail-api
More information about 2am.tech/laravel-mail-api
Files in 2am.tech/laravel-mail-api
Package laravel-mail-api
Short Description Laravel mail API microservice
License MIT
Homepage https://github.com/2amigos/laravel-mail-api
Informations about the package laravel-mail-api
Laravel Mail API
About Mail API Service
Laravel Mail API is an email microservice to avoid having to configure mail over and over on projects involving microservices infrastructure.
It uses Signed AppKeys to handle the authentications through requests. The mailer transport interface was build using Laravel Mail (powered by Symfony Mailer) with Markdown Mail Notifications to enhance the email layout configuration.
To accomplish dispatching emails with an efficient response time, Laravel Mail API uses Laravel Queues to execute the tasks in background. We already have configured a driver for Redis (Predis) connection. You are free to configure other driver if it's needed.
Finally, it makes use of Laravel Localization for content internationalization and Laravel Logs for logging.
Install
Configuration
Docker Container Configuration
A docker image was provided through Laravel Sail.
You can configure a shell alias for Sail command and make it easier to access.
Please, refer to Sail Docs to know more about executing commands in your application's container.
To start up/stop the docker container, use the following commands: To be able to use the docker container, you need to first install the dependencies. To achieve that, you must execute the given command:
If you're using Laravel Sail, refer to .env.sail for redis connection strings.
The .env.example file gives the basic structure your project must have in order to run the service properly. Copy its content to .env file.
Application Configuration
As mentioned before, we're using Signed AppKeys for requests authentication.
First, you have to define you're Access Token (AppKey and AppSecret) on
./config/laravel-mail-api
, on AccessTokens
section.
We already provided a sample access key.
You can register as many access token it's necessary for your microservices.
With the access token defined, we just need to sign it to and add its values to the request header.
Access Token Sign Algorithm
Define a hash algorithm to create a hashed token. The default is sha512
.
You can define it on by adding HASH_SIGNATURE
on the .env
file:
HASH_SIGNATURE=sha512
To create a signature for you access token, you follow this steps:
Get the current timestamp (on UTC) on ISO-8601 format.
Then, simple hash your AppKey with the gotten timestamp, using your AppSecret as the hash password.
A command was created to help the signature creation, for testing purposes:
Email Transport Configuration
You must configure your mailer transport on .env file as well. This project was built using SMTP. Laravel Mail provides an easy way to configure the driver your project needs.
This is a sample SMTP Driver configuration:
Usage
To serve the application, Laravel provides the handy built in command serve
This command serve your application at http://127.0.0.1:8000. If you run the command through Laravel Sail, the application will be served on port 80 instead http://127.0.0.1.
Endpoints
The API has one endpoint: /api/email/send
.
A postman collection Laravel Mail API
has been served to simplify the testing process.
/api/email/send
In order to have the authentication mechanism working, we must add the following headers:
Then you can send multipart/form-data
request with the following parameters:
- from
- sender (optional)
- to
- receiver (optional)
- subject
- language (optional, default=en)
- template (optional, default defined on the application)
- attachments[] (optional)
Here is a sample request:
Done. Now your new message is on the queue, ready to be dispatched. To achieve that, you just need to run this command:
Email Attachments
The /api/email/send
endpoint apply validations for attachments mimetypes.
By default, the application will allow PDF
and any Image
mimetypes.
You can easily set an array of your needed mimetypes, or even set a string '*'
to allow any mimetype.
e.g. to allow any file mimetype, you just need to change this line on config/laravel-mail-api.php
:
Customization
As mentioned before, this service uses Markdown Mail Notifications to enhance the email layout configuration.
You can find the template files for Markdown published at resources/views/vendor/mail/html
.
The email body is set on template files. It already has Laravel Localization to provide an internationalization feature to it.
You can check the default template at recources/views/templates/hello-world.blade.php
and
the password template at resruoces\views\templates\password.blade.php
for reference, as they've been written
with localization already.
You can define for how long a token will be valid by declaring the constant TOKEN_TIME
(in minutes) in your .env file. Default is 60.
You can define the default email template declaring DEFAULT_TEMPLATE
, where the default is hello-world
and the default language by
declaring the LANGUAGE
constant (default en
).
Deploying
Although the queue work command is handy and makes it really easy to consume the queue while testing the application, but it's extremely recommended to use Supervisor when deploying to production.
Laravel has a nice guide to properly configure the Supervisor.
Serverless deployment to AWS Lambda
This project uses Bref framework to empower a smoothly deployment to AWS Lambda.
It has a tiny list of dependencies to achieve that:
- Node.js 14.0+
- Serverless framework
- and, an AWS account (key and secret).
To install the Serverless framework:
Install Serverless plugin dependency:
And them, setup the AWS credentials for the Serverless framework:
You can change the project name on file serverless.yml
to match your project name,
right on the first line:
Last step before the actual deployment is to clear local cashed files:
and, them:
Once the process is finished, you will be prompted with the endpoint, functions and jobs created on AWS Lambda!
Contributing
Please, see CONTRIBUTING for details.
License
The BSD License (BSD). Please see License File for more information/
Web development has never been so fun!
All versions of laravel-mail-api with dependencies
bref/bref Version ^2.1
bref/laravel-bridge Version ^2.2
guzzlehttp/guzzle Version ^7.2
laravel/framework Version ^10.0
laravel/sanctum Version ^3.2
laravel/tinker Version ^2.8
predis/predis Version ^2.2