to send mail via Sendgrid and SendinBlue APIs.
composer install
run the following command to start docker:
docker-compose up
docker services included: php
, nginx
, 'mysql, 'rabbitmq
to be able to send emails, we need to run the worker first. I used Laravel queue for this puprose.
$ php artisan queue:worker
send a post request to the following endpoint:
/mail/send
with the json content:
$curl -XPOST http://localhost:8000/mail/send --data '{"name":"NAME", "email":"example@example.com", "subject:"test email", "body":"test <b>email</b>", "isHtml":true}'
This project comes with an interactive command to send an email:
$ php artisan mail:send
>name
saeed
>email
example@example.com
> subject
test email
> body
test body
> is HTML? (yes/no)?
yes
> do you want to add it to the queue? (yes/no):
yes
if yes, the email will be added to queue, otherwise it sends the email right away with the Sendgrid provider.
run the following command to run the tests:
./vendor/bin/phpunit
- Inside the
app/Mail
directory there are some classes as follow: - Contracts: interfaces needed for the project
- Providers: all email providers can be found here. All of them must extends
Provider
or implememntEmailProvider
- Mailable.php: a simple php object that implemented
Contracts\Mailable
contains email information name, email, subject, etc. - Mailer.php: A class that receives a EmailProvider, and tries to send the email.
- SendQueuedMailable.php: This class receives a
MailerInterface
(Mailer.php
or any other class that implements theMailerInterface
), and a collection of providers (ProviderCollection
) and tries to send the email by each provider. If sending failes, it tries next provider. app/Jobs/MailSend
is a Laravel job that registers providers and callssend
method fromSendQueuedMailable
class.app/Collection/Collection.php
is a PHP Collection to simulate strict typed arrays (more info)