previousMail::factory() (Previous) (Next) RFC822 - Introductionnext

View this page in Last updated: Sun, 18 Oct 2009
English | Brazilian Portuguese | Chinese | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Turkish

Mail::send()

Mail::send() – wysyła email

Synopsis

require_once 'Mail.php';

mixed send ( mixed $recipients , array $headers , string $body )

Description

Wysyła email. Metoda send() udostępniana jest przez obiekt zwrócony z metody factory()

Parameter

  • mixed $recipients - odbiorcy, w formie tablicy lub łañcucha tekstowego rozdzielonego przecinkami.

  • array $headers - asocjacyjna tablica nagłówków. Nazwa nagłówka stanowi klucz, natomiast wartością jest wartość nagłówka.

  • string $body - treść właściwa emaila.

Return value

boolean - TRUE lub obiekt PEAR_Error, przy niepowodzeniu

Throws

Possible PEAR_Error values
Nazwa mailera Kod błędu Wiadomość błędu Powód Rozwiązanie
sendmail NULL "No from address given." Tablica $headers wymaga conajmniej wpisu from . Dodaj nagłówek From:
<?php
$headers
['From'] = 'mymail@example.com';
?>
sendmail NULL "From address specified with dangerous characters." Wpis "from" w tablicy $headers zawiera jeden lub więcej znaków, które mogą być niezgodne z zaleceniami RFC Sprawdź wprowadzone adresy na znaki takie jak: spacje lub ; lub & lub ` (odwrotny apostrof)
sendmail NULL "sendmail [path to sendmail ] not executable" Niewłaściwa ścieżka do programu sendmail. Nie znaleziono tam pliku wykonywalnego sendmail. Sprawdź wpis $param['sendmail_path'] w wywołaniu Mail::factory(). Jeżeli używany jest inny mailer niż sendmail, np. qmail, sprawdź jego instalację. Zazwyczaj powinna ona zawierać nakładkę symulującą sendmail.
sendmail NULL "sendmail returned error code code " Sendmail zwrócił błąd, który musi zostać obsłużony. Zobacz do dokumentacji odpowiedniego mailera.
smtp PEAR_MAIL_SMTP_ERROR_CREATE "Failed to create a Net_SMTP object" Niepowodzenie przy tworzeniu instancji klasy Przeinstaluj/zaktualizuj pakiet Net_SMTP.
smtp PEAR_MAIL_SMTP_ERROR_CONNECT "Failed to connect to host:port " Połączenie z serwerem SMTP się nie powiodło Sprawdź wpisy $param['port'] oraz $param['host'] w wywołaniu Mail::factory().
smtp PEAR_MAIL_SMTP_ERROR_AUTH "method authentication failure" Uwierzytelnienie się nie powiodło Sprawdź wpisy $param['auth'] , $param['username'] oraz $param['password'] w wywołaniu Mail::factory(). Upewnij się iż używasz poprawnej metody uwierzytelnienia dla danego serwera SMTP.
smtp PEAR_MAIL_SMTP_ERROR_FROM "No From: address has been provided" Tablica $headers wymaga conajmniej wpisu from . Dodaj nagłówek From:
<?php
$headers
['From'] = 'mymail@example.com';
?>
smtp PEAR_MAIL_SMTP_ERROR_SENDER "Failed to set sender: from " Ustawienie adresu nadawcy się nie powiodło Sprawdź zgodność z zaleceniami RFC adresu nadawcy oraz połączenie SMTP.
smtp PEAR_MAIL_SMTP_ERROR_RECIPIENT "Failed to add recipient: recipient " Wysłanie adresu odbiorcy się nie powiodło Sprawdź zgodność z zaleceniami RFC adresu odbiorcy oraz połączenie SMTP.
smtp PEAR_MAIL_SMTP_ERROR_DATA "Failed to send data" Treść wiadomości email nie może zostać wysłana Sprawdź zgodność z zaleceniami RFC treści wiadomości oraz połączenie SMTP.

Note

This function can not be called statically.

Example

<?php
include('Mail.php');

$recipients 'joe@example.com';

$headers['From']    = 'richard@example.com';
$headers['To']      = 'joe@example.com';
$headers['Subject'] = 'Wiadomo&sacute;&cacute; testowa';

$body 'Wiadomo&sacute;&cacute; testowa';

$params['sendmail_path'] = '/usr/lib/sendmail';

// Utworzenie obiektu mail przy u&zdot;yciu metody Mail::factory
$mail_object =& Mail::factory('sendmail'$params);

$mail_object->send($recipients$headers$body);
?>
previousMail::factory() (Previous) (Next) RFC822 - Introductionnext

Download Documentation Last updated: Sun, 18 Oct 2009
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
Note by: undisclosed-recipients
If you are using smtp to send out the mail, and the To: field shows "undisclosed-recipients:", please remember to add

$headers['To'] = "Recipient <recipient@host.com>"

to your headers.
Note by: burak a.t toruko d.o.t com
Just a correction for my previous message.

These two lines:
$From = "From: ".$from_name." <fromaddress@domain.com>";
$To = "To: ".$to_name." <toaddress@domain.com>";

should read:
$From = $from_name." <fromaddress@domain.com>";
$To = $to_name." <toaddress@domain.com>";
Note by: burak a.t toruko d.o.t com
You can combine PEAR::Mail with mb_string functions to send valid Japanese(ISO-2022-JP) emails via SMTP connection:
<?php
    $from_name 
"&#40658;&#27810;&#12288;&#26126;"// Japanese name
    
$to_name "&#19977;&#33337;&#12288;&#25935;&#37070;"// Another Japanese name
    
$subject "&#26085;&#26412;&#35486;&#12513;&#12540;&#12523;&#12398;&#12486;&#12473;&#12488;"// Japanese subject
    
$mailmsg "&#12371;&#12428;&#12399;&#26085;&#26412;&#35486;&#12398;&#12513;&#12483;&#12475;&#12540;&#12472;&#12391;&#12377;&#12290;&#25991;&#23383;&#21270;&#12369;&#12398;&#12486;&#12473;&#12488;&#12434;&#34892;&#12356;&#12414;&#12377;&#12290;";
    
    
//Convert encodings of subject, sender, recipient and message body to ISO-20222-JP
    
$from_name mb_encode_mimeheader($from_name"ISO-2022-JP""Q");
    
$to_name mb_encode_mimeheader($to_name"ISO-2022-JP""Q");
    
$subject mb_encode_mimeheader($subject"ISO-2022-JP""Q");
    
$mailmsg mb_convert_encoding($mailmsg"ISO-2022-JP""AUTO");

    
$From "From: ".$from_name." <fromaddress@domain.com>";
    
$To "To: ".$to_name." <toaddress@domain.com>";

    
$recipients "toaddress@domain.com";
    
$headers["From"] = $From;
    
$headers["To"] = $To;
    
$headers["Subject"] = $subject;
    
$headers["Reply-To"] = "reply@address.com";
    
$headers["Content-Type"] = "text/plain; charset=ISO-2022-JP";
    
$headers["Return-path"] = "returnpath@address.com";
    
    
$smtpinfo["host"] = "smtp.server.com";
    
$smtpinfo["port"] = "25";
    
$smtpinfo["auth"] = true;
    
$smtpinfo["username"] = "smtp_user";
    
$smtpinfo["password"] = "smtp_password";

    
$mail_object =& Mail::factory("smtp"$smtpinfo);

    
$mail_object->send($recipients$headers$mailmsg);
?>

Tested the script and the E\email sent correctly to: Japanese Outlook Express, Outlook 2000/2003, Japanese Thunderbird, Becky, several Docomo mobiles, several Softbank mobiles.
Note by: David Lidstone
Warning... Depending (I'm guessing) on your SMTP MTA some emails can be sent without dates and some with, unless you specify a date in the header. For example, my _locally_ delivered mail was ending up at the bottom of people's inboxes because it had a date of 1970. External mail was fine (header added by MTA?).

Add this header to fix:
'Date' => date("r")
Note by: arminfrey@gmail.com
In order to send e-mail to cc or bcc with smtp you have to list the cc e-mail address both as a recipient (which decides where the e-mail is sent) and in the cc header, which tells the mail client how to display it.

Here is the code I use:

$to = 'to@example.com';
$cc = 'cc@example.com';

$recipients = $to.", ".$cc;

$headers['From'] = 'from@example.com';
$headers['To'] = $to;
$headers['Subject'] = 'Test message';
$headers['Cc'] = 'cc@example.com';
$headers['Reply-To'] = 'from@example.com';

$send = $mail->send($recipients, $headers, $body);
Note by: James Leckie
To handle errors when sending mail use the following. Great for checking if the SMTP server accepted all the addresses.

$send = $mail->send($to, $headers, $body);
if (PEAR::isError($send)) { print($send->getMessage());}
Note by: Mike Soh
It is important to note that the mail->send() function will wait for the socket to close. Your script may sit for a long time waiting for sendmail/mail/etc to exit.

You may prefer to store the mail information to be processed at a later time.