Search code examples
symfonyemailproxysmtpmailer

How to add a proxy to a symfony mailer?


I'm using symfony. I want to send mail. On dev mode, I'm using a specific SMTP server which is working fine, but on production: I have an SMTP server which is not with auth, and require a proxy to use.

My code is:

$mailer->send((new Email())->from("[email protected]")
            ->to("[email protected]")
            ->subject("Subject")
            ->html("Here is my content"));

In my .env I have the value of MAILER_DSN which is like smtp://from%40my-email.com:@smtp.my-email.com:25.

I get this error:

Expected response code "220" but got code "554", with message "554 5.7.1 <unknown[1.2.3.4]>: Client host rejected: Access denied".

I searched how to add a proxy, but symfony documentation is only about the reverse proxy for thing like cloudflare client-side (here I want something server side).

How can I add a proxy to fix it?


Solution

  • Haven't done it myself, but I'm guessing the following way:

    1. Use the symfony/http-client with config like default or scoping client:
    #framework.yaml
    framework:
      http_client:
        proxy:
          host: YOUR_HOST
          port: YOUR_PORT
    
    1. Create your own Transport instance with DSN like smtp-proxy and setup proxy-client:
    # in __constructor of custom Transport
    
    $httpClient = new NativeHttpClient();
    $this->setClient($httpClient);
    /*
    #And other settings if needed:
    $this
        ->setUsername('your_username')
        ->setPassword('your_password')
        ->setStreamOptions([
            'ssl' => [
                'allow_self_signed' => true,
                'verify_peer' => false,
            ],
        ])
        ->setLocalDomain('[127.0.0.1]')
    */
    
    1. Set MAILER_DSN in the [dev] .env like smtp-proxy://...

    So, the code like $mailer->send((new Email())... will not change, just Transport instance with proxy http-client will be used inside it.