Mail::factory() – tworzy instancję mailera


require_once 'Mail.php';

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


Tworzy instancję klasy specyficznej dla wybranej metody wysy³ania.


  • string $backend - nazwa metody wysy³ania "mail","smtp", "sendmail"

  • array $params - tablica parametrów specyficznych dla danej metody wysy³ania.

    Lista parametrów dla metod

    • mail

      • Gdy wy³±czony jest tryb "safe mode", $params zostan± przekazane jako pi±ty parametr dla funkcji PHP mail(). Je¿eli $params jest tablic±, jej elementy zostan± po³±czone w ³añcuch tekstowy rozdzielone spacj±.

    • sendmail

      • $params["sendmail_path"] - Lokalizacja programu sendmail. Domy¶lna to /usr/bin/sendmail

      • $params["sendmail_args"] - Dodatkowe parametry do przekazania do sendmail. Domy¶lne to -i

    • smtp

      • $params["host"] - serwer z którym siê po³±czyæ. Domy¶lny to localhost

      • $params["port"] - port na który siê po³±czyæ. Domy¶lny to 25

      • $params["auth"] - czy u¿yæ uwierzytelnienia SMTP. Domy¶lna warto¶æ to FALSE

      • $params["username"] - nazwa u¿ytkownika do u¿ycia przy uwierzytelnieniu SMTP.

      • $params["password"] - has³o do u¿ycia przy uwierzytelnieniu SMTP.

      • $params["localhost"] - warto¶æ do przekazania przy wysy³aniu polecenia EHLO lub HELO. Domy¶lna to localhost

      • $params["timeout"] - maksymalny czas bezczynnego po³±czenia. Domy¶lny to NULL (brak)

      • $params["verp"] - czy u¿yæ VERP czy te¿ nie. Domy¶lna warto¶æ to FALSE

      • $params["debug"] - czy w³±czyæ debugowanie SMTP czy te¿ nie. Domy¶lna warto¶æ to FALSE

      • $params["persist"] - wskazuje czy po³±czenie SMTP powinno byæ nawi±zane ci±gle pomiêdzy wieloma wywo³aniami metody send().

Return value

object - specyficzna instancja klasy Mail lub obiekt PEAR_Error, przy niepowodzeniu


Possible PEAR_Error values
Kod b³êdu Wiadomo¶æ b³êdu Powód Rozwi±zanie
NULL "Unable to find class for driver xxx" Nie znaleziono klasy dla ¿±danego mailera. Sprawd¼ parametr $backend, je¿eli jest prawid³owy przeinstaluj i/lub zaktualizuj pakiet Mail.


This function should be called statically.

Jak oraz czym wysyłać emaile (Previous) wysyła email (Next)
Last updated: Sat, 19 Apr 2014 — 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!