title: How to setup Nginx as smtp proxy to external smtp server?
body: <p>I am trying to setup nginx mail proxy Gmail smtp server. Nginx server runs on docker.</p>
  <p>During tests, I use <code>openssl s_client -connect mail.localhost.com:12465 -starttls smtp</code> (ports are different from config for port availability reasons).</p>
  <p>Openssl connects successfully and I manage to get EHLO and run AUTH LOGIN. Still, I receive error <code>502 5.5.1 Unrecognized command. u6sm12628547ejn.14 - gsmtp</code>. Same message appears when <code>less secure apps</code> security is on in account.</p>
  <p>Running command above directly to smpt.gmail.com works great, and error is displayed correctly if <code>less secure apps</code> security is not allowed.</p>
  <p>Current nginx.conf is below.</p>
  <pre><code>worker_processes 1;
  
  events {
      worker_connections 1024;
  }
  
  http {
      include mime.types;
      default_type application/octet-stream;
  
      sendfile on;
  
      keepalive_timeout 65;
  
      server {
          listen 9101;
          server_name localhost;
  
          location /smtpauthssl {
              if ($http_auth_user ~* &quot;(?&lt;=@)(\S+$)&quot; ) {
                  set $domain $1;
              }
              set $smtp_port '587';
              add_header Auth-Status &quot;OK&quot;;
              add_header Auth-Port $smtp_port;
              add_header Auth-Server &quot;108.177.119.108&quot;;
              return 200;
  
          }
          location = / {
              return 403;
          }
      }
  }
  
  mail {
      server_name mail.mydomain.com;
      auth_http localhost:9101/smtpauthssl;
  
      proxy_pass_error_message on;
      proxy_smtp_auth on;
      xclient on;
  
      ssl_certificate /etc/ssl/certs/certificate.pem;
      ssl_certificate_key /etc/ssl/certs/key.pem;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers HIGH:!aNULL:!MD5;
      ssl_session_cache shared:SSL:10m;
      ssl_session_timeout 10m;
  
      server {
          listen 587 ssl;
          protocol smtp;
          smtp_auth login plain cram-md5;
      }
  }
  
  </code></pre>
  <p>My assumption is that something with logon is not passed to Gmail SMTP, but not sure what.</p>
  <p>One more thing, what does Auth-Server header should contain, FQDN or IP address?</p>