Pāriet uz saturu
MailGateAPI

Īsā pamācība

Izveidojiet testa API atslēgu un norādiet simulatora saņēmēja adresi. Testa ziņojumi ļauj pārbaudīt API darbību un reģistrēt simulētu rezultātu, nepiegādājot e-pastu.

Simulators pašlaik nav pieejams.

  1. Iestatiet darba vidi

    Izveidojiet kontu, apstipriniet savu e-pasta adresi un pierakstieties.

  2. Izveidojiet API atslēgu

    Veidojot atslēgu, izvēlieties testa režīmu. Testa atslēgas sākas ar mg_test_.

    Izveidojiet atslēgu darba vidē un glabājiet tās slepeno vērtību servera vidē. Neiekļaujiet to pārlūkā izpildāmajā kodā, koda repozitorijos vai žurnālos.

  3. Nosūtiet simulētu e-pastu

    Iestatiet MAILGATE_API_KEY komandrindas vidē un izpildiet šo pieprasījumu. Simulators izmanto šeit norādīto piemēra sūtītāju.

    Glabājiet MAILGATE_API_KEY termināļa vidē vai servera slepeno datu pārvaldniekā.

    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)

    Atbildē ar kodu 202 ir norādīts ziņojuma ID. Simulētais rezultāts ir redzams vadības paneļa testa vides žurnālā. Citam ziņojumam izvēlieties jaunu Idempotency-Key vērtību.

  4. Apstipriniet sūtīšanas domēnu

    Kad pakalpojums ir ieslēgts, reālu e-pastu sūtīšanai izmantojiet apstiprinātu sūtīšanas domēnu un reālai sūtīšanai paredzētu API atslēgu.

    Pievienojiet domēnu darba vidē. Publicējiet iestatījumos norādītos DNS ierakstus un gaidiet, līdz domēns tiks apstiprināts.

Simulatora saņēmēji

Saņēmējs Rezultāts
delivered@simulator.mailgate.test Simulēta piegāde
bounced@simulator.mailgate.test Simulēts paziņojums par nepiegādātu e-pastu
complained@simulator.mailgate.test Simulēta sūdzība
API uzziņa