Send email across all platforms using one interface.
This library uses PHP 5.5+.
It is recommended that you install the Omnimail library through composer. To do so, run the Composer command to install the latest stable version of Omnimail library.
composer require omnimail/omnimail
To use the AmazonSES sender class, you will need to install the daniel-zahariev/php-aws-ses
library using composer.
composer require daniel-zahariev/php-aws-ses
use Omnimail\Email;
use Omnimail\AmazonSES;
$sender = new AmazonSES($accessKey, $secretKey);
$email = (new Email())
->addTo('[email protected]')
->setFrom('[email protected]')
->setSubject('Hello, world!')
->setTextBody('Hello World! How are you?');
$sender->send($email);
To use the Mailgun sender class, you will need to install the mailgun/mailgun-php
library using composer.
composer require mailgun/mailgun-php
use Omnimail\Email;
use Omnimail\Mailgun;
$sender = new Mailgun($apiKey, $domain);
$email = (new Email())
->addTo('[email protected]')
->setFrom('[email protected]')
->setSubject('Hello, world!')
->setTextBody('Hello World! How are you?');
$sender->send($email);
To use the Mailjet sender class, you will need to install the mailjet/mailjet-apiv3-php
library using composer.
composer require mailjet/mailjet-apiv3-php
use Omnimail\Email;
use Omnimail\Mailjet;
$sender = new Mailjet($apikey, $apisecret);
$email = (new Email())
->addTo('[email protected]')
->setFrom('[email protected]')
->setSubject('Hello, world!')
->setTextBody('Hello World! How are you?');
$sender->send($email);
To use the Mandrill sender class, you will need to install the mandrill/mandrill
library using composer.
composer require mandrill/mandrill
use Omnimail\Email;
use Omnimail\Mandrill;
$sender = new Mandrill($apiKey);
$email = (new Email())
->addTo('[email protected]')
->setFrom('[email protected]')
->setSubject('Hello, world!')
->setTextBody('Hello World! How are you?');
$sender->send($email);
To use the Postmark sender class, you will need to install the wildbit/postmark-php
library using composer.
composer require wildbit/postmark-php
use Omnimail\Email;
use Omnimail\Postmark;
$sender = new Postmark($serverApiToken);
$email = (new Email())
->addTo('[email protected]')
->setFrom('[email protected]')
->setSubject('Hello, world!')
->setTextBody('Hello World! How are you?');
$sender->send($email);
To use the Sendgrid sender class, you will need to install the sendgrid/sendgrid
library using composer.
composer require sendgrid/sendgrid
use Omnimail\Email;
use Omnimail\Sendgrid;
$sender = new Sendgrid($apiKey);
$email = (new Email())
->addTo('[email protected]')
->setFrom('[email protected]')
->setSubject('Hello, world!')
->setTextBody('Hello World! How are you?');
$sender->send($email);
To use the SendinBlue sender class, you will need to install the mailin-api/mailin-api-php
library using composer.
composer require mailin-api/mailin-api-php
use Omnimail\Email;
use Omnimail\SendinBlue;
$sender = new SendinBlue($accessKey);
$email = (new Email())
->addTo('[email protected]')
->setFrom('[email protected]')
->setSubject('Hello, world!')
->setTextBody('Hello World! How are you?');
$sender->send($email);
An Email
object implements the EmailInterface
inteface. You can create your own Email
class and send it to any
sender if it implements the EmailInterface
inteface.
The To
property of the email is for defining the recipients of the email. You can set multiple recipients.
$email = new Email();
$email->addTo('[email protected]', 'Recipient1 Name');
$email->addTo('[email protected]', 'Recipient2 Name');
The From
property of the email is for defining the sender of the email.
$email = new Email();
$email->setFrom('[email protected]', 'Sender Name');
Like the To
property, the CC
property can have multiple recipients.
$email = new Email();
$email->addCc('[email protected]', 'Recipient1 Name');
$email->addCc('[email protected]', 'Recipient2 Name');
Like the To
property, the BCC
property can have multiple recipients.
$email = new Email();
$email->addBcc('[email protected]', 'Recipient1 Name');
$email->addBcc('[email protected]', 'Recipient2 Name');
The Reply To
property of the email is for defining the email that should receive responses.
$email = new Email();
$email->setReplyTo('[email protected]', 'Sender Name');
The Subject
property of the email is for defining the subject of the email.
$email = new Email();
$email->setSubject('Hello, World!');
The Text Body
property of the email is for defining the text body of the email.
$email = new Email();
$email->setTextBody('This is plain text.');
The HTML Body
property of the email is for defining the HTML body of the email.
$email = new Email();
$email->setHtmlBody('<h1>Hi!</h1><p>This is HTML!</p>');
The Attachments
property of the email is for joining attachments to the email.
use Omnimail\Email;
use Omnimail\Attachment;
$attachment = new Attachment();
$attachment->setName('my_file.txt');
$attachment->setMimeType('text/plain');
$attachment->setContent('This is plain text');
$email = new Email();
$email->addAttachment($attachment);
use Omnimail\Email;
use Omnimail\Attachment;
$attachment = new Attachment();
$attachment->setMimeType('text/plain');
$attachment->setPath(__DIR__ . '/my_file.txt');
$email = new Email();
$email->addAttachment($attachment);
use Omnimail\Email;
use Omnimail\Attachment;
$attachment = new Attachment();
$attachment->setPath(__DIR__ . '/image.png');
$attachment->setContentId('image.png');
$email = new Email();
$email->setHtmlBody('<p>Hello!</p><img src="cid:image.png">');
$email->addAttachment($attachment);
Failures to send emails will throw exceptions.
Exceptions
- Omnimail\Exception\Exception
- Omnimail\Exception\EmailDeliveryException
- Omnimail\Exception\InvalidRequestException
- Omnimail\Exception\UnauthorizedException
To catch all exception, consider the following.
try {
$sender->send($email);
} catch (\Omnimail\Exception\Exception $e) {
echo 'Something went wrong: ' . $e->getMessage();
}
To catch specific exceptions, consider the following.
try {
$sender->send($email);
} catch (\Omnimail\Exception\UnauthorizedException $e) {
echo 'Your credentials must be incorrect';
} catch (\Omnimail\Exception\InvalidRequestException $e) {
echo 'The request is badly formatted, check that all required fields are filled.';
} catch (\Omnimail\Exception\EmailDeliveryException $e) {
echo 'The email did not go out.';
}
All sender constructors take a PSR-3 compatible logger.
Email sent (including the email) are logged at INFO level. Errors (including the email) are reported at the ERROR level.
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Omnimail\Mailgun;
$logger = new Logger('name');
$logger->pushHandler(new StreamHandler('path/to/your.log', Logger::INFO));
$sender = new Mailgun($apiKey, $domain, $logger);
$sender->send($email);
Omnimail is licensed under The MIT License (MIT).