【问题标题】:how to send email invitation with laravel and gmail如何使用 laravel 和 gmail 发送电子邮件邀请
【发布时间】:2016-11-10 16:48:15
【问题描述】:

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

@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 配置为我的电子邮件客户端。你能帮帮我吗?

【问题讨论】:

    标签: php laravel-5 gmail


    【解决方案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
    

    在对 .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' => 'me@example.com', 'name' => 'Your name'],
    

    在上面配置这个文件,如果你需要更多帮助,那么 - see this

    【讨论】:

    • 我已经用 gmail 配置了我的 .env。那我应该重新配置一下吗?
    • 好的,谢谢,我需要一些有关此电子邮件发送路线的信息。这应该是资源/视图/电子邮件/send.blade.php
    • 不,路由将是您在表单操作中提到的路径... --。 Route::post('send/activation/mail', 'EmailsController@send');
    • 我可以使用任何控制器发送此电子邮件吗?
    • 好的,然后我用 EmailsController 做了这个脚本,得到了这个错误,EmailsController.php 第 15 行中的 FatalErrorException: Class 'App\Http\Controllers\Mail' not found跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2017-01-16
    • 2018-03-18
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    相关资源
    最近更新 更多