带有g套房和XOAUTH2的Laravel邮件

Dei*_*orm 5 php email gmail laravel-5

我有与我的电子邮件相关联的套件帐户和应用程序.我正在查看Laravel邮件功能,但我没有看到使用xoauth auth类型登录到gmail smtp的任何选项.

我使用PHPMailer与codeigniter,我不得不使用clientId,clientSecret和refreshToken通过smtp.gmail.com发送电子邮件

我有没有机会使用xoauth与本地laravel swiftmailer进行身份验证?

Kar*_*ski 1

由于 Laravel 没有可用的配置来设置 AuthMode,因此我们需要对其进行一些调整。

  1. 在以下位置注册新的邮件服务提供商config/app.php

    // ...
    'providers' => [
        // ...
    
        // Illuminate\Mail\MailServiceProvider::class,
        App\MyMailer\MyMailServiceProvider::class,
    
        // ...
    
    Run Code Online (Sandbox Code Playgroud)
  2. app/MyMailer/MyMailServiceProvider.php应该创建你自己的TransportManager类:

````

namespace App\MyMailer;

class MyMailServiceProvider extends \Illuminate\Mail\MailServiceProvider
{
    public function registerSwiftTransport()
    {
        $this->app['swift.transport'] = $this->app->share(function ($app) {

            return new MyTransportManager($app);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

````

  1. 在 中,app/MyMailer/MyTransportManager.php我们可以提供额外的配置SwiftMailer

````

<?php

namespace App\MyMailer;


class MyTransportManager extends \Illuminate\Mail\TransportManager
{
    /**
     * Create an instance of the SMTP Swift Transport driver.
     *
     * @return \Swift_SmtpTransport
     */
    protected function createSmtpDriver()
    {
        $transport = parent::createSmtpDriver();
        $config = $this->app->make('config')->get('mail');


        if (isset($config['authmode'])) {
            $transport->setAuthMode($config['authmode']);
        }

        return $transport;
    }
}
Run Code Online (Sandbox Code Playgroud)

````

  1. 最后要做的事情是提供邮件配置并authmode设置XOAUTH2password您的访问令牌:

````

<?php

return array(

/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "mail", "sendmail"
|
*/

'driver' => 'smtp',

/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Postmark mail service, which will provide reliable delivery.
|
*/

'host' => 'smtp.gmail.com',

/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to delivery e-mails to
| users of your application. Like the host we have set this value to
| stay compatible with the Postmark e-mail application by default.
|
*/

'port' => 587,

/*
|--------------------------------------------------------------------------
| 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' => array('address' => 'user@gmail.com', 'name' => 'user'),

/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/

'encryption' => 'tls',

/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/

'username' => 'user@gmail.com',

/*
|--------------------------------------------------------------------------
| SMTP Server Password
|--------------------------------------------------------------------------
|
| Here you may set the password required by your SMTP server to send out
| messages from your application. This will be given to the server on
| connection so that the application will be able to send messages.
|
*/

'password' => 'YOUR ACCESS TOKEN',

/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/

'sendmail' => '/usr/sbin/sendmail -bs',

/*
|--------------------------------------------------------------------------
| Mail "Pretend"
|--------------------------------------------------------------------------
|
| When this option is enabled, e-mail will not actually be sent over the
| web and will instead be written to your application's logs files so
| you may inspect the message. This is great for local development.
|
*/

'pretend' => false,

'authmode' => 'XOAUTH2',

);
Run Code Online (Sandbox Code Playgroud)

````