In this article, I will show you how to create a Newsletter Package in Laravel and how to use this. You can check the source code from GitHub link: https://github.com/digitalcrm/pkg-newsletter and package in packagist.org link: https://packagist.org/packages/digitalcrm/newsletter

Creating a Package:

In this article, I have created a Newsletter package which will manage the newsletter in Laravel. So just follow the steps:
1. First, install fresh Laravel Project:
Run command in your terminal: Composer create-project –prefer-dist laravel/laravel PkgNewsletter “5.8.*”

2. Create a package folder in the root directory and under this create a subfolder newsletter, like this:

3. Next, for package, we have needed two main files: one composer.json and second is Service Provider file.
3.1. Creating composer.json file: Select package path in your terminal and run the composer init command which will create the composer.json file in your newsletter package but after running this command, this will ask various question like package name and vendor, description and author etc… So fill this information.

After completion of init command, you can see composer.json file is created under your package directory. So, open package/newsletter/composer.json file:

3.2. Creating Service Provider: After setup the composer.json file, then we have needed a Service Provider for your package, so create a “src” folder under package/newsletter/, like this:

Create NewsletterServiceProvider.php file under this package/newsletter/src/ directory and add code under boot() and register() function for load, publishing and registering the services of packages file.

4. Modify the composer.json file:
4.1. Open your package composer.json file and add require field, autoload psr-4 and package discovery in your package/newsletter/composer.json file, like this:

Open main composer.json file and add your package path in autoload-dev. Because this will tell your Laravel project that there is a package inside the Laravel project.

4.3. Open your terminal, select the project path and run the composer command: composer dump-autoload. This command will help to recognize the package.

5. Create files for newsletter package: All required files for packages will be created under the src folder. You can visit the GitHub directory for this source code: https://github.com/digitalcrm/pkg-newsletter/tree/master/src.

For this package, I have used the Mailchimp for mail services. To know more about Mailchimp visit https://mailchimp.com/. So, I have configured MailChimp in our package, like this;
src/config/newletter.php

src/Exception/InvalidNewList.php

src/NewsletterFacade.php

src/NewsletterList.php

src/NewsletterCollection.php

src/NullDriver.php

src/Newletter.php

Note: When using this newsletter package, you should have to integrate Mailchimp (MAILCHIMP_APIKEY and MAILCHIMP_LIST_ID) in your .env file. For getting the API key and List Id, you should have to create an account in Mailchimp first, where you will get your API Key and List Id.

Upload in packagist.org

Here with few steps, you can upload your package in packagist.org. Just follow the steps:

Step1:

First you need to open packagist.org and create your account using GitHub and sign in:

Step2:

After “sign in” in Packagist.org, then you have to upload your package in GitHub repository.

Step3:

After uploading the package in GitHub, copy the link address and go to the packagist.org then click on submit and paste the address link:

Step4:

Finally, after submitting package, your package has been uploaded and you will get your composer require command for this package.

How To Use fincrm/newsletter Package:

Here, I will discuss how to use this newsletter package. Just follow the steps given below:

Step1:

First download the Laravel project. Run this composer command:
composer create-project –prefer-dist laravel/laravel NewsLetterPackage

Step2:

After downloading the Laravel project, next run this composer command:
composer require fincrm/newsletter. This command will download the newsletter package in your Laravel project.

Step3:

Publishing the configuration file run:
php artisan vendor:publish –provider=“Fincrm\Newsletter\NewsletterServiceProvider”
This command will publish the config file to config/newsletter.php.

Step4:

Next, we need view, controller and routes for running the newsletter package. So let’s create a view, controller and routes for this newsletter package.
View file: Create view file under resources/views/subscriber.blade.php and add code:

Controller: For creating controller run the command:
php artisan make:controller NewsLetterController –r.

This command will create a resource controller under app/Http/Controllers/NewsLetterController.php. Then open this file and add code:

Finally create routes under routes/web.php.

Step5:

Open .env file and integrate MailChimp API_KEY and LIST_ID. You get your own MailChimp API key and List ID from mailchimp.com.

Step6:

Navigate the URL: http://127.0.0.1:8000/subs in your browser.
• Newsletter page:

• After Submitting

• Open your mail account and you will get subscription page, like this