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

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

问题描述:

I need send an email invitation from My app to new users. I have no any idea about how to manage controller? this is My blade view

@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

I need simply enter an email address and send invitation mail. I have configure gmail as My email client. can you help me?

You can send Mails using Laravel Mail Class like this,

Your controller should look like this:

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!']);
}
}

Your view should be in directory - 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>

Note: Remember to configure your .env file for mails:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

Don't forget to run php artisan config:cache after you make changes in your .env file. Hope this helps!

Also remember to configure your mail.php file ioside config/mail.php like this:

/*
|--------------------------------------------------------------------------
| 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'],

Configure this file above, if you want more help then - see this