Manual:Mailgun

From Linux Web Expert

Mailgun is an API-based tool for sending out emails.

Setup options

$wgSMTP

To set it up, you might put something like the following in your LocalSettings.php file. The $wgSMTP option is simplest, but may expose your server's IP address, increasing the risk of DDOS attacks, so therefore you may instead want to use the cURL option. Be sure to change the values of IDHost, username, and password.

$wgSMTP = array (
        'host' => 'smtp.mailgun.org',
        'IDHost' => 'foowiki.com',
        'port' => 25,
        'username' => 'postmaster@foowiki.com',
        'password' => 'xxx',
        'auth' => true
);

To check whether this is exposing your server's IP address, use "show original" (in Gmail) and look for something like the following:

X-Mailgun-Sending-Ip: xxx.xx.xxx.xxx
X-Mailgun-Sid: ...
Received: from localhost (affiliate.test.known.host [xx.xxx.xx.xxx]) by mxa.mailgun.org with ESMTP id ... ; Tue, 27 Sep 2016 20:42:39 -0000 (UTC)

cURL

To use the cURL option, put this in your LocalSettings.php file, being sure to change the values of MAILGUN_API_KEY and MAILGUN_API_BASE_URL:

define('MAILGUN_API_KEY', 'key-b293646a46b1f7243d096de4df781641');
define('MAILGUN_API_BASE_URL', 'https://api.mailgun.net/v3/foowiki.com');
$wgHooks['AlternateUserMailer'][] = 'foowikimailgunsender';
function foowikimailgunsender( $headers, $to, $from, $subject, $body ) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, 'api:' . MAILGUN_API_KEY);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_URL, MAILGUN_API_BASE_URL . '/messages');
        $recipients = array();
        $id = 1;
        $firstRecipient = true;
        $toString = '';
        foreach ( $to as $toElement) {
                $toString .= $toElement->address;
                $recipients[$toElement->address] = array( 'id' => $id );
                $id++;
                if ( !$firstRecipient ) {
                        $toString .= ',';
                }
                $firstRecipient = false;
        }
        $data =  array(
                'from' => $from,
                'to' => $toString,
                  'subject' => $subject,
                  'text' => $body,
                'recipient-variables' => json_encode ( $recipients )
        );
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data) ;
        $mgResult = curl_exec($ch);
        curl_close($ch);
        return false;
}

Mailgun extension

See Extension:Mailgun.

See also

Configuration
Extension