elhajouji5/php-laravel-mailer

php-laravel-mailer

Downloads

4

Stars

0

Version

PHP/Laravel Mailer

This package allows sending email to a list of contacts based on queue/job Laravel built-in APIs, it inculdes the following options:

Send to a list of contact
Wait a delay between sending each email
Send email notification when sending has started/finished

Guide

  • Requirements
    • PHP >= 7.1
    • Laravel >= 5.5
    • Redis
    • SMTP or API to send email through
    • Laravel/horizon
    • A connected database to your project

Installation & configuration

type the following commands respecting the same order as bellow:

composer require laravel/horizon
composer require elhajouji5/php-laravel-mailer
php artisan vendor:publish

type the number between brackets that matches Devinweb\phpLaravelMailer\MailerServiceProvider and hit enter
E.g:

  [1 ] Provider: Devinweb\phpLaravelMailer\MailerServiceProvider

and again

php artisan vendor:publish

type the number between brackets that matches Laravel\Horizon\HorizonServiceProvider hit enter
Then

php artisan migrate

Make sure the 3 tables failed_jobs, jobs and subscribers have migrated

Feeding database (Optional)

Add contacts to subscribers table (People you want to send them emails in the future)
This is the schema of subscribers table:

    +------------+------------------+------+-----+---------+----------------+
    | Field      | Type             | Null | Key | Default | Extra          |
    +------------+------------------+------+-----+---------+----------------+
    | id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
    | name       | varchar(255)     | NO   |     | NULL    |                |
    | email      | varchar(255)     | NO   | UNI | NULL    |                |
    | created_at | timestamp        | YES  |     | NULL    |                |
    | updated_at | timestamp        | YES  |     | NULL    |                |
    +------------+------------------+------+-----+---------+----------------+

You can feed (insert records) your subscribers table as explained bellow:
Concerning ORM standard
use this namespace whereever you want it inside your controllers:

    use Elhajouji5\phpLaravelMailer\Models\Subscriber;

Fill the table in the regular way

$subscriber        = new Subscriber();
$subscriber->name  = "Somestring here";
$subscriber->email = "[email protected]";
$subscriber->save();

// Or 

Subscriber::create([
    "name"  => "Somestring here",
    "email" => "[email protected]",
]);

Browse and manage (edit, delete ... ) your subscribers

// browse all
$subscribers = Subscriber::latest()->get();

// Find by $id
$subscriberX        = Subscriber::find($id);
$subscriberX->delete();

// Total number of subscribers
$numberOfSubscribers = Subscriber::count();

..
..
..

// Or without including the namespace 

$subscribers = \DB::select("SELECT * FROM subscribers");
var_dump($subscribers);

Output:

    array:2 [
        0 => {
            "id": 3
            "name": "Somestring here"
            "email": "[email protected]"
            "created_at": "2018-03-23 14:11:07"
            "updated_at": "2018-03-23 14:11:07"
        }
        1 => {
            "id": 4
            "name": "Somestring here"
            "email": "[email protected]"
            "created_at": "2018-03-23 14:11:25"
            "updated_at": "2018-03-23 14:11:25"
        }
        ...
        ...
        ...
    ]



Setting up the local machine

  • Make this changes to .env file

    It's highly recommended to use redis driver for better performance
      - QUEUE_DRIVER=redis
    

    Your Redis server credentials(Below are the default credentials)

      - REDIS_HOST=127.0.0.1
      - REDIS_PASSWORD=null
      - REDIS_PORT=6379
    

    Your SMTP credentials(Example)

      - MAIL_DRIVER=smtp
      - MAIL_HOST=smtp.mailtrap.io
      - MAIL_PORT=2525
      - MAIL_USERNAME=null
      - MAIL_PASSWORD=null
      - MAIL_ENCRYPTION=null
    

How to use it

now your new service supposed to be set up and ready to use
you can try it by sending a get request to the following route:

[host]/send/{delay}/{sender name}/{sender email address}/{tag}

host: your domain
delay: seconds to wait between sending each email
sender name: the from name will appear in the header of the email
sender email address the email address will appear in the header of the email
tag: a simple string to distinguish your campaigns

Example:

http://dev.something.local/send/10/RESAL/[email protected]/holiday

Once you've sent the request, run this command at the root of your project

php artisan horizon

You can watch the process of that job from this link

[host]/horizon    

E.g: http://dev.something.local/horizon

For a deeper understanding of horizon package visit this link




Please feel free to share your opinion to improve this service ...

elhajouji5

Author

elhajouji5