草庐IT

php - Laravel 邮件与 g 套件和 XOAUTH2

coder 2024-04-07 原文

我有一个与我的电子邮件关联的 g 套件帐户和应用程序。我正在查看 Laravel 邮件功能,但我没有看到任何使用 xoauth 身份验证类型登录 gmail smtp 的选项。

我在 codeigniter 中使用 PHPMailer,我必须使用 clientId、clientSecret 和 refreshToken 通过 smtp.gmail.com 发送电子邮件

我有机会使用 xoauth 和原生 laravel swiftmailer 进行身份验证吗?

最佳答案

由于 Laravel 没有可用的配置来设置 AuthMode,所以我们需要稍微调整一下。

  1. config/app.php 中注册一个新的邮件服务提供者:

    // ...
    'providers' => [
        // ...
    
        // Illuminate\Mail\MailServiceProvider::class,
        App\MyMailer\MyMailServiceProvider::class,
    
        // ...
    
  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);
        });
    }
}

```

  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;
    }
}

```

  1. 最后要做的是提供邮件配置,将 authmode 设置为 XOAUTH2 并将 password 设置为您的访问 token :

```

<?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',

);

```

关于php - Laravel 邮件与 g 套件和 XOAUTH2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49434397/

有关php - Laravel 邮件与 g 套件和 XOAUTH2的更多相关文章

  1. ruby-on-rails - 验证电子邮件地址是 Paypal 用户 - 2

    我想验证一个电子邮件地址是否是PayPal用户。是否有API调用来执行此操作?是否有执行此操作的ruby​​库?谢谢 最佳答案 GetVerifiedStatus来自PayPal'sAdaptiveAccounts平台会为您做这件事。PayPal没有任何codesamples或SDKs用于Ruby中的自适应帐户,但我确实找到了编写codeforGetVerifiedStatusinRuby的人.您需要更改该代码以检查他们拥有的帐户类型的唯一更改是更改if@xml['accountStatus']!=nilaccount_status

  2. ruby-on-rails - Ruby on Rails - 需要在每周的特定时间将消息发送到电子邮件 - 2

    我想知道我应该如何着手这个项目。我需要每周向人们发送一次电子邮件。但是,这必须在每周的特定时间自动生成并发送。编码有多难?我需要知道是否有任何书籍可以提供帮助,或者你们中的任何人是否可以指导我。它必须使用ruby​​onrails进行编程。因此有一个网络服务和数据库集成。干杯 最佳答案 为什么这么复杂?您只需安排工作。您可以使用Delayed::Job例如。Delayed::Job让您可以使用run_at符号在特定时间安排作业,如下所示:Delayed::Job.enqueue(SendEmailJob.new(...),:run_

  3. ruby - 如何在 watir 测试套件结束时关闭浏览器? - 2

    使用ruby​​的watir测试网络应用程序时,浏览器最后会保持打开状态。网上的一些建议是,要进行真正的单元测试,您应该在每次测试时(在拆卸调用中)打开和关闭浏览器,但这很慢而且毫无意义。或者他们做这样的事情:defself.suites=superdefs.afterClass#Closebrowserenddefs.run(*args)superafterClassendsend但这会导致摘要输出不再显示(诸如“100次测试、100次断言、0次失败、0次错误”之类的内容仍应显示)。我怎样才能让ruby​​或watir在我的测试结束时关闭浏览器? 最佳答案

  4. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  5. ruby - 使用ruby mail gem获取电子邮件正文而不获取附件 - 2

    我正在尝试使用IMAP和ruby​​mailgem获取gmail的正文。当我按照anotherstackoverflowanswer.中的描述获取RFC822字段时,它工作得很好.Fiedl很好地描述了这种方法answer类似的问题。这种方法很棒,只是它需要获取RFC822,而RFC822也会获取所有附件。是否有任何其他领域或其他方法我可以采取不获取附件但仍然使用ruby​​mailgem来获得解码良好的正文? 最佳答案 您必须解析并理解返回的BODYSTRUCTURE响应的实际结构,请参阅RFC3501,p.56.还要记住应用相关

  6. ruby - 使用 S/MIME 在 Ruby 中对电子邮件进行数字签名 - 2

    Ruby中是否有一种方法可以使用S/MIME对电子邮件消息进行数字签名?我们的团队使用PKI,我们的用户习惯于期待重要消息的数字签名。我知道我可以调用openssl命令行工具:opensslsmime-sign-signer$CERT_FILE-passinpass:$CERT_PASS-in$UNSIGNED_MAIL-out$SIGNED_MAIL-certfile$CERT_CA_FILE-from'your'-to'recipients'-subject'TheSubject'但我希望利用Ruby解决方案。 最佳答案 我最终

  7. ruby-on-rails - 带有内联附件的 Rails 邮件程序错误 - 2

    我在ActionMailer中有一个非常罕见的行为,我在5个月前实现了一个邮件程序操作并且它工作正常,但昨天,由于一些奇怪的原因,它崩溃了。问题我有一个邮件布局,为了在我所有的电子邮件中使用它,我在其中渲染了一张之前由前置过滤器附加的图像Layout=app/views/layouts/email.html.erbVisionamos>......用户邮件程序=app/mailers/users.rbclassUsuariosMailer"monitoreo@visionamos.com"layout"mail"#Setemaillayoutbefore_filter:add_inli

  8. ruby-on-rails - 发生异常时发送电子邮件不起作用,使用 exception_notification - 2

    我正在从rails2.3迁移到rails3.1,我试图在生成异常时发送电子邮件。我正在使用exception_notificationgem。我的其余电子邮件都在工作。但是异常邮件不会被解雇。以下是我的staging.rb文件中的设置。config.action_mailer.perform_deliveries=trueconfig.action_mailer.raise_delivery_errors=true下面是application.rb中的代码C::Application.config.middleware.useExceptionNotification::Rack,:e

  9. ruby - 使用 gmail gem 跟踪一些电子邮件 - 2

    我正在使用gmailgem发送电子邮件,我需要跟踪这些电子邮件。我该怎么做?我正在尝试搜索带有message_id的电子邮件,但它会从我的收件箱中提取所有电子邮件,而我只想要特定电子邮件的回复。这是我的实际代码:*使用message_id保存电子邮件*mail=gmail.deliver(email)Email.create(:message_id=>mail.message_id,:from=>user.email,:to=>annotation.to,:body=>annotation.content,:title=>annotation.title,:annotation=>an

  10. ruby-on-rails - Rails actionmailer,在特定时间发送邮件 - 2

    我有一个带有actionmailer的Rails应用程序,可以向客户发送提醒。所以我想在特定的日期时间发送电子邮件。我该怎么做,或者来自Controller的任何操作? 最佳答案 您可以通过使用某种后台任务来实现此目标,例如resque或delayed_job例如。此外,像whenever这样的gem(Ruby中的Cron作业)将帮助您实现目标!看看thistutorial它显示通过ActiveJob使用后台处理器发送电子邮件和delayed_job更新要在特定日期时间发送电子邮件,您可以使用deliver_later使用wait_

随机推荐