Mailgun plugin for CakePHP 3
This plugin provides email delivery using Mailgun. See this post for detailed examples.
Requirements
This plugin has the following requirements:
- CakePHP 3.0.0 or greater.
- PHP 5.4.16 or greater.
Installation
You can install this plugin into your CakePHP application using composer.
composer require narendravaghela/cakephp-mailgun
After installation, Load the plugin
Plugin::load('MailgunEmail');
Or, you can load the plugin using the shell command
$ bin/cake plugin load MailgunEmail
Setup
Set your Mailgun credentials in EmailTransport
settings in app.php
'EmailTransport' => [
...
'mailgun' => [
'className' => 'MailgunEmail.Mailgun',
'apiKey' => 'key-123456789123456789', // your api key
'domain' => 'test.mailgun.org' // your sending domain
]
]
If you face an SSL certificate error, please follow below steps:
- Open http://curl.haxx.se/ca/cacert.pem
- Copy the entire page and save it as a "cacert.pem"
- Open your php.ini file and insert or update the following line: curl.cainfo = "[pathtofile]\cacert.pem"
And create new delivery profile for mailgun in Email
settings.
'Email' => [
'default' => [
'transport' => 'default',
'from' => 'you@localhost',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
],
'mailgun' => [
'transport' => 'mailgun'
]
]
Usage
You can now simply use the CakePHP Email
to send an email via Mailgun.
$email = new Email('mailgun');
$result = $email->from(['foo@example.com' => 'Example Site'])
->to('bar@example.com')
->subject('Welcome to CakePHP')
->template('welcome')
->viewVars(['foo' => 'Bar'])
->emailFormat('both')
->addHeaders(['o:tag' => 'testing'])
->addHeaders(['o:deliverytime' => strtotime('+1 Min')])
->addHeaders(['v:my-custom-data' => json_encode(['foo' => 'bar'])])
->readReceipt('admin@example.com')
->returnPath('bounce@example.com')
->attachments([
'cake_icon1.png' => Configure::read('App.imageBaseUrl') . 'cake.icon.png',
'cake_icon2.png' => ['file' => Configure::read('App.imageBaseUrl') . 'cake.icon.png'],
WWW_ROOT . 'favicon.ico'
])
->send('Your email content here');
That is it.
Reporting Issues
If you have a problem with this plugin or any bug, please open an issue on GitHub.