PHP code example of bogardo / mailgun

1. Go to this page and download the library: Download bogardo/mailgun library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bogardo / mailgun example snippets


Mailgun::send('emails.invoice', $data, function ($message) {
    $message
        ->subject('Your Invoice')
        ->to('[email protected]', 'John Doe')
        ->bcc('[email protected]')
        ->attach(storage_path('invoices/12345.pdf'))
        ->trackClicks(true)
        ->trackOpens(true)
        ->tag(['tag1', 'tag2'])
        ->campaign(2);
});

// config/app.php

'providers' => [
    ...
    Bogardo\Mailgun\MailgunServiceProvider::class

];

...

'aliases' => [
	...
    'Mailgun' => Bogardo\Mailgun\Facades\Mailgun::class
],

$this->app->bind('mailgun.client', function() {
	return \Http\Adapter\Guzzle6\Client::createWithConfig([
		// your Guzzle6 configuration
	]);
});

Mailgun::send('emails.welcome', $data, function ($message) {
    $message->to('[email protected]', 'John Doe')->subject('Welcome!');
});

Mailgun::send(['html' => 'emails.htmlmail', 'text' => 'emails.textmail'], $data, $callback);

Mailgun::send(['emails.htmlmail','emails.textmail'], $data, $callback);

Mailgun::send(['emails.htmlmail'], $data, $callback);

Mailgun::send(['text' => 'emails.textmail'], $data, $callback);

Mailgun::raw("This is the email body", $callback);
	
$data = [
	'customer' => 'John Doe',
	'url' => 'http://laravel.com'
];

Mailgun::send('emails.welcome', $data, function ($message) {
	$message->to('[email protected]', 'John Doe')->subject('Welcome!');
});

Mailgun::send('emails.welcome', $data, function($message) {
	$message->to('[email protected]', 'Recipient Name');
});

$message->cc('[email protected]', 'Recipient Name');

$message->bcc('[email protected]', 'Recipient Name');

use Bogardo\Mailgun\Mail\Message;

Mailgun::send('email.batch', $data, function(Message $message){
    $message->to([
        '[email protected]' => [
            'name' => 'User One',
            'age' => 37,
            'city' => 'New York'
        ],
        '[email protected]' => [
            'name' => 'User Two',
            'age' => 41,
            'city' => 'London'
        ]
    ]);
});

// Or

Mailgun::send('email.batch', $data, function(Message $message){
    $message->to('[email protected]', 'User One', [
        'age' => 37, 
        'city' => 'New York'
    ]);
    $message->to('[email protected]', 'User Two', [
        'age' => 41,
        'city' => 'London'
    ]);
});


// resources/views/email/batch.blade.php

Hi %recipient.name%,

Age: %recipient.age%
City: %recipient.city%


// with name
$message->from('[email protected]', 'Recipient Name');

// without name
$message->from('[email protected]');

$message->subject('Email subject');

$message->replyTo('[email protected]', 'Helpdesk');

$message->attach($path, $name);

$data = [
	'img' => 'assets/img/example.png',
	'otherImg' => 'assets/img/foobar.jpg'
];

Mailgun::send('emails.welcome', $data, function ($message) {
I	$message->to('[email protected]', 'Recipient Name');
});

Mailgun::later(60, 'emails.welcome', $data, function ($message) {
    $message->to('[email protected]', 'John Doe')->subject('Welcome!');
});

Mailgun::later(['hours' => 5], 'emails.welcome', $data, function($message) {
    $message->to('[email protected]', 'John Doe')->subject('Welcome!');
});

Mailgun::send('emails.welcome', $data, function ($message) {
	$message->tag('myTag');
});

Mailgun::send('emails.welcome', $data, function ($message) {
	$message->tag(['Tag1', 'Tag2', 'Tag3']);
});

Mailgun::send('emails.welcome', $data, function ($message) {
	$message->campaign('my_campaign_id');
	//or
	$message->campaign(['campaign_1', 'campaign_2', 'campaign_3']);
});

Mailgun::send('emails.welcome', $data, function ($message) {
	$message->trackClicks(true);
	//or
	$message->trackClicks(false);
});

Mailgun::send('emails.welcome', $data, function ($message) {
	$message->trackOpens(true);
	//or
	$message->trackOpens(false);
});

Mailgun::send('emails.welcome', $data, function ($message) {
	$message->dkim(true);
	// or
	$message->dkim(false);
});

Mailgun::send('emails.welcome', $data, function ($message) {
	$message->testmode(true);
	// or
	$message->testmode(false);
});

'api' => [
    'endpoint' => 'api.mailgun.net',
    'version' => 'v3',
    'ssl' => true
],

'api' => [
    'endpoint' => 'bin.mailgun.net',
    'version' => 'abc1de23', // your Bin ID
    'ssl' => false
],

Mailgun::send('emails.welcome', $data, function ($message) {
	$message->header($name, $value);
});

Mailgun::send('emails.welcome', $data, function ($message) {
	$message->data($key, $value);
});

namespace App\Http\Controllers;

class CustomController extends Controller
{

    /**
     * @var \Bogardo\Mailgun\Contracts\Mailgun
     */
    protected $mailgun;

    /**
     * @param \Bogardo\Mailgun\Contracts\Mailgun $mailgun
     */
    public function __construct(\Bogardo\Mailgun\Contracts\Mailgun $mailgun)
    {
        $this->mailgun = $mailgun;
    }
    
    public function index()
    {
        $this->mailgun->send($view, $data, $callback);
    }
}

Mailgun::api()->get("lists/pages");

Mailgun::api()->get("lists/{$list}");

Mailgun::api()->post("lists", [
    'address'      => '[email protected]',
    'name'         => 'Developers',
    'description'  => 'Developers Mailing List',
    'access_level' => 'readonly'
]);

Mailgun::api()->put("lists/{$list}/members/{$member}", [
    'address'      => '[email protected]',
    'name'         => 'John Doe',
    'vars'         => json_encode(['age' => 35, 'country' => 'US']),
    'subscribed'   => 'no'
]);

$secretKey   = 'a_very_secret_key';

$listaddress = '[email protected]';
$subscriber  = '[email protected]';

$hash = Mailgun::optInHandler()->generateHash($listaddress, $secretKey, $subscriber);

var_dump($hash);
string 'eyJoIjoiODI2YWQ0OTRhNzkxMmZkYzI0MGJjYjM2MjFjMzAyY2M2YWQxZTY5MyIsInAiOiJleUp5SWpvaWNtVmphWEJwWlc1MFFHVjRZVzF3YkdVdVkyOXRJaXdpYkNJNkltMWhhV3hwYm1kc2FYTjBRR1Y0WVcxd2JHVXVZMjl0SW4wPSJ9' (length=180)

$result = Mailgun::optInHandler()->validateHash($secretKey, $hash);

var_dump($result);
array (size=2)
	'recipientAddress' => string '[email protected]' (length=21)
	'mailingList' => string '[email protected]' (length=23)
  
// Subscribe the user to the mailinglist
Mailgun::api()->post("lists/{$result['mailingList']}/members", [
    'address' => $result['recipientAddress'],
    'subscribed' => 'yes'
]);

Mailgun::validator()->validate("[email protected]");

stdClass Object
(
    [address] => [email protected]
    [did_you_mean] => 
    [is_valid] => 1
    [parts] => stdClass Object
        (
            [display_name] => 
            [domain] => bar.com
            [local_part] => foo
        )

)

Mailgun::validator()->validate("[email protected]")

stdClass Object
(
    [address] => [email protected]
    [did_you_mean] => [email protected]
    [is_valid] => 1
    [parts] => stdClass Object
        (
            [display_name] => 
            [domain] => gmil.com
            [local_part] => foo
        )

)

$addresses = 'Alice <[email protected]>,[email protected],example.com';
//or
$addresses = [
	'Alice <[email protected]>',
	'[email protected]',
	'example.com'
];

Mailgun::validator()->parse($addresses);

stdClass Object
(
    [parsed] => Array
        (
            [0] => Alice <[email protected]>
            [1] => [email protected]
        )

    [unparseable] => Array
        (
            [0] => example.com
        )

)

$addresses = 'Alice <[email protected]>,[email protected],example.com';
Mailgun::validator()->parse($addresses, false);

stdClass Object
(
    [parsed] => Array
        (
        )

    [unparseable] => Array
        (
            [0] => Alice <[email protected]>
            [1] => [email protected]
            [2] => example.com
        )

)
bash
php artisan vendor:publish --provider="Bogardo\Mailgun\MailgunServiceProvider" --tag="config"
bash
php artisan vendor:publish
html
<body>
    <img src="{{ $message->embed($path, 'rename.png'); }}">
</body>