W3docs

php.ini & SMTP= - how do you pass username & password

To pass a username and password to the SMTP server in the php.ini file, you can use the following format:

PHP does not support passing SMTP credentials directly in the php.ini file. The built-in mail() function also lacks native SMTP authentication, as it relies on the system's local mail transfer agent (MTA).

Configuring SMTP settings in php.ini The php.ini file does not have username or password directives for SMTP authentication. Instead, you configure the local MTA path and options using the sendmail_path directive:

sendmail_path = /usr/sbin/sendmail -t -i

If you need to connect to an external SMTP server with authentication, you must configure it at the MTA level or use a PHP library.

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

Keep in mind that changes to php.ini are read when PHP starts up, so you will need to restart the web server for them to take effect.

Passing arguments to the mail() function The fifth parameter of the mail() function accepts additional command-line arguments for the local MTA. It does not support SMTP authentication credentials. The -f option sets the envelope sender address:

$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

mail($to, $subject, $message, $headers, '[email protected]');

For SMTP authentication, modern PHP applications should use a dedicated library such as PHPMailer or Symfony Mailer, which handle secure connections and credentials natively.