Repository navigation
Quick Start
Every example below builds a message with the fluent API and calls send().
send() returns nothing and throws on failure — see
Error Handling.
use InitPHP\Mailer\Mailer;
$mailer = Mailer::newInstance(); // defaults to the mail() transportMailer::newInstance($config) and new Mailer($config) are equivalent. The
$config array is optional; see Configuration.
use InitPHP\Mailer\Exception\MailerException;
try {
$mailer->setFrom('you@example.com', 'Your Name')
->setTo('recipient@example.com')
->setSubject('Hello')
->setMessage('This is the body of the message.')
->send();
} catch (MailerException $e) {
echo 'Could not send: ' . $e->getMessage();
}Only the configuration changes — the build/send code is identical:
$mailer = Mailer::newInstance([
'protocol' => 'smtp',
'SMTPHost' => 'smtp.example.com',
'SMTPUser' => 'you@example.com',
'SMTPPass' => 'secret',
'SMTPPort' => 587,
'SMTPCrypto' => 'tls',
]);
$mailer->setFrom('you@example.com', 'Your Name')
->setTo('recipient@example.com')
->setSubject('Hello over SMTP')
->setMessage('Sent through an SMTP server.')
->send();$mailer->setMailType('html')
->setFrom('you@example.com', 'Your Name')
->setTo('recipient@example.com')
->setSubject('Your invoice')
->setMessage('<h1>Thank you!</h1><p>Your invoice is attached.</p>')
->setAltMessage('Thank you! Your invoice is attached.')
->attach('/path/to/invoice.pdf')
->send();$mailer->setFrom('you@example.com')
->setTo(['alice@example.com', 'bob@example.com'])
->setCC('manager@example.com')
->setBCC('audit@example.com')
->setSubject('Team update')
->setMessage('…')
->send();After a successful send(), the per-message state (recipients, subject, body,
custom headers) is cleared automatically, so you can send the next message right
away. Attachments are kept unless you call clear(true).
$mailer->setTo('first@example.com')->setSubject('One')->setMessage('…')->send();
$mailer->setTo('second@example.com')->setSubject('Two')->setMessage('…')->send();Pass send(false) to keep the state instead.
- Configuration — every option you can pass.
- Sending Mail — recipients, reply-to, BCC batching.
- HTML Messages and Attachments.
- Error Handling — the exceptions and how to catch them.
initphp/mailer · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Sending
Reliability
Reference
Migration