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.
$ apk add php-pear
php.ini
:
$ php --ini
# 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";'
Exemple de configuration avec Authentification :
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <recipient@example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow 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("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>