2016-11-10 171 views
0

我需要从我的应用向新用户发送电子邮件邀请。我不知道如何管理控制器?这是我的刀片视图如何向laravel和gmail发送电子邮件邀请函

@extends('layouts.app') 

<!-- Main Content --> 
@section('content') 
<div class="container"> 
    <div class="row"> 
     <div class="col-md-8 col-md-offset-2"> 
      <div class="panel panel-default"> 
       <div class="panel-heading">Send E-Mail Invitation</div> 
       <div class="panel-body"> 
        @if (session('status')) 
         <div class="alert alert-success"> 
          {{ session('status') }} 
         </div> 
        @endif 

        <form class="form-horizontal" role="form" method="POST" action="{{ url('/password/email') }}"> 
         {{ csrf_field() }} 

         <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> 
          <label for="email" class="col-md-4 control-label">E-Mail Address</label> 

          <div class="col-md-6"> 
           <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}"> 

           @if ($errors->has('email')) 
            <span class="help-block"> 
             <strong>{{ $errors->first('email') }}</strong> 
            </span> 
           @endif 
          </div> 
         </div> 

         <div class="form-group"> 
          <div class="col-md-6 col-md-offset-4"> 
           <button type="submit" class="btn btn-primary"> 
            <i class="fa fa-btn fa-envelope"></i> Send Invitation Link 
           </button> 
          </div> 
         </div> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 
@endsection 

我只需输入一个电子邮件地址并发送邀请邮件。我已将gmail配置为我的电子邮件客户端。你可以帮我吗?

回答

1

可以使用Laravel邮件类这样发送邮件,

你的控制器看起来应该是这样:

use Mail; 

class EmailsController { 
public function send(Request $request) 
{ 
    $email = $request->get('email'); 

    Mail::send('emails.send', ['email' => $email], function ($message) use ($email) 
    { 
     $message->from('[email protected]', 'Your Name'); 
     $message->to($email); 
    }); 

    return response()->json(['message' => 'Invitation Email Sent!']); 
} 
} 

你的观点应该是在目录 - resources/views/emails/send.php

<html> 
    <head></head> 
    <body style="background: black; color: white"> 
    <h1>Email Invitation</h1> 
    <p>Hello - {{$email}}</p> 
    <p>....</p> 
    </body> 
</html> 

注:记得配置你的.env文件的邮件:

MAIL_DRIVER=smtp 
MAIL_HOST=smtp.gmail.com 
MAIL_PORT=587 
[email protected] 
MAIL_PASSWORD=apppassword 
MAIL_ENCRYPTION=tls 

在对文件.env进行更改后,请不要忘记运行php artisan config:cache。希望这可以帮助!

还记得配置mail.php文件ioside config/mail.php这样的:

/* 
|-------------------------------------------------------------------------- 
| 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' => '[email protected]', 'name' => 'Your name'], 

配置上面这个文件,如果你想要更多的帮助,然后 - see this

+0

媒体链接我已经配置我与Gmail .ENV。那我应该再配置一次吗? – Fernando

+0

不,如果你已经配置它,你很好去... –

+0

好的谢谢,我需要一些关于此电子邮件发送路线的想法。这是应该是资源/意见/电子邮件/ send.blade.php – Fernando

相关问题