如何使用laravel和gmail发送电子邮件邀请

时间:2016-11-10 16:48:16

标签: php laravel-5 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 个答案:

答案 0 :(得分:1)

您可以使用Laravel Mail Class发送邮件,

您的控制器应如下所示:

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('me@gmail.com', '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
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

php artisan config:cache文件中进行更改后,不要忘记运行.env。希望这有帮助!

另外请记得像这样配置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' => 'me@example.com', 'name' => 'Your name'],

如果您需要更多帮助,请在上面配置此文件 - see this