~thirdplace/mailgun

bcb1e412c2f86ac37abd4ed89c7841358ebd84c5 — Dag 2 years ago 131181f
refactor
3 files changed, 74 insertions(+), 50 deletions(-)

M README.md
M composer.json
M mailgun.php
M README.md => README.md +47 -12
@@ 1,22 1,57 @@
# Mailgun client library
# Mailgun client

Send email using mailgun's http api.
Send email via their http api.

## Tutorial

Install
Install:

    composer require thirdplace/mailgun
    composer require thirdplace/mailgun:dev-main

Example
Example:

    <?php
        use function Thirdplace\mailgun_send_email;

        \Thirdplace\mailgun_send_email(
            'example.com',
            'API_KEY',
            'FROM',
            'TO',
            'SUBJECT',
            'TEXT'
        $apiDomain = 'example.com';
        $apiKey = 'key-XXXXXXXXXXXXXXXXXXXXXX';

        $result = mailgun_message_send(
            $apiDomain,
            $apiKey,
            'noreply@example.com',
            'recipient@example.com',
            'Here is the subject',
            'Here is the email body',
        );

        var_dump($result);

Output:

    array(2) {
      [0]=>
      int(200)
      [1]=>
      string(88) "{"id":"<20220423070303.1449106df60a3dbb@example.com>","message":"Queued. Thank you."}
    "
    }

## How-to

## Explanation

Uses the us endpoint.

## Reference

    function mailgun_message_send(
        string $apiDomain,
        string $apiKey,
        string $from,
        string $to,
        string $subject,
        string $text
    ): array

https://documentation.mailgun.com/en/latest/index.html

M composer.json => composer.json +5 -3
@@ 1,7 1,7 @@
{
    "name": "thirdplace/mailgun",
    "description": "Mailgun client library for sending email",
    "keywords": ["mailgun"],
    "description": "Mailgun client - send email via their http api",
    "keywords": ["mailgun", "email"],
    "license": "GPL-3.0-or-later",
    "homepage": "https://thirdplace.no",
    "authors": [


@@ 24,6 24,8 @@
        ]
    },
    "require": {
        "php" : ">=7.4"
        "php" : ">=7.4",
        "ext-curl": "*",
        "ext-json": "*"
    }
}

M mailgun.php => mailgun.php +22 -35
@@ 1,53 1,40 @@
<?php
declare(strict_types=1);

namespace Thirdplace;

function mailgun_send_email(
final class MailgunException extends \Exception {}

/**
 * @throws MailgunException
 */
function mailgun_message_send(
    string $apiDomain,
    string $apiKey,
    string $from,
    string $to,
    string $subject,
    string $text
) {
    $url = "https://api.mailgun.net/v3/$apiDomain/messages";

    $config = [
        'useragent'         => 'thirdplace mailgun client',
        'connect_timeout'   => 5,
        'timeout'           => 5,
        'follow_location'   => true,
        'max_redirs'        => 5,
    ];

    $post = [
): array {
    $ch = curl_init(sprintf('https://api.mailgun.net/v3/%s/messages', $apiDomain));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch, CURLOPT_POSTFIELDS, [
        'from'      => $from,
        'to'        => $to,
        'subject'   => $subject,
        'text'      => $text,
    ];

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,                     $url);
    curl_setopt($ch, CURLOPT_HEADER,                  false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,          true);
    curl_setopt($ch, CURLOPT_USERAGENT,               $config['useragent']);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,          $config['connect_timeout']);
    curl_setopt($ch, CURLOPT_TIMEOUT,                 $config['timeout']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,          $config['follow_location']);
    curl_setopt($ch, CURLOPT_MAXREDIRS,               $config['max_redirs']);
    curl_setopt($ch, CURLOPT_ENCODING,                '');
    curl_setopt($ch, CURLOPT_POSTFIELDS,              $post);
    curl_setopt($ch, CURLOPT_USERPWD,                 "api:$apiKey");

    $response = curl_exec($ch);

    if ($response === false) {
        throw new \Exception($url . ': ' . curl_error($ch), curl_errno($ch));
    ]);
    curl_setopt($ch, CURLOPT_USERPWD, "api:$apiKey");
    $result = curl_exec($ch);
    if ($result === false) {
        throw new MailgunException(curl_error($ch), curl_errno($ch));
    }
    $code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
    curl_close($ch);
    return [$code, $response];
    if ($code !== 200) {
        throw new MailgunException("$code $result", $code);
    }
    return [$code, $result];
}