How to configure php.ini to use Gmail as mail server
When attempting to send emails from a local development environment like WAMP using PHP's mail() function, developers often encounter configuration hurdles that stem from a fundamental misunderstanding of how SMTP protocols interact with modern email providers. A common scenario involves trying to route mail through Google's servers while retaining the default sendmail path in your configuration.
The specific error you are encounteringâwhere the system attempts to connect on port 25 instead of the required secure portâis a classic symptom of incomplete SMTP protocol configuration. Port 25 is the standard for unencrypted Simple Mail Transfer Protocol (SMTP), which Google explicitly blocks for incoming connections from clients that do not use authentication or encryption. When you set SMTP = smtp.gmail.com in your php.ini, you are telling PHP to attempt a direct connection, but if the port remains at 25 or if the protocol isn't strictly defined as SMTP with SSL/TLS support, the handshake fails immediately.
To resolve this, you must explicitly define both the host and the correct port within your php.ini file. In your example, changing SMTP to ssl:smtp.gmail.com is a good start, but it is often insufficient on its own depending on the PHP version and how the mail extension is compiled. The most robust configuration involves setting the following parameters in the [mail function] section of your php.ini:
; http://php.net/smtp
SMTP = smtp.gmail.com
smtp_port = 465
sendmail_from = myemail@gmail.com
Here, port 465 is the standard for SMTP over SSL. If you prefer using STARTTLS (which starts on port 25 and then upgrades to TLS), you must ensure your firewall allows inbound traffic on port 25, but Google often requires a valid certificate or specific authentication headers that simple mail() functions struggle with without additional libraries. Port 465 is generally more reliable for basic PHP configurations because it wraps the entire session in SSL from the start.
It is also crucial to note that setting SMTP does not automatically override the internal behavior of the sendmail_path. In many PHP builds, especially on Windows, the sendmail_path might still be pointing to a local executable like sendmail.exe or /usr/sbin/sendmail. If this path exists and is set, PHP will ignore the SMTP setting entirely and attempt to use the specified port for the local sendmail binary. To ensure your Gmail settings take precedence, you should either clear the sendmail_path by setting it to an empty string:
sendmail_path =
or simply leave it unset if your PHP build defaults correctly on Windows. Once this is adjusted, restart WAMP Server to apply the changes.
Furthermore, configuring the application-level parameters in your Yii framework config file is a separate concern from the server configuration. Your adminEmail parameter is correct; however, ensure that the view or controller using it explicitly constructs the message headers using the configured SMTP settings. The error message you saw likely persists because the PHP runtime is still attempting to use the default local mail agent on port 25 before falling back to your SMTP settings.
Finally, remember that Gmail's own security policies require App Passwords if you are using two-factor authentication. Even with the correct php.ini settings, standard login credentials will fail if 2FA is enabled. If you encounter persistent connection timeouts or "connection refused" errors after adjusting the port and host, verify that your Windows Firewall rules specifically allow outbound connections to port 465 (or 25 for STARTTLS) and ensure that no antivirus software is intercepting the local PHP process's network requests.
For those moving beyond local development to production environments, consider migrating to a framework like Laravel, which offers robust built-in mail drivers including mail, smtp, and sendmail. The LaravelCompany resource can provide deeper insights into managing email queues and switching from the basic PHP mailer to more secure, queue-based delivery systems that handle authentication and retries automatically.