Preskoči na sadržaj
MailGateAPI

Kratki vodič

Izradite testni API ključ i upotrijebite adresu primatelja za simulator. Testne poruke služe za isprobavanje API-ja i bilježe simulirani ishod bez isporuke e-pošte.

Simulator trenutačno nije dostupan.

  1. Postavite radni prostor

    Otvorite račun, potvrdite adresu e-pošte i prijavite se.

  2. Izradite API ključ

    Pri izradi ključa odaberite testni način rada. Testni ključevi počinju prefiksom mg_test_.

    Izradite ključ u radnom prostoru i spremite njegovu tajnu vrijednost u okruženje poslužitelja. Nemojte je navoditi u kodu koji se izvršava u pregledniku, repozitorijima ili zapisnicima.

  3. Pošaljite simuliranu e-poruku

    Postavite varijablu MAILGATE_API_KEY u naredbenoj ljusci pa pokrenite ovaj zahtjev. Simulator upotrebljava pošiljatelja iz prikazanog primjera.

    Varijablu MAILGATE_API_KEY čuvajte u okruženju terminala ili u sustavu za upravljanje tajnama na poslužitelju.

    cURL

    POST /api/v1/emails
    curl --fail-with-body --request POST https://mailgateapi.com/api/v1/emails \
      --header "Authorization: Bearer ${MAILGATE_API_KEY:?Set MAILGATE_API_KEY}" \
      --header "Content-Type: application/json" \
      --header "Idempotency-Key: first-test-1" \
      --data '{
        "from": "noreply@simulator.mailgate.test",
        "to": "delivered@simulator.mailgate.test",
        "subject": "My first test",
        "text": "Hello from the MailGateAPI simulator."
      }'

    PHP

    POST /api/v1/emails
    <?php
    
    $key = getenv('MAILGATE_API_KEY') ?: throw new RuntimeException('Set MAILGATE_API_KEY');
    $request = curl_init('https://mailgateapi.com/api/v1/emails');
    curl_setopt_array($request, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 20,
        CURLOPT_HTTPHEADER => [
            'Authorization: Bearer '.$key,
            'Content-Type: application/json',
            'Idempotency-Key: first-test-1',
        ],
        CURLOPT_POSTFIELDS => json_encode([
            'from' => 'noreply@simulator.mailgate.test',
            'to' => 'delivered@simulator.mailgate.test',
            'subject' => 'My first test',
            'text' => 'Hello from the MailGateAPI simulator.',
        ], JSON_THROW_ON_ERROR),
    ]);
    
    $body = curl_exec($request);
    if ($body === false) {
        throw new RuntimeException(curl_error($request));
    }
    echo $body, PHP_EOL;
    exit(curl_getinfo($request, CURLINFO_RESPONSE_CODE) === 202 ? 0 : 1);

    Node.js

    POST /api/v1/emails
    const key = process.env.MAILGATE_API_KEY;
    if (!key) throw new Error('Set MAILGATE_API_KEY');
    
    fetch('https://mailgateapi.com/api/v1/emails', {
      method: 'POST',
      signal: AbortSignal.timeout(20_000),
      headers: {
        Authorization: `Bearer ${key}`,
        'Content-Type': 'application/json',
        'Idempotency-Key': 'first-test-1',
      },
      body: JSON.stringify({
        from: 'noreply@simulator.mailgate.test',
        to: 'delivered@simulator.mailgate.test',
        subject: 'My first test',
        text: 'Hello from the MailGateAPI simulator.',
      }),
    }).then(async (response) => {
      console.log(await response.text());
      if (response.status !== 202) process.exitCode = 1;
    }).catch((error) => {
      console.error(error.message);
      process.exitCode = 1;
    });

    Python

    POST /api/v1/emails
    import json
    import os
    from urllib.error import HTTPError
    from urllib.request import Request, urlopen
    
    request = Request(
        "https://mailgateapi.com/api/v1/emails",
        method="POST",
        headers={
            "Authorization": "Bearer " + os.environ["MAILGATE_API_KEY"],
            "Content-Type": "application/json",
            "Idempotency-Key": "first-test-1",
        },
        data=json.dumps({
            "from": "noreply@simulator.mailgate.test",
            "to": "delivered@simulator.mailgate.test",
            "subject": "My first test",
            "text": "Hello from the MailGateAPI simulator.",
        }).encode("utf-8"),
    )
    
    try:
        with urlopen(request, timeout=20) as response:
            print(response.read().decode("utf-8"))
    except HTTPError as error:
        print(error.read().decode("utf-8"))
        raise SystemExit(1)

    Odgovor 202 sadrži ID poruke. Simulirani ishod vidljiv je među testnim aktivnostima u pregledu radnog prostora. Za drugu poruku odaberite novu vrijednost zaglavlja Idempotency-Key.

  4. Potvrdite domenu za slanje

    Kada slanje stvarnih poruka bude omogućeno, upotrijebite potvrđenu domenu za slanje i produkcijski API ključ.

    Dodajte domenu u radni prostor. Objavite DNS zapise prikazane pri postavljanju i pričekajte dovršetak provjere.

Adrese primatelja za simulator

Primatelj Ishod
delivered@simulator.mailgate.test Simulirana isporuka
bounced@simulator.mailgate.test Simulirani povrat poruke
complained@simulator.mailgate.test Simulirana pritužba
Referentna dokumentacija za API