请问如何让laravel通过http代理来发送邮件

laravel(版本5.5)部署在内网,服务器(win10-xmapp-php7.2)只能通过特定的http代理访问外网。
我想用mailgun来发送邮件,请问如何让laravel通过代理连接mailgun从而发送邮件?
还请大佬给下方向

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

哈哈 :grin:,找到方法了,开心!

解决过程:

首先感谢《说一说 Laravel 邮件发送流程》

作者 coding01

链接:juejin.im/post/5b82715de51d4538d63...


从文章里找到配置 mailgun 的代码位置,在TransportManager类的createMailgunDriver()方法中

    /**
     * Create an instance of the Mailgun Swift Transport driver.
     *
     * @return \Illuminate\Mail\Transport\MailgunTransport
     */
    protected function createMailgunDriver()
    {
        $config = $this->app['config']->get('services.mailgun', []);

        return new MailgunTransport(
            $this->guzzle($config),
            $config['secret'], $config['domain']
        );
    }

其中的$this->guzzle($config)估计是 guzzle 相关的东西,而且还传入了配置,继续找到代码如下

    /**
     * Get a fresh Guzzle HTTP client instance.
     *
     * @param  array  $config
     * @return \GuzzleHttp\Client
     */
    protected function guzzle($config)
    {
        return new HttpClient(Arr::add(
            $config['guzzle'] ?? [], 'connect_timeout', 60
        ));
    }

再继续找HttpClient,代码如下

class Client implements ClientInterface, \Psr\Http\Client\ClientInterface
{
    use ClientTrait;

    /**
     * @var array Default request options
     */
    private $config;

    /**
     * Clients accept an array of constructor parameters.
     *
     * Here's an example of creating a client using a base_uri and an array of
     * default request options to apply to each request:
     *
     *     $client = new Client([
     *         'base_uri'        => 'http://www.foo.com/1.0/',
     *         'timeout'         => 0,
     *         'allow_redirects' => false,
     *         'proxy'           => '192.168.16.1:10'
     *     ]);
     *
    ...
}

找到啦!

new HttpClient()的时候可以传入$config['guzzle']设置 proxy。

这个$config在前面的createMailgunDriver()方法里,是$config = $this->app['config']->get('services.mailgun', []);


解决方法:

config/services.php文件中给mailgun增加guzzleproxy配置,如下

    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
        'guzzle' => [
            'proxy' => env('MAILGUN_PROXY'),
        ],
    ],

然后在.env文件中设置好MAILGUN_PROXY就行了。

MAILGUN_PROXY=address:port

ps. 如果需要的话,别忘了运行php artisan config:cache

4年前 评论
讨论数量: 3
leo

在 .env 文件里加上 HTTP_PROXY=xx.xxx.xx:8080

4年前 评论

@leo 我按你说的这个设置了代理,但是不起作用。还是直连的,没走代理服务器。请问还有别的方法吗?

4年前 评论

哈哈 :grin:,找到方法了,开心!

解决过程:

首先感谢《说一说 Laravel 邮件发送流程》

作者 coding01

链接:juejin.im/post/5b82715de51d4538d63...


从文章里找到配置 mailgun 的代码位置,在TransportManager类的createMailgunDriver()方法中

    /**
     * Create an instance of the Mailgun Swift Transport driver.
     *
     * @return \Illuminate\Mail\Transport\MailgunTransport
     */
    protected function createMailgunDriver()
    {
        $config = $this->app['config']->get('services.mailgun', []);

        return new MailgunTransport(
            $this->guzzle($config),
            $config['secret'], $config['domain']
        );
    }

其中的$this->guzzle($config)估计是 guzzle 相关的东西,而且还传入了配置,继续找到代码如下

    /**
     * Get a fresh Guzzle HTTP client instance.
     *
     * @param  array  $config
     * @return \GuzzleHttp\Client
     */
    protected function guzzle($config)
    {
        return new HttpClient(Arr::add(
            $config['guzzle'] ?? [], 'connect_timeout', 60
        ));
    }

再继续找HttpClient,代码如下

class Client implements ClientInterface, \Psr\Http\Client\ClientInterface
{
    use ClientTrait;

    /**
     * @var array Default request options
     */
    private $config;

    /**
     * Clients accept an array of constructor parameters.
     *
     * Here's an example of creating a client using a base_uri and an array of
     * default request options to apply to each request:
     *
     *     $client = new Client([
     *         'base_uri'        => 'http://www.foo.com/1.0/',
     *         'timeout'         => 0,
     *         'allow_redirects' => false,
     *         'proxy'           => '192.168.16.1:10'
     *     ]);
     *
    ...
}

找到啦!

new HttpClient()的时候可以传入$config['guzzle']设置 proxy。

这个$config在前面的createMailgunDriver()方法里,是$config = $this->app['config']->get('services.mailgun', []);


解决方法:

config/services.php文件中给mailgun增加guzzleproxy配置,如下

    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
        'guzzle' => [
            'proxy' => env('MAILGUN_PROXY'),
        ],
    ],

然后在.env文件中设置好MAILGUN_PROXY就行了。

MAILGUN_PROXY=address:port

ps. 如果需要的话,别忘了运行php artisan config:cache

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!