laravel框架中Email邮件配置

时间:2024-03-29 15:37:02
.在composer.json加入下面一行代码
"guzzlehttp/guzzle": "~4.0"
.发送邮件的Route
Route::get('/mail',['as'=>'emails.index','uses'=>'EmailController@index']);
.项目/app/mail.php
<?php
return array(
    'driver' => 'smtp',
    'host' => 'smtp.163.com',
    'port' => , 或 'port'=>,
    'from' => array('address' => 'admin@163.com', 'name' => 'admin'),
    'encryption' => 'tls', 或 'encryption' => 'ssl', /*ssl 和端口994配用 */
    'username' => 'admin@163.com',
    'password' => 'password',
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,
);
.发送邮件的类
在控制器文件夹下创建EmailController.php
<?php
public function index()
    {
        //$data = ['name'=>'wlian'];
        $data = array(
            'name'  => 'admin',
            'tell'  => '',
            'mail'  => 'admin@163.com'
            );
            Mail::send('emails.index',$data,function($message){
                $message->to('qhorse@163.com','wlian')->subject('hello word!!');
                $message->attach(public_path().'/img/banner1.jpg');
            });
        return '已发送';
        return View::make('emails.index')->with('data',$data);
    };
.显示调用数据的视图
emails/index.blade.php
<p>{{ $name }}</p>
<p>{{ $mail }}</p>
<p>{{ $tell }}</p>