618

Product support

Get help in the plugin support forum.

  • Added on Aug 29, 2016
  • Current version: 1.0.1
  • Platform Compatibility
    v3.x not tested
    v2.x not tested
    v1.x use latest
  • Created by
  • Public repository

Categories

This plugin provides the endpoint for awesome Mailgun Routes feature.

It provides you with a component, which you can put on a CMS page, therefore making it API endpoint.

Every notification from Mailgun Routes will be saved into database. You will have access to it in a webmail-like interface in the backend and you will be able to listen to plugin's event in order to do some further e-mail parsing or analysis.

Configuration

You should have a Mailgun account.

  • Configure a domain with Mailgun
  • Configure it for inbound traffic (if your main domain has e-mail service already active, you will probably want to create MX records for mg.yourdomain.com record and get emails from address like notifications@mg.yourdomain.com.)
  • Go to Routes and add New route.
  • Setup Expression as you like (you may want to notify October about all emails or email just from a specific sender)
  • Select Store and notify in Actions section. Paste there your endpoint OctoberCMS page (eg. http://domain.com/mail-notif)
  • Optionally, select Stop if this is the only Route that should be valid for these emails.

That's it about Mailgun. Leave the routes tab open, we'll configure OctoberCMS now.

  • Install the plugin
  • Create a new empty page with no layout.
  • Provide it with URL you configured in mailgun (eg. /mail-notif)
  • Put "mailgun_endpoint" component there.
title = "Mailgun Endpoint"
url = "/mail-notif"
is_hidden = 0

[mailgun_endpoint]
==
{% component "mailgun_endpoint" %}

That's it!

You can send Test Post Request from Mailgun Route Page to see if all works properly.

You should get the e-mail saved and available in Mailgun Endpoint controller, with all the content and attachments.

Events

Whenever the e-mail notification is arriving, plugin emits an Event, which includes the notification model to work on.

You can easily subscribe to this event in your plugin and get the full email content and attachments for further processing.

Basic usage:

Put following code into your Plugin.php boot method:

Event::listen('mailgunendpoint.newemail', function($event) {
    // do something here - $event is your MailgunNotification model.
});

Example usage:

/**
* If email receiver matches 'email' field of some UserProfile, add email ID to pivot table.
**/
public method boot(){
    Event::listen('mailgunendpoint.newemail', function($event) {
         /**
         * Let's create a Notification object from the model
         * @var \Keios\MailgunEndpoint\Classes\Notification $notification
         **/
          $notification = $event->toNotification();
          $userProfile = UserProfile::where('email', $notification->getReceiverEmail())->first();
          if ($userProfile){
               DB::table('user_emails')->insert(['user_id' => $userProfile->id, 'mail_id' => $event->id]);
          } 
    });
}

It is quite recommended to create Notification object from the model. Object has some useful methods included (eg getReceiverEmail, getReceiverName etc) and may save you some regex rules.

1.0.1

First version of MailgunEndpoint

Aug 28, 2016