Sending Mail with Dynamic SMTP configuration
Firstly, create a table in database and create all fields according to SMTP settings(.env file).
Step:1 Create a Custom Mail Provider
1 |
php artisan make:provider MailConfigServiceProvider |
Step:2 Fetch data from database in app/Providers/MailConfigServiceProvider
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use DB; use Config; class MailConfigServiceProvider extends ServiceProvider { public function register() { $mail = DB::table('mail')->first(); if ($mail) { $config = array( 'driver' => $mail->driver, 'host' => $mail->host, 'port' => $mail->port, 'from' => array('address' => $mail->from_address, 'name' => $mail->from_name), 'encryption' => $mail->encryption, 'username' => $mail->username, 'password' => $mail->password, 'sendmail' => '/usr/sbin/sendmail -bs', 'pretend' => false, ); Config::set('mail', $config); } } public function boot() { // } } |
Step:3 Make routes in web.php
1 2 3 4 5 |
Route::get('change-email-settings','MailController@viewForm'); Route::post('email-settings','MailController@create')->name('createConfiguration'); Route::get('send-mail','MailController@sendMail'); |
Step:4 Now create a controller
1 |
php artisan make:controller MailController |
Step:5 Write your code in app/http/Controller/MailController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; use Config; use Mail; class MailController extends Controller { public function viewForm() { return view('mail.add'); } public function create(Request $request) { $insert = $request->all(); unset($insert['_token']); unset($insert['btn']); DB::table('mail')->insert($insert); return view('mail.add'); } public function sendMail() { $content = "<html>"; $content .= "<head>"; $content .= "<title>Test Template</title>"; $content .= "</head>"; $content .= "<body>"; $content .= "<p>This is Test Template</p>"; $content .= "</body>"; $content .= "</html>"; $mailTo = "FROM_MAIL_ADDRESS"; Mail::send(array(),array(), function($message) use ($content,$mailTo) { $message->to($mailTo) ->subject('Test Dynamic SMTP Config') ->from(Config::get('mail.from.address'),Config::get('mail.from.name')) ->setBody($content, 'text/html'); echo 'Mail Sent Successfully'; }); } } |
Step: 6 Now create a blade file,name it according to your convenience
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<!DOCTYPE html> <html> <head> <title>Change Email Configuration</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <div class="row"> <div class="col-lg-6 m-auto"> <h2 class="ml-5">Change Email Configurations</h2> <form method="POST" action="{{ route('createConfiguration') }}"> @csrf <label>Driver:</label> <input type="text" name="driver" class="form-control"><br> <label>Host:</label> <input type="text" name="host" class="form-control"><br> <label>Port:</label> <input type="text" name="port" class="form-control"><br> <label>From Address:</label> <input type="text" name="from_address" class="form-control"><br> <label>From Name:</label> <input type="text" name="from_name" class="form-control"><br> <label>Encryption:</label> <input type="text" name="encryption" class="form-control"><br> <label>Username:</label> <input type="text" name="username" class="form-control"><br> <label>Password:</label> <input type="password" name="password" class="form-control"><br> <input type="submit" name="btn" class="btn btn-primary"><br><br> </form> </div> </div> </div> </body> </html> |
Hit a url as shown below to fill form and add new SMTP details
1 |
localhost:8000/change-email-settings |
Hit a url as shown below to send mail
1 |
localhost:8000/send-mail |
0 Comments
Share