Skip to content

Instantly share code, notes, and snippets.

@moradi-morteza
Last active December 26, 2020 07:45
Show Gist options
  • Save moradi-morteza/0952d78196b56aa4943f61ad20ef2a85 to your computer and use it in GitHub Desktop.
Save moradi-morteza/0952d78196b56aa4943f61ad20ef2a85 to your computer and use it in GitHub Desktop.
[email] #LaravelT
// important in localhost :
MAIL_USERNAME=bambboapp@gmail.com
MAIL_PASSWORD=112859112859
MAIL_ENCRYPTION=tls
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_FROM_ADDRESS=bambboapp@gmail.com
MAIL_FROM_NAME=bambboapp
// in cpanel mail server
MAIL_USERNAME=admin@bambboapp.com
MAIL_PASSWORD=Parsweb1399
MAIL_ENCRYPTION=ssl
MAIL_DRIVER=mail
MAIL_HOST=mail.bambboapp.com
MAIL_PORT=465
MAIL_FROM_ADDRESS=admin@bambboapp.com
MAIL_FROM_NAME=bambboapp
//use html template for email
// after insert config data
// port 587 gmail is TLS -> so you should MAIL_ENCRYPTION=tls
$date=['message'=>'hello','user_name'=>'morteza']; // this data send to view
// first parameter is view it can be html or text that should be defined in view in resources folder
'text'=>'mail.textmail'
'html'=>'mail.htmlmail'
\Mail::send(['text'=>'mail.test],$data,function(Message $message){
$message->to('moradiemails@gmail.com','morteza moradi')
->from('hienglishapp@gmail.com,'hienglish') // you can set MAIL_FROM_ADDRESS ,MAIL_FROM_NAME in env and disable this line
->subject('this is example email');
});
// view
<h1>Important Email</h1>
hello {{$user_name}}
your message is :
{{$message}}
//Advance Code with Class---------------------------------------
php artisan make:mail TestMail
\Mail::send(new TestMail('ali')); // do not need to set from beacuse we set it in env
class TestMail extends Mailable
{
use Queueable, SerializesModels;
public $user_name; // it should be public not private
public function __construct($user_name)
{
$this->user_name=$user_name;
}
public function build()
{
return $this->view('mails.testmail',['user_name'=>$this->user_name])
->subject('test mail')
->replyTo('your email','name')
->attach(storage_path('app/public/test1.txt')) // its important to use currect address // ->attach(public_path('app/public/test.txt'))
->attach(storage_path('app/public/test2.txt'))
->to('target email','his name'); // also -with(['date'=>'10-10-2019']); for send to view
}
}
// add image to view email
<h1>hello</h1>
// message is that message in line 13
<img src="{{$message->embed(storage_path('image/test.jpg'))}}"/>
// show email without send
// you can show your email with a route
return new TestMail('ali');
// More effective
$user = User::find(2); //or
$user = auth()->user;
\Mail::send(new TestMail($user));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment