Mail::send
Send the mail
Description
public boolean Mail::send( mixed $to, [ string $from_name = null, string $from_ad = null ] )
Sends the email with its attachments and returns true on success. A failed send (including an invalid address) throws PHPMailer\PHPMailer\Exception.
Without a sender address ($from_ad is null or ''), the sender of $settings['mail'] is used (see Sender). When none is set there either, send() throws \InvalidArgumentException.
When the debug flag is enabled with Mail::setDebugFlag, the email is not sent: the whole message is written to the log file with Mail::log instead.
note
Each call sends only to the addresses given to it, so one Mail object can send the same message to several people in turn. To send one email to several recipients, pass them as an array.
Arguments
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $to | mixed | Recipient address, or an array of addresses | |
| $from_name | string | null | Sender name (none: $settings['mail']['from_name'] when $from_ad is not given either) |
| $from_ad | string | null | Sender address, also used as the envelope sender (none: $settings['mail']['from']) |
Example
Controller
use Fegg\Tool\Mail;
use PHPMailer\PHPMailer\Exception;
try {
$mail = new Mail('Mail subject', 'Mail body');
$mail->send('mail@example.com', 'John Doe', 'from@example.com');
// Several recipients
$mail = new Mail('Mail subject', 'Mail body');
$mail->send(array('foo@example.com', 'bar@example.com'), 'John Doe', 'from@example.com');
// The sender of $settings['mail']
$mail = new Mail('Mail subject', 'Mail body');
$mail->send('mail@example.com');
} catch (Exception $e) {
// Failed to send: $e->getMessage()
}