Mail::factory() (Previous) (Next) RFC822 - Introduction

View this page in Last updated: Mon, 02 Jul 2007
English | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Plain HTML

Mail::send()

Mail::send() -- sends a mail

Beschrijving

Sends a mail. The send()-function is provided by the object returned from factory()

Parameter

  • mixed $recipients - an array or a string with comma separated recipients.

  • array $headers - an associative array of headers. The header name is used as key and the header value as value.

  • string $body - the body of the email.

Return waarde

boolean - TRUE or a PEAR_Error object, if fails

Throws

Tabel 48-1. Mogelijke PEAR_Error waarden

Mailer driverError codeError messageReasonSolution
sendmailNULL "No from address given." The $headers array requires at least a from entry. Add a From header:

<?php
$headers['From'] = 'mymail@example.com';
?>

sendmailNULL "From address specified with dangerous characters." The from entry in the $headers array contains one ore more characters which could be non-RFC compliant Check the given from address for characters like: spaces or ; or & or ` (backtick)
sendmailNULL "sendmail [path to sendmail] not executable" The path to sendmail program is not correct. No sendmail executable found there. Check the $param['sendmail_path'] entry in your Mail::factory() call. If you use another mailer then sendmail, ie. qmail, check installation of the mailer. Normally it should includes a sendmail wrapper.
sendmailNULL "sendmail returned error code code" Sendmail returns a error, which must be handled by use. See the documention of your mailer programm.
smtpPEAR_MAIL_SMTP_ERROR_CREATE "Failed to create a Net_SMTP object" Failure in class creation Reinstall/update the Net_SMTP package.
smtpPEAR_MAIL_SMTP_ERROR_CONNECT "Failed to connect to host:port" Connect to SMTP server failed Check $param['port'] and $param['host'] entries in your Mail::factory() call.
smtpPEAR_MAIL_SMTP_ERROR_AUTH "method authentication failure" Authentication failed Check $param['auth'], $param['username'] and $param['password'] entries in your Mail::factory() call. Ensure to use the correct authentication method for the SMTP server.
smtpPEAR_MAIL_SMTP_ERROR_FROM "No From: address has been provided" The $headers array requires at least a from entry. Add a From header:

<?php
$headers['From'] = 'mymail@example.com';
?>

smtpPEAR_MAIL_SMTP_ERROR_SENDER "Failed to set sender: from" Setting the sender address failed Check the RFC-compliances of the sender address and the server connnectivity.
smtpPEAR_MAIL_SMTP_ERROR_RECIPIENT "Failed to add recipient: recipient " Sending of recipient address failed Check the RFC-compliances of the recipient address and the server connnectivity.
smtpPEAR_MAIL_SMTP_ERROR_DATA "Failed to send data" Body of the mail message could not send Check the RFC-compliances of the message body and the server connnectivity.

Note

Deze functie kan niet statisch worden aangeroepen.

Voorbeeld


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

$recipients = 'joe@example.com';

$headers['From']    = 'richard@example.com';
$headers['To']      = 'joe@example.com';
$headers['Subject'] = 'Test message';

$body = 'Test message';

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

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory('sendmail', $params);

$mail_object->send($recipients, $headers, $body);
?>

Mail::factory() (Previous) (Next) RFC822 - Introduction

Download Documentation Last updated: Mon, 02 Jul 2007
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
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.