Friday, July 3, 2015

Email templat engine_laravel 5


this is the way end mail with Email Template Engine .save templates in database and replace some fields and send mail frist confige your laravel email config file mail.php

return [

 'driver' => env('MAIL_DRIVER', 'smtp'),
 'host' => env('MAIL_HOST', ''),
 'port' => env('MAIL_PORT', 587),
 'from' => ['address' => '', 'name' => 'name'],
 'encryption' => 'tls',
 'username' => env('MAIL_USERNAME'),
 'password' => env('MAIL_PASSWORD'),
 'sendmail' => '/usr/sbin/sendmail -bs',
 'pretend' => false,

.env file

create data base & create email tempalte migration php artisan make:migration create_users_table upload migration file
class CreateEmailtemplateTable extends Migration {

  * Run the migrations.
  * @return void
 public function up()
        Schema::create('emailtemplate', function(Blueprint $table)
            $table->string('subject', 255);
            $table->enum('status', array('1', '0'));

  * Reverse the migrations.
  * @return void
 public function down()


OR you can upload sql file which on have in file in app\Http\Controllers.php add fuloowing function to accesss email sendig system throughout of you application IN CONTROLL.PHP
namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Mail;
use DB;
use Exception;

abstract class Controller extends BaseController {

 use DispatchesCommands, ValidatesRequests;

    public function sendemail($emil_template='',$mailsubject='',$email_variable='')

//get data record using tempalte name

        $gettemplate=  $this->mailtemplate($emil_template);

//      deploy deta stringobject array
        $all_detail = [] ;
        foreach ($gettemplate[0] as $kay=>$tempalte_detail)

//        get template description in to variable by channa

  //   send $description $description to VariableReplaceArray by channa
       $variable_replace_message= $this->VariableReplaceArray($email_variable,$description);

//     if controller subject is    get subject of email from default template

//get reciver name & email
        $receiver_name=$email_variable['{to_fname}'].' '.$email_variable['{to_lname}'];
//        echo '
'; // die(); Mail::send('email_tempate.user_template', ['mail_content' => $variable_replace_message], function($sendmail)use ($mailsubject,$receiver_name,$receiver_email) { $sendmail->to($receiver_email, $receiver_name); $sendmail->cc(''); $sendmail->subject($mailsubject); }); return ; } // get email template from data table public function mailtemplate($getname) { // get email template $gettemplate = DB::table('emailtemplate')->where('name',$getname)->get(); //trigger exception in a "try" block for email template try { if(empty($gettemplate)) { //If the exception is thrown, this text will not be shown echo 'this title not available in email template table '; } } //catch exception catch(Exception $e) { echo 'Message: ' .$e->getMessage(); } return $gettemplate; } //VariableReplaceArray by channa public function VariableReplaceArray($email_variable,$description) { $getvariableArray=array_keys($email_variable); $replacevariableArray=array_values($email_variable); $variable_repalsement= str_replace($getvariableArray,$replacevariableArray,$description); return $variable_repalsement; } }
this is add your controller fuction which you create for send email
public function index()

        $emil_template='user registration tempalate'; // template name  which need to send
        $mailsubject=""; //if user template default email subject kaeep this empty
        $message= 'this is the way end mail with Email Template Engine .save templates in database and replace some fields and send mail
        frist confige your laravel email config file mail.php';
            '{to_fname}'=>'channa',// receiver frist name
            '{to_lname}'=>'bandaara',//receiver last name
            '{to_email}'=>'', //receiver email
            '{link}'=>'',// link if you send
            '{message}'=>$message, // message
//        get this on sendemail function

$email_variable mean i define commen variable for repalce sender & reciever detail this method will easy becouse when we create template body like this (see sql file)
hello {to_fname} {to_lname} 

you have message


click this link {link}

thnank you
create folder resources\views email_template & create template layout file i make user.php this bring email body

HELLO GUYS channa make your work easy please comment see my blog for more
{!! $mail_content !!}
mail ot put

i hope this will help for advance develiopment 

get sample project from GIT-HUB

thank you


Post a Comment