Mail::send()

Mail::send() – sends a mail

Synopsis

require_once 'Mail.php';

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

Description

Sends a mail. The send() method 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. If you want to override the envelope sender of the email, set the Return-Path header and that value will be used instead of the value of the From: header.

  • string $body - the body of the email.

Return value

boolean - TRUE or a PEAR_Error object on failure.

Throws

Possible PEAR_Error values
Mailer driver Error code Error message Reason Solution
sendmail NULL "No from address given." The $headers array requires at least a from entry. Add a From header:
<?php
$headers
['From'] = 'mymail@example.com';
?>
sendmail NULL "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).
sendmail NULL "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.
sendmail NULL "sendmail returned error code code" Sendmail returns a error, which must be handled by use. See the documention of your mailer program.
smtp PEAR_MAIL_SMTP_ERROR_CREATE "Failed to create a Net_SMTP object" Failure in class creation. Reinstall/update the Net_SMTP package.
smtp PEAR_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.
smtp PEAR_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.
smtp PEAR_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';
?>
smtp PEAR_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.
smtp PEAR_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.
smtp PEAR_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

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'] = '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);
?>
creates a mailer instance (Previous) email address validation (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: dineshgaru@gmail.com
Sir,

i'm below code which is not all working for my website....please help and guide the exact thing that i have to follow.


<?php


$host 
"localhost"
$username "hr@taconsulting.in"
$password "taconsulting2012"



$name $_REQUEST['name'] ;
$sex $_REQUEST['sex'] ;
$email $_REQUEST['email'] ;
$number $_REQUEST['number'] ;
$location $_REQUEST['location'] ;
$relocate $_REQUEST['relocate'] ;
$salary $_REQUEST['salary'] ;
$salary2 $_REQUEST['salary2'] ;
$resume $_REQUEST['resume'] ;
$comments $_REQUEST['comments'] ;




$from $email;
$to "hr@taconsulting.in";
$subject "Enquiry";



$body $body "Name: $name \nSex: $sex \nEmail: $email \nContactNumber: $number \nLocation: $location \nRelocate: $relocate \nCurrentSalary: $salary \nExpectedSalary: $salary2 \nResume: $resume \nComments: $comments \n";


$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp Mail::factory('smtp',
array (
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));

$mail $smtp->send($to$headers$body);

if (
PEAR::isError($mail)) {
?>

<p> <? echo $mail->getMessage();
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.