https://appdividend.com/2018/03/05/send-email-in-laravel-tutorial/
composer create-project laravel/laravel emailsend --prefer-dist
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=email address
MAIL_PASSWORD=pw of gmail
MAIL_ENCRYPTION=ssl
use mailtrap
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=c874cb52c3e7ef
MAIL_PASSWORD=e3f356dff81791
MAIL_ENCRYPTION=null
create maillable
php artisan make:mail SendMailable
SendMailable.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendMailable extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public $name;
public $subject;
public $docs;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($message,$subject,$docs)
{
$this->name = $message;
$this->subject = $subject;
$this->docs = $docs;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
if(!empty($this->docs))
{
return $this->attach($this->docs)->subject($this->subject)->view('email.name');
}
else
{
return $this->subject($this->subject)->view('email.name');
}
}
}
//purano
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;use Illuminate\Mail\Mailable;use Illuminate\Queue\SerializesModels;use Illuminate\Contracts\Queue\ShouldQueue;
class SendMailable extends Mailable{ use Queueable, SerializesModels;
/** * Create a new message instance. * * @return void */ public $name; public $subject;
/** * Create a new message instance. * * @return void */ public function __construct($message,$subject) { $this->name = $message; $this->subject = $subject; }
/** * Build the message. * * @return $this */ public function build() {
return $this->subject($this->subject)->view('email.name'); }}
//purano
Config -> Mail.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
| "sparkpost", "log", "array"
|
*/
'driver' => env('MAIL_DRIVER', 'smtp'),
/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/
'port' => env('MAIL_PORT', 587),
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Important Mail'),
],
/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/
'sendmail' => '/usr/sbin/sendmail -bs',
/*
|--------------------------------------------------------------------------
| Markdown Mail Settings
|--------------------------------------------------------------------------
|
| If you are using Markdown based email rendering, you may configure your
| theme and component paths here, allowing you to customize the design
| of the emails. Or, you may simply stick with the Laravel defaults!
|
*/
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
//purano
'from' => [ 'address' => env('MAIL_USERNAME', 'hello@example.com'),
'name' => env('MAIL_USERNAME', 'Example'),],
MailController
<?php
namespace App\Http\Controllers;
use App\Mail\SendMailable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function writemail()
{
return view('email.mailsend');
}
public function mail(Request $request)
{
$docs='';
if(!empty($request->file('image')))
{
$file=$request->file('image');
$path=base_path().'/public/document_upload';
$name=uniqid().'_'.$file->getClientOriginalName();
if($file->move($path,$name))
{
$docs=$name;
$docs=base_path().'\public\document_upload'.'\\'.$docs;
}
}
// dd($docs);
$name=$request->input('name');
$subject=$request->input('subject');
$to=$request->input('email');
$msg=$request->input('message');
$message=$name.','.$msg;
// dd($message);
// $name = 'Krunal';
Mail::to($to)->send(new SendMailable($message,$subject,$docs));
return 'Email was sent';
//after successfull send of email this will appear in screen,we can create
new blade view and return to there also like this:
return view('email.confirm',compact('name'));
}
}
//purano
<?php
namespace App\Http\Controllers;
use App\Mail\SendMailable;use Illuminate\Http\Request;use Illuminate\Support\Facades\Mail;
class MailController extends Controller{
public function writemail() { return view('email.mailsend');
} public function mail(Request $request)
{
$name=$request->input('name');
$subject=$request->input('subject');
$to=$request->input('email');
$msg=$request->input('message');
$message=$name.','.$msg;// dd($message);
// $name = 'Krunal';
Mail::to($to)->send(new SendMailable($message,$subject));
return 'Email was sent'; }}
name.blade.php (this is the page to format the message to be sent)
//from below sended message will be seen like this:
hi Mr./Mrs.,and sended message ......
<html>
<head>
</head>
<body>
<div>
Hi Mr./Mrs.,{{$name}}</div>
</body>
</html>
Mailsend.blade.php
@extends('layouts.back_master')
@section('content')
<form action="{{route('send.mail')}}" method="post" enctype="multipart/form-data" >
{{ csrf_field() }}
<div class="form-group">
<label>Subject:</label><input type="text" id="subject" name="subject" required>
</div>
<div class="form-group">
<label>NAme:</label><input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label>Email:</label><input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label>MEssage:</label><textarea id="message" name="message"></textarea>
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" name="image" id="image" class="form-control">
</div>
<input type="submit" value="Send MAil" class="btn btn-success">
</form>
@endsection
// Purano
<html>
<head>
</head>
<body>
<form action="{{route('send.mail')}}" method="post" enctype="multipart/form-data" >
{{ csrf_field() }} <label>Subject:</label><input type="text" id="subject" name="subject">
<label>NAme:</label><input type="text" id="name" name="name">
<label>Email:</label><input type="email" id="email" name="email">
<label>MEssage:</label><textarea id="message" name="message"></textarea>
<input type="submit" value="Send MAil" class="btn btn-success">
</form>
</body>
</html>
//purano
web.php
Auth::routes();Route::post('/send/email', 'MailController@mail')->name('send.mail');Route::get('/write/mail', 'MailController@writemail');//Route::get('/emails/name', 'MailController@writemail');
IMPORTANT
Suscribe and send mail :
public function subscription(Request $request)
{
// dd($request);
$email=$request->input('email');
$name=$myArray = explode('@', $email);
$name=$name[0];
$status=Subscription::create([
'email'=>$request->input('email'),
]);
if($status){
Session::flash('success','Thank You For Suggestion. Keep in Touch');
}else{
Session::flash('error','Information and message cannot be added.');
}
return $this->maildirect($email,$name);
// return view('frontend.lastpage');
}
public function maildirect($email,$name)
{
$docs='';
// $name=$request->input('name');
$subject="FEPB NEPAL";
$to=$email;
$msg="www.fepb.gov.np";
$message=$name.','.$msg;
Mail::to($to)->send(new SendMailable($message,$subject,$docs));
return back()->withErrors($name.' Thank you !!!');
// return redirect()->back()->withInput($name);
// return view('email.confirm',compact('name'));
}
Route file:
Route::post('subscription','MailController@subscription')->name('front.subscription');
Comments
Post a Comment