Project

General

Profile

Actions

Mailgun Integration

This integration uses the official library to communicate with Mailgun API. You need to follow instruction on the documentation page: https://github.com/mailgun/mailgun-php

Once you build the required files via Composer, place them to the folder includes/composer/ (it does not exist, you need to create it).

From the Subrion side, the following PHP code should be added to the end of file includes/classes/ia.core.mailer.php

    private function _apiGetAddresses($addresses)
    {
        $result = '';
        foreach ($addresses as $key => $address) $result.= $address[1] . ' <' . $address[0] . '>' . (isset($addresses[$key+1]) ? ',' : '');
        return $result;
    }

    private function _apiGetAttachments()
    {
        $result = array();
        foreach ($this->attachment as $file) $result[] = $file[0];
        return array('attachment' => $result);
    }

    private function _sendThroughMailgunApi()
    {
        require_once IA_INCLUDES . 'composer/autoload.php';

        $mgClient = new Mailgun\Mailgun('key-YOUR-MAILGUN-API-KEY-HERE');

        $params = array(
            'from'    => sprintf('%s <%s>', $this->_iaCore->get('site_from_name', 'Subrion CMS'), $this->_iaCore->get('site_email')),
            'to'      => $this->_apiGetAddresses($this->to),
            'subject' => $this->Subject,
            'html'    => $this->Body
        );

        empty($this->cc) || $params['cc'] = $this->_apiGetAddresses($this->cc);
        empty($this->bcc) || $params['bcc'] = $this->_apiGetAddresses($this->bcc);

        try
        {
            $response = $mgClient->sendMessage('your.mailgun.domain.name.here', $params, $this->_apiGetAttachments());
        }
        catch (Exception $e)
        {
            $this->setError($e->getMessage());
            return false;
        }

        return 200 == $response->http_response_code;
    }

The following existing code of the method send()

$result = (bool)parent::send();

should be replaced by

$result = $this->_sendThroughMailgunApi();

Updated by Vasily Bezruchkin about 4 years ago ยท 2 revisions