2am.tech/laravel-mail-api

Laravel mail API microservice

1.0.1 2023-12-20 02:44 UTC

This package is auto-updated.

Last update: 2024-04-20 03:37:02 UTC


README

Laravel Mail API

Build Software License Codacy Badge Quality Codacy Badge Coverage

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

$ composer create-project 2am.tech/laravel-mail-api app

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:

$ docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v "$(pwd):/var/www/html" \
    -w /var/www/html \
    laravelsail/php82-composer:latest \
    composer install --ignore-platform-reqs
// to start up the container
$ ./vendor/bin/sail up -d

# to stop the container
$ ./vendor/bin/sail stop

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.

# register access token
...
'accessTokens' => [
    'access-key-user-1' => [
        'appKey' => 'jwYitJJOop2v',
        'appSecret' => 'token',
    ],
   ... 
];

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.

$ date -u +'%Y-%m-%dT%H:%M:%S.%3NZ'

# ouputs date format
$ 2023-08-10T04:32:25.620Z

Then, simple hash your AppKey with the gotten timestamp, using your AppSecret as the hash password.

signature: HASH-HMACK( AppKey + timestamp, AppSecret )

# with our sample values
signature: HASH-HMACK( 'jwYitJJOop2v' + '2023-08-10T04:32:25.620Z', 'token' )

# output
cb64bbccdff25dcaba24e4c029aa54d99522a3e2e70e5be7be1b48dd8816b4e05b0102f2a2775c895ac73f649b45f6f97f755a112a9f4a206e4053128fc5ada9

A command was created to help the signature creation, for testing purposes:

$ php artisan app:create-signature

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:

MAIL_MAILER=smtp
MAIL_HOST={smtp-server}
MAIL_PORT={smtp-port}
MAIL_USERNAME={smtp-mailer-email-address}
MAIL_PASSWORD={smtp-mailer-email-password{
MAIL_ENCRYPTION=tls

Usage

To serve the application, Laravel provides the handy built in command serve

$ php artisan serve

# or on Sail
$ ./vendor/bin/sail php artisan 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:

Authentication: Bearear {signature}
ts: {timestap (used to sign the appKey)}
accessToken: {your access token}

# using our sample values
Authentication: Bearear cb64bbccdff25dcaba24e4c029aa54d99522a3e2e70e5be7be1b48dd8816b4e05b0102f2a2775c895ac73f649b45f6f97f755a112a9f4a206e4053128fc5ada9
ts: 2023-08-10T04:32:25.620Z
accessToken: access-key-user-1

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:

curl --location 'http://localhost:8000/api/email/send' \
--header 'accessKey: tests' \
--header 'ts: 2023-08-10T04:56:50+00:00' \
--header 'Authorization: Bearer a7ef7032999db841c75a7a2de4d40106e6de19b7c269b722f81bc51a1f713f6642d25e3bc25af7b04f9a385ef8f9c9ed346e029d0262cbc61dde2f640d0f8c48' \
--form 'from="test@mail.com"' \
--form 'to="receiver@email.com"' \
--form 'sender="2am.tech"' \
--form 'receiver="Amigo OSS"' \
--form 'subject="test api"'

Done. Now your new message is on the queue, ready to be dispatched. To achieve that, you just need to run this command:

$ php artisan queue:work

# or on sail
$ ./vendor/bin/sail php artisan queue:work

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:

use Laravel\Sanctum\PersonalAccessToken;

// from this 
return [
    ...
    'attachmentsAllowedMimetypes' => env('ATTACHMENT_MIMETYPES', ['application/pdf', 'image/*']),
];

// to this
return [
    ...
    'attachmentsAllowedMimetypes' => env('ATTACHMENT_MIMETYPES', '*'),
]; 

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:

$ npm install -g serverless

Install Serverless plugin dependency:

$ npm install

And them, setup the AWS credentials for the Serverless framework:

$ serverless config credentials --provider aws --key "key" --secret "secret"

You can change the project name on file serverless.yml to match your project name, right on the first line:

service: laravel-mail-api

Last step before the actual deployment is to clear local cashed files:

$ php artisan config:clear

and, them:

$ serverless deploy

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/

2am.tech

Web development has never been so fun!

https://2am.tech/