Mail::factory() – Crée une instance du mailer


require_once 'Mail.php';

object &factory ( string $backend , array $params = array() )


Crée une instance de la classe spécifique au back-end d'envoi de mail choisi.


  • string $backend - nom du back-end "mail" ,"smtp" , "sendmail"

  • array $params - un tableau de paramètres spécifiques au back-end choisi.

    Liste des paramètres pour le back-ends.

    • mail

      • Si le safe-mode est désactivé, $params sera passé en tant que cinquième argument à la fonction mail() de PHP. Si $params est un tableau, ses éléments seront linéarisés en utilisant un espace en tant que délimiteur.

    • sendmail

      • $params["sendmail_path"] - L'emplacement du programme sendmail dans le système de fichier. Par defaut c'est /usr/bin/sendmail .

      • $params["sendmail_args"] - Paramètres supplémentaires à transmettre au programme sendmail. Par défaut, -i .

    • smtp

      • $params["host"] - Serveur auquel se connecter. Par défaut : localhost .

      • $params["port"] - Port de connexion. Par défaut : 25 .

      • $params["auth"] - Faut-il ou pas utiliser l'authentification SMTP. Par défaut : FALSE.

      • $params["username"] - Le nom d'utilisateur à communiquer pour l'authentification SMTP.

      • $params["password"] - Le mot de passe à communiquer pour l'authentification SMTP.

      • $params["persist"] - Indique si la connection SMTP doit rester ouverte pour plusieurs appels de la méthode send().

      • $params["localhost"] - La valeur à donner lors de l'envoi de EHLO ou HELO. Par défaut vaut localhost .

      • $params["timeout"] - Le délai maximal de connexion SMTP. Par défaut vaut NULL (aucun délai maximal).

      • $params["verp"] - Si l'on doit utiliser ou non VERP Par défaut vaut FALSE.

      • $params["debug"] - Si l'on doit utiliser le mode de débogage SMTP ou non. Par défaut vaut FALSE.

      • $params["persist"] - Indique si la connexion SMTP doit être persistente ou non lors de multiples appels à la méthode send().

Return value

object - une instance de la classe Mail spécifique ou un objet de classe PEAR_Error, en cas d'echec.


Possible PEAR_Error values
Code d'erreur Message d'erreur Cause Solution
NULL " Unable to find class for driver xxx " La classe du back-end d'envoi de mail n'a pas été trouvée. Vérifiez les paramètres de $backend . Si tout est correct, réinstallez ou mettez à jour le module mail.


This function should be called statically.

Comment envoyer un email et les back-ends d'envoi de mails (Previous) envoyer un mail (Next)
Last updated: Sun, 29 Aug 2010 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

Note by:
I was having trouble with XVERP being added in the SMTP connection even though $params["verp"] was FALSE.

The fix is to NOT set $params["verp"] at all, either unset it or just don't specify it in your $params.

$params["verp"] = FALSE; // comment out this line if you see an XVERP error
$params["debug"] = TRUE;
$smtp = Mail::factory('smtp', $params);

Calling $smtp->send() gives the following debugging:
DEBUG: Recv: 555 5.5.4 Unsupported option: XVERP

PEAR::isError($mail) outputs:
Failed to set sender: [SMTP: Invalid response code received from server (code: 555, response: 5.5.4 Unsupported option: XVERP)]
Note by:
If you are using 'mail' as your backend and you want to set your Return-Path you can do it like:

$mail_object =& Mail::factory('mail', '-f') ;

Then later if you need to change it you can

$mail_object->_params = '-f' ;

This is useful if you use a function to send your email since you can't create a new reference to the factory inside a function. But keep in mind that your new _params will be used for all subsequent calls to your $mail_object. So you may want to manually set them every time you send an email.
Note by:
If you are using the smtp backend and an SMTP server that requires authentication and you want to use a method other than PLAIN or LOGIN (such as CRAM-MD5) you'll need to make sue you've got Pear's Auth_SASL package installed as well, as it isn't made obvious from the error messages that Mail returns why it's failing!
Note by:
Set headers for UTF-8:
$headers['Content-Type'] = "text/plain; charset=\"UTF-8\"";
$headers['Content-Transfer-Encoding'] = "8bit";
Note by:
Still can't figure out how to send proper UTF 8 emsils.... any ideas?
Note by:
the persist value should be TRUE or FALSE. it's not clear on the page.

if you are getting the error:
Failed to connect to [SMTP: Invalid response code received from server (code: 421, response: 4.7.0 Error: too many connections from your_ip)]

try to get this option to TRUE.
Note by: Joe
There is hard-coded -f option in Mail_sendmail->send so it is impossible to set it using $params['sendmail_args']. It is hard-coded to -f$from.

In my case it caused 'X-Authentication-Warning' in mail headers.
Note by:
When sending mail via smtp be careful with the $params["auth"] option, the docs say it set it either to true or false, but in my case this didn't work and only when I set it to the actual authentication method of the server it worked. In my case I had to set it to:

$params["auth"] = "PLAIN";

This little problem had me going around in circles for 2 weeks!