doulou1989 2016-11-10 16:48
浏览 79
已采纳

如何使用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?

  • 写回答

1条回答 默认 最新

  • doukang7501 2016-11-10 18:08
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)