Expected response code 220 but got code "" in Laravel SMTP Configuration
When you encounter a Swift_TransportException stating "Expected response code 220 but got code """, your application is attempting to initiate an SMTP conversation, but the server refused to respond with the standard greeting acknowledgement. In the context of Laravel, this specific error usually points to a misconfiguration in how the mail driver interacts with the remote SMTP server, or a fundamental issue with the credentials provided.
Looking at the configuration snippet provided, the setup explicitly defines 'driver' => 'sendmail' while simultaneously specifying an SMTP host (smtp.gmail.com) and port. This is the primary source of the failure. The sendmail driver does not communicate via the SMTP protocol; instead, it invokes the local system's mail transfer agent (MTA), such as Postfix or Exim, which then handles delivery. Because your configuration forces Laravel to use the system's sendmail binary while expecting SMTP handshake responses from Gmail, the transport layer fails immediately when it cannot establish a direct TCP connection to smtp.gmail.com.
To resolve this, you must align the driver setting with the intended protocol. If you are authenticating with a username and password against an external provider like Gmail, you should switch the driver to 'smtp'. This change instructs Laravel's Swift Mailer component to open a direct socket connection to the host specified in the host configuration line.
Here is the corrected configuration block:
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'timeout' => 5,
'local_domain' => env('mail.local_domain', gethostname()),
'encryption' => 'tls',
'username' => 'myUsername',
'password' => 'password', // Ensure this is a secure environment variable in production
'sendmail' => '/usr/sbin/sendmail -bs', // Remove this line if using the smtp driver
'pretend' => false,
There are two critical adjustments made here. First, the 'sendmail' option must be removed or commented out. The sendmail binary is irrelevant when using the SMTP driver; keeping it can sometimes confuse older versions of Swift Mailer or cause unexpected behavior depending on how the transport layer initializes. Second, ensure you are removing any conflicting settings if your environment relies on an MTA like Postfix. If you are running Laravel on a shared hosting environment or a standard server without a dedicated MTA, relying on sendmail will fail unless that specific system binary is correctly configured and accessible.
Beyond the driver mismatch, consider the security implications of using smtp.gmail.com with a static password. Modern Google accounts require Application-Specific Passwords (ASPs) if two-factor authentication (2FA) is enabled. If you are entering your primary account password into the configuration file, Gmail's SMTP server will reject the connection, often resulting in a 535 Authentication failure rather than a handshake error, but it is worth verifying that the credentials are compatible with the current Google security requirements.
If you require complex routing or need to leverage an existing Postfix installation on your server, the sendmail driver is appropriate, but the configuration must be clean. In that scenario, you would remove the host, port, and encryption keys, as those belong exclusively to the SMTP protocol. The error message "Expected response code 220" specifically indicates the client sent an EHLO command (or similar greeting) and waited for a server response. When using the wrong driver, no EHLO is sent, or the connection logic is entirely different, leading to that empty string response in the exception.
For production applications, it is highly recommended to move sensitive credentials out of the config/mail.php file directly. Instead, define these values in your .env file and reference them using the env() helper. This practice prevents credential leakage during deployments and allows you to toggle between different mail services without modifying code files. Additionally, verifying that port 587 is open on the firewall for the server where Laravel is hosted is essential; if the network path is blocked, the connection will timeout before a valid response can be received.
By correcting the driver to smtp, cleaning up the conflicting sendmail directive, and ensuring your credentials are secure, the transport exception should resolve, allowing Laravel to successfully hand off the email message.
