Php : Mail() et authentification SMTP

La fonction Mail() par défaut de php n'est pas très riche et ne permet d'envoyer des mails par SMTP qu'en utilisant un relais ouvert. Pour y remédier, il suffit d'installer la version PEAR de cette dernière. apk add est à remplacer par apt-get install si vous utilisez une distribution du type debian.

Installation et configuration de PEAR

$ apk add php-pear


1.  renvoie :

Configuration File (php.ini) Path: /etc/php/cli-php5 Loaded
Configuration File: /etc/php/cli-php5/php.ini ## C'est la bonne ligne.
Scan for additional .ini files in: /etc/php/cli-php5/ext-active
Additional .ini files parsed: /etc/php/cli-php5/ext-active/php_gtk2.ini,
/etc/php/cli-php5/ext-active/xdebug.ini

php -c /etc/php/cli-php5/php.ini -r 'echo get_include_path()."n";'

Utilisation de PEAR:Mail()

Exemple de configuration avec Authentification :

<?php require_once "Mail.php";

$from = "Sandra Sender "; $to = "Ramona Recipient "; $subject = "Hi!"; $body = "Hi,nnHow are you?";

$host = "mail.example.com"; $username = "smtp_username"; $password = "smtp_password";

$headers = array ('From' => $from,

'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) { echo("

" . $mail->getMessage() . "

");

} else { echo("

Message successfully sent!

");

} ?>