Mail::factory() – létrehoz egy mailer példányt


require_once 'Mail.php';

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


Létrehoz egy példányt a megadott backend-specifikus mailer osztályból.


  • string $backend - a backend neve "mail","smtp", "sendmail"

  • array $params - egy tömb backend-specifikus paraméterekkel.

    Paraméterek a különböző backend-ekhez:

    • mail

      • Ha a safe mode kikapcsolt állapotban van, a $params paraméter a PHP mail() függvényének ötödik paraméterként kerül átadásra. Ha a $params paraméter egy tömb, az elemei szóközökkel tagolt sztringgé lesznek egyesítve.

    • sendmail

      • $params["sendmail_path"] - A sendmail program elérési útvonala a fájlrendszerben. Alapértelmezett értéke /usr/bin/sendmail

      • $params["sendmail_args"] - További átadandó paraméterek a sendmail programnak.

    • smtp

      • $params["host"] - A csatlakozásra kijelölt szerver. Alapértelmezett értéke localhost

      • $params["port"] - A csatlakozás portja. Alapértelmezett értéke 25

      • $params["auth"] - SMTP authentikáció használatának engedélyezése. Alapértelmezett értéke FALSE

      • $params["username"] - Felhasználónév az SMTP authentikációhoz.

      • $params["password"] - Jelszó az SMTP authentikációhoz.

      • $params["persist"] - Beállítja, hogy az SMTP authentikáció érvényben maradjon-e a send() metódus egymás utáni meghívásai között.

Return value

object - a megadott Mail példány vagy egy PEAR_Error objektum hiba esetén


Possible PEAR_Error values
Hibakód Hibaüzenet Ok Megoldás
NULL "Unable to find class for driver xxx" A mailer backend osztály nem található. Ellenőrizzük a $backend paramétert, amennyiben helyes, telepítsük újra és/vagy frissítsük a Mail csomagot.


This function should be called statically.

Az e-mail-küldési folyamat és a levélküldő backend-ek (Previous) E-mail elküldése (Next)
Last updated: Sun, 29 Nov 2015 — 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:
There should be an option to set the SSL context options (eg: verify_peer, verify_pear_name, cafile, etc).

As of PHP 5.6, the verify_peer options is set to true:

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!