使用Laravel 4.2和Mailgun发送邮件

时间:2017-05-20 16:13:27

标签: mailgun laravel-4.2

我对此有点新鲜。

我已经阅读了Laravel 4和一些Mailgun的文档,我测试了一些邮件并且工作但是只是在这样的路线:

Route::get('send_test_email', function(){
    Mail::send('emails.registro', array('key' => 'value'), function($message)
    {
        $message->subject('Bienvenido a la gran experiencia');
        $message->from(env('CONTACT_MAIL'), env('CONTACT_NAME'));
        $message->to('luis02lopez@hotmail.com');
    });
});

我在浏览器中访问了myapp / send_test_email并收到了一封电子邮件。

但现在我想在注册时发送电子邮件,我创建了路线:

Route::get('mail', ['uses' => 'MailController@send', 'as' => 'send']);

控制器:

<?php

class MailController extends \BaseController {

    public function index()
    {
        return View::make('signup');
    }

    public function send() {

        Mail::send('emails.registro', $data, function($message) use
           {

           $message->subject('Bienvenido a la gran experiencia');

           
           $message->from(env('CONTACT_MAIL'), env('CONTACT_NAME'));

           
           $message->to($user->email, $user->firstname);

           });
    }

并在注册表单中添加了一个表单:

    {{ Form::open(['route' => 'send', 'method' => 'get']) }}

    <div class="form-group">
        {{ Form::label('username', 'Usuario', ['class' => 'sr-only']) }}
        {{ Form::text('username', null, ['placeholder' => 'Usuario', 'required', 'minlength' => 6, 'class' => 'form-control', ]) }}
        @foreach($errors->get('username', '<span class=error>:message</span>') as $message)
            {{$message}}
        @endforeach
    </div>
    <div class="form-group">
        {{ Form::label('password', 'Contrase&ntilde;a', ['class' => 'sr-only']) }}
        {{ Form::password('password', ['placeholder' => 'Contrase&ntilde;a', 'required', 'minlength' => 8, 'class' => 'form-control']) }}
        @foreach($errors->get('password', '<span class=error>:message</span>') as $message)
            {{$message}}
        @endforeach
    </div>
    <div class="form-group">
        {{ Form::label('password_confirm', 'Confirmar Contrase&ntilde;a', ['class' => 'sr-only']) }}
        {{ Form::password('password_confirmation', ['placeholder' => 'Confirmar Contrase&ntilde;a', 'required', 'minlength' => 8, 'class' => 'form-control']) }}
        @foreach($errors->get('password_confirmation', '<span class=error>:message</span>') as $message)
            {{$message}}
        @endforeach
    </div>
    <div class="form-group">
        {{ Form::label('email', 'Email', ['class' => 'sr-only']) }}
        {{ Form::email('email', null, ['placeholder' => 'Email', 'required', 'class' => 'form-control']) }}
        @foreach($errors->get('email', '<span  class=error>:message</span>') as $message)
            {{$message}}
        @endforeach
    </div>
    <div class="form-group">
        {{ Form::label('firstname', 'Nombres', ['class' => 'sr-only']) }}
        {{ Form::text('firstname', null, ['placeholder' => 'Nombres', 'required', 'class' => 'form-control']) }}
    </div>
    <div class="form-group">
        {{ Form::label('lastname', 'Apellidos', ['class' => 'sr-only']) }}
        {{ Form::text('lastname', null, ['placeholder' => 'Apellidos', 'required', 'class' => 'form-control']) }}
    </div>

    <div class="form-group">
        {{ Form::submit('Registrar', ['class' => 'btn btn-lg btn-block btn-kinbu'])}}
    </div>
    {{ Form::close() }}

我在控制器中有一个Parse error: syntax error, unexpected 'Mail' (T_STRING),为什么?

1 个答案:

答案 0 :(得分:0)

我有错误:

    public function send() {

    Mail::send('emails.registro', $data, function($message) use
       {

       $message->subject('Bienvenido a la gran experiencia');


       $message->from(env('CONTACT_MAIL'), env('CONTACT_NAME'));


       $message->to($user->email, $user->firstname);

       });
}

我使用$ user var,但是我没有通过关闭&#34;用户&#34; 传递它,所以我必须这样做:

    public function send() {

    Mail::send('emails.registro', array('key' => 'value'), function($message) use ($user)
       {

       $message->subject('Bienvenido a la gran experiencia');


       $message->from(env('CONTACT_MAIL'), env('CONTACT_NAME'));


       $message->to($user->email, $user->firstname);

       });
}