OhMySMTP lets you send transactional emails from your app over an easy to use API.
This OhMySMTP PHP Package is a transport for SwiftMailer to send emails via OhMySMTP to make sending emails from PHP apps super simple. You can use this with popular frameworks such as Laravel, Codeigniter and Symfony to send transactional emails, or with a standalone PHP app.
This package uses the OhMySMTP HTTPS /send endpoint to send the email - which is generally faster and more reliable than SMTP, although you can of course use SMTP to send out emails from your PHP app without installing this package if you prefer.
You will need an OhMySMTP account with a verified domain and organization with an active plan.
Install the package via composer:
composer require ohmysmtp/ohmysmtp-swiftmailer
Set up an account at OhMySMTP and complete the Onboarding steps
First you will need to retrieve your API token for your sending domain from OhMySMTP. You can find it under Organization -> Domain -> API Tokens
You'll need to store this API token somewhere in your app/runtime, we recommend Environment Variables for this, and the examples below assume that you have an environment variable called OHMYSMTP_API_TOKEN
that contains your API token
<?php
require_once('./vendor/autoload.php');
$transport = new OhmysmtpSwiftmailerTransport(env('OHMYSMTP_API_TOKEN'));
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message('A transactional email from OhMySMTP!'))
->setFrom(['php@yourdomain.com' => 'Your Name'])
->setTo(['someone@example.com'])
->setBody('<h1>HTML content</h1>', 'text/html')
->addPart('Text Body','text/plain');
// Attachments
$data = 'Raw Attachment Data';
$attachment = new Swift_Attachment($data, 'attachment.txt', 'text/plain');
$message->attach($attachment);
// Email Tags
$headers = $message->getHeaders();
$headers->addTextHeader('OMS-Tag', 'tag-1');
$headers->addTextHeader('OMS-Tag', 'tag with spaces');
$mailer->send($message);
?>
To send with Laravel you need to make a few small tweaks, but it really only takes a moment.
- Add ohmysmtp to the
config/mail.php
configuration file:
'ohmysmtp' => [
'transport' => 'ohmysmtp',
],
- Add the following to your
config/services.php
configuration file:
'ohmysmtp' => [
'apiToken' => env('OHMYSMTP_API_TOKEN'),
]
- In
config/app.php
, add the following to the providers array:
App\Providers\OhmysmtpServiceProvider::class,
and remove / comment out the line:
Illuminate\Mail\MailServiceProvider::class,
- In your
.env
file (or wherever you store environment variables), change theMAIL_MAILER
variable as follows:
MAIL_MAILER=ohmysmtp
- Create a new file called
OhmysmtpServiceProvider.php
inApp/Providers
with the following contents:
<?php
namespace App\Providers;
use Illuminate\Mail\MailManager;
use Illuminate\Mail\MailServiceProvider;
use Ohmysmtp\OhmysmtpSwiftmailer\OhmysmtpSwiftmailerTransport;
class OhmysmtpServiceProvider extends MailServiceProvider
{
protected function registerIlluminateMailer()
{
$this->app->singleton('mail.manager', function ($app) {
$manager = new MailManager($app);
$this->registerOhMySmtpTransport($manager);
return $manager;
});
}
protected function registerOhMySmtpTransport(MailManager $manager) {
$manager->extend('ohmysmtp', function ($config) {
if (! isset($config['apiToken'])) {
$config = $this->app['config']->get('services.ohmysmtp', []);
}
return new OhmysmtpSwiftmailerTransport($config['apiToken']);
});
}
}
After completing the above steps, all email will be sent via OhMySMTP.
For support please check the OhMySMTP Documentation or contact us at support@ohmysmtp.com
Please ensure to add a test for any changes. To run the tests:
composer test
Pull requests always welcome
The gem is available as open source under the terms of the MIT License.