Mutahhar's avatar
Level 11

How to use proxy IP for connecting with SendGrid smtp?

I need to use a proxy server to connect with SendGrid for sending emails. As of now, I have tried multiple approaches and solutions, but non of them is working. I have tried passing proxy parameter to stream.ssl in mail.php in Laravel version 10, but it is still not working. Please suggest a solution.

'stream' => [
    'ssl' => [
        'allow_self_signed' => true,
        'verify_peer' => false,
        'verify_peer_name' => false,
        'proxy'=> '192.111.111.111:3128',
    ],
],
0 likes
3 replies
LaryAI's avatar
Level 58

To use a proxy server for connecting with SendGrid SMTP in Laravel, you need to configure the stream options correctly in your mail.php configuration file. However, the proxy option you are trying to use is not a standard option for the stream context in PHP. Instead, you need to set up a proxy using the http or https context options.

Here is a step-by-step solution to achieve this:

  1. Install the php-proxy package: This package allows you to set up a proxy for your HTTP and HTTPS requests. You can install it via Composer:

    composer require amphp/http-client
    
  2. Configure the proxy in your mail.php configuration file: You need to set up the proxy settings in the stream context options. Here is an example configuration:

    return [
        'driver' => env('MAIL_DRIVER', 'smtp'),
        'host' => env('MAIL_HOST', 'smtp.sendgrid.net'),
        'port' => env('MAIL_PORT', 587),
        'from' => [
            'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
            'name' => env('MAIL_FROM_NAME', 'Example'),
        ],
        'encryption' => env('MAIL_ENCRYPTION', 'tls'),
        'username' => env('MAIL_USERNAME'),
        'password' => env('MAIL_PASSWORD'),
        'sendmail' => '/usr/sbin/sendmail -bs',
        'markdown' => [
            'theme' => 'default',
            'paths' => [
                resource_path('views/vendor/mail'),
            ],
        ],
        'stream' => [
            'ssl' => [
                'allow_self_signed' => true,
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
            'http' => [
                'proxy' => 'tcp://192.111.111.111:3128',
                'request_fulluri' => true,
            ],
        ],
    ];
    
  3. Set environment variables: Ensure you have the necessary environment variables set in your .env file:

    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.sendgrid.net
    MAIL_PORT=587
    MAIL_USERNAME=your_sendgrid_username
    MAIL_PASSWORD=your_sendgrid_password
    MAIL_ENCRYPTION=tls
    [email protected]
    MAIL_FROM_NAME=Your Name
    
  4. Test the configuration: Send a test email to ensure that the proxy settings are correctly applied and that you can connect to SendGrid via the proxy server.

By following these steps, you should be able to configure Laravel to use a proxy server for connecting with SendGrid SMTP. If you encounter any issues, make sure to check the proxy server settings and ensure that the proxy server is up and running.

Estinvosh's avatar

I’d switch to a SOCKS proxy and pass it through the transport stream options in mail.php using stream_context_options, since normal HTTP proxies won’t work with SMTP.

imranbru's avatar

use http, useragent, to make it more safe

Please or to participate in this conversation.