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

标签 php laravel-5 gmail

我需要从“我的应用”向新用户发送电子邮件邀请。我不知道如何管理 Controller ?这是我的 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

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

最佳答案

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

您的 Controller 应如下所示:

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('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="620f0722050f030b0e4c010d0f" rel="noreferrer noopener nofollow">[email protected]</a>', 'Your Name');
        $message->to($email);
    });

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

您的 View 应该位于目录 - 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
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="125f535b5e4d474157405c535f572f7f6b777f737b7e52757f737b7e3c717d7f" rel="noreferrer noopener nofollow">[email protected]</a>
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

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

还记得在 ioside config/mail.php 中配置您的 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' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="14797154716c75796478713a777b79" rel="noreferrer noopener nofollow">[email protected]</a>', 'name' => 'Your name'],

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

关于php - 如何使用 laravel 和 gmail 发送电子邮件邀请,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40532917/

相关文章:

php - 使用 PHP 和 Mysql 取得了银行业的成就

python - 如何从电子邮件 Python 中删除多余的语法?

php - Laravel Controller 不存在

mysql - Laravel 5 查询计数和分组

php - 将 Laravel 集合附加到另一个集合

php - Laravel 5 - 检查邮件是否已发送

java - 使用javamail时,草稿gmail附件出现在垃圾文件夹中

javascript - Gmail 不在邮件 Laravel 上显示图片

php - 旋转 PNG,然后使用图像透明度重新保存

php - 使用 volley 和 php 在 android 中发布 utf8(波斯语或阿拉伯语)