phpmailgun

How to delete a mail from Mailgun API domain queue?


I'm writing a module in PHP Laminas to send mails through Mailgun API. I'm able to send the mail as well as list the pending queue. I want to delete a particular mail in mailgun using message-id. Can someone please guide me if its even possible? If yes, how?

The closest I've got in terms of documentation is clearing the domain queue which isn't what I want. https://help.mailgun.com/hc/en-us/articles/360012487654-How-Can-I-Delete-Messages-From-the-Queue-

Below is the code I've written to send the mail using Mailgun.

    /**
     * MailgunApiSendRequest constructor.
     *
     * @param MailgunApiModel $mailgunApiModel
     * @param MailgunApiSendResponse $mailgunApiSendResponse
     */
    public function __construct(MailgunApiModel $mailgunApiModel, MailgunApiSendResponse $mailgunApiSendResponse)
    {
        $this->mailgunApiModel = $mailgunApiModel;
        $this->mailgunApiSendResponse = $mailgunApiSendResponse;
    }

    /**
     * @return MailgunApiSendResponse
     * @throws ClientExceptionInterface
     */
    public function send(): MailgunApiSendResponse
    {
        /** @var Mailgun $mailgun */
        $mailgun = $this->getMailgunApiClient()->create();

        /** @var array[] $params */
        $params = [
            "from" => $this->getMailgunApiClient()->getFromEmail(),
            "to" => $this->getMailgunApiModel()->getEmailAddress(),
            "subject" => $this->getMailgunApiModel()->getTitle(),
            "text" => $this->getMailgunApiModel()->getContent(),
            "o:deliverytime" => Carbon::now()->adddays(3)->toRfc2822String(),
        ];

        /** @var SendResponse $sendResponse */
        $sendResponse = $mailgun->messages()->send($this->getMailgunApiClient()->getEmailDomain(), $params);

        /** @var array[] $data */
        $data = [
            "id" => $sendResponse->getId(),
            "message" => $sendResponse->getMessage()
        ];

        $this->getMailgunApiSendResponse()->setResponse($data);

        return $this->getMailgunApiSendResponse();
    }

Below is the current status as per Mailgun Log.

{
    "envelope": {
        "transport": "smtp",
        "sender": "Admin@sandboxf130aeded8d44db4b211ec6bb2488f14.mailgun.org",
        "targets": "gohar*****@gmail.com"
    },
    "storage": {
        "region": "us-west1",
        "env": "production",
        "key": "BAABAQYhT_85YerJ675CSDFASXxRjabYg==",
        "url": "https://storage-us-west1.api.mailgun.net/v3/domains/sandboxf130aeded8d44db4b211ec6bb2488f14.mailgun.org/messages/BAABAQYhT_85YerJ675CbouSEEXxRjabYg=="
    },
    "method": "HTTP",
    "log-level": "info",
    "recipient-domain": "gmail.com",
    "event": "accepted",
    "user-variables": {},
    "id": "984HKEiQRemVjMeFKoHyyw",
    "recipient": "gohar*****@gmail.com",
    "tags": null,
    "timestamp": 1684929274.7635095,
    "flags": {
        "is-test-mode": false,
        "is-authenticated": true
    },
    "message": {
        "size": 387,
        "headers": {
            "from": "Admin <Admin@sandboxf130aeded8d44db4b211ec6bb2488f14.mailgun.org>",
            "subject": "Live Test",
            "message-id": "20230524115434.bc162edb03872131@sandboxf130aeded8d44db4b211ec6bb2488f14.mailgun.org",
            "to": "gohar*****@gmail.com"
        },
        "scheduled-for": 1685188473
    },
    "originating-ip": "124.29.239.111"
}

"scheduled-for": 1685188473 shows that its going to be sent after 3 days.


Solution

  • Mailgun doesn't provide the option to delete mails.

    https://documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages:~:text=process%20this%20data.-,Deleting%20Stored%20Messages,-Stored%20messages%20are