composer require initphp/mailerRequires PHP 8.1+ with the mbstring, iconv and fileinfo extensions.
A mailer is created with an optional configuration array. With no argument it
defaults to the native mail() transport and a UTF-8, plain-text message.
use InitPHP\Mailer\Mailer;
$mailer = Mailer::newInstance(); // or: new Mailer();newInstance() and new Mailer() are equivalent; newInstance() simply reads
better in a fluent chain.
Every set* method returns the mailer, so calls chain. send() returns nothing
and throws on failure.
use InitPHP\Mailer\Exception\MailerException;
try {
$mailer->setFrom('you@example.com', 'Your Name')
->setTo('recipient@example.com')
->setSubject('Hello from InitPHP Mailer')
->setMessage('This is the body of the message.')
->send();
} catch (MailerException $e) {
echo 'Could not send: ' . $e->getMessage();
}By default send() clears the per-message state (recipients, subject, body,
headers) afterwards, so the same mailer can be reused for the next message.
Attachments are not cleared unless you ask:
$mailer->setTo('first@example.com')->setSubject('One')->setMessage('…')->send();
$mailer->setTo('second@example.com')->setSubject('Two')->setMessage('…')->send();
$mailer->clear(true); // also drop attachmentsPass send(false) to keep the state instead of clearing it.
- Configuration — all the options you can pass.
- Sending mail — CC/BCC, HTML bodies and more.
- SMTP transport — sending through an SMTP server.