LucasCCS / postal_server_api_transport_for_mautic

4 stars 5 forks source link

Events (Bounced and Complaints) are not processed #2

Closed IonutOjicaDE closed 11 months ago

IonutOjicaDE commented 1 year ago

The MessageDeliveryFailed and MessageBounced Webhooks (actually no Webhook) are recognized by the plugin.

I suppose that the actual Postal version 2.1.4 is using another encription and the function does not process the encription right:

public function processCallbackRequest(Request $request): void

Pacerino commented 12 months ago

@IonutOjicaDE I ran into a similar problem.

  1. emails were formatted incorrectly
  2. bounces and complaints did not arrive and were not assigned to the contact.

I therefore forked the repo and made changes. In today's mass mailing to approx. 900 contacts, the bounces and complaints were processed perfectly and stored in Mautic.

Take a look https://github.com/Pacerino/MauticPostalServerBundle

IonutOjicaDE commented 12 months ago

@Pacerino Thank you ! Very much ! I have 2 points:

1. I changed the code to add more details received from Postal.

Your code:

    /**
     * Handle bounces & complaints from Postal.
     */
    public function processCallbackRequest(Request $request)
    {
        $postData = json_decode($request->getContent(), true);

        $event    = $postData['event'];
        $payload  = $postData['payload'];
        $message  = isset($payload['original_message']) ? $payload['original_message'] : $payload['message'];
        $email    = $message['to'];

        if ('MessageDeliveryFailed' == $event) {
            $this->transportCallback->addFailureByAddress($email, $this->translator->trans('mautic.email.bounce.reason.other'));
        } elseif ('MessageBounced' == $event) {
            $this->transportCallback->addFailureByAddress($email, $this->translator->trans('mautic.email.bounce.reason.hard_bounce'));
        }
    }

Changed to:

    /**
     * Handle bounces & complaints from Postal.
     From: https://github.com/Pacerino/MauticPostalServerBundle

     as an example for transportCallback->addFailureByAddress see: https://forum.mautic.org/t/webhook-postmark-support-management-of-bounces-spam-and-unsubscription/25799/2?u=ionutojicade
     */
    public function processCallbackRequest(Request $request): void
    {
        $postData = json_decode($request->getContent(), true);

        $event    = $postData['event'];
        $payload  = $postData['payload'];
        $message  = isset($payload['original_message']) ? $payload['original_message'] : $payload['message'];
        $email    = $message['to'];

        if ('MessageDeliveryFailed' == $event || 'MessageBounced' == $event) {
            $output = ('MessageDeliveryFailed' == $event) 
                ? $this->translator->trans('mautic.email.bounce.reason.other') 
                : $this->translator->trans('mautic.email.bounce.reason.hard_bounce');
            $output .= ": " . $payload['output'] . "[Postal: " . $payload['details'] . "]";
            //error_log("output: " . $output . "\n", 3, "/var/www/html/m/var/logs/postal.log");
            $this->transportCallback->addFailureByAddress($email, $output, DoNotContact::BOUNCED);
        }
    }

2. Security

We should implement some sort of authentication, otherwise anyone can send Mautic this type of webhook. I found an example that works with the current version of Postal, but I do not know how to implement it in Mautic:

WebhookController from laravel-postal

Pacerino commented 11 months ago

@IonutOjicaDE Thank you for your contribution!

I've implemented the Webhhok signature checker. Could you try it? My understanding is that you need to add the public key under the Config.php https://github.com/Pacerino/MauticPostalServerBundle/blob/master/Config/config.php#L45

  1. Go to your config folder of postal, e.g. /opt/postal/config
  2. Get the public key from signing.key: openssl rsa -in signing.key -pubout
  3. Copy the part between BEGIN PUBLIC KEY and END PUBLIC KEY into the above config

Example:

'parameters' => [
        'mailer_postal_webhook_signing_key' => 'MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgF4w4tegq4rIWjPddTTaOCG6SMQx1MXbD4s5Wxx1x+Ao761eqeCGinmxy0wIGMMux21EGkr9IkMeU2dtCopMPFuMsb8hTZs7DhbfQ7BPttp7CxrzDhgEEVJOMrTl9KbDWYvSUozPHOLASLQwlJXuLrYj3leDNL2nui4J8x+mFzAgMBAAE=',
    ],

I haven't tested it yet. I've ran into other problems the last few days and had to disable the postal server unil i've got the problems fixed. But I wanted to offer you a possible solution.

IonutOjicaDE commented 11 months ago

@Pacerino line 93 gives an error:

$encodedSignature = $request->header('x-postal-signature');

( LINK to the github code )

This is the error:

mautic.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Attempted to call an undefined method named "header" of class "Symfony\Component\HttpFoundation\Request"." ...

I changed the line 93 to:

$encodedSignature = $request->headers->get('X-POSTAL-SIGNATURE', '');

And I get the error:

Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\HttpException: "Wrong signature" ...

I do not know further... It should work, but I do not understand why it does not.

Pacerino commented 11 months ago

@IonutOjicaDE

Thank you for the fix with the header! I'll push it to the repo in a minute. For the signature it was my fault. The webhook_signing_key is the part from the DKIM.

So instead of generating the PublicKey with OpenSSL, you can take the part from the dkim.

  1. Run on the Server postal default-dkim-record
  2. Take the Key after the p= part
  3. Paste it into the config

I've ran a small test and it seems to work.

grafik

More to Read: https://github.com/postalserver/postal/issues/432#issuecomment-404172757 https://github.com/postalserver/postal/issues/432#issuecomment-353143578

IonutOjicaDE commented 11 months ago

Great ! Super ! I implement it and it works indeed :) I am so glad. You are amazing !!! I also searched Postal forum, but not found or did not understand.

Now it should be implemented also here, in the github code.

Should it be before or after the issue is closed? For me, the issue is successfully solved.

fanpero87 commented 6 months ago

Hi, Im using Postal 3.3.3 and Mautic 4.4.12 and I haven't been able to make the bounce emails work. Im not sure what additional configuration I need to do to make it work.

IonutOjicaDE commented 6 months ago

Can you use this one, as I have made the proper changes as I use the plugin:

https://github.com/IonutOjicaDE/MauticPostalServerBundle

fanpero87 commented 6 months ago

Hi, I just cloned your fork onto Mautic and it installed Postal - api but on your README file you mentioned that it suppose to be SMTP. Also, as soon as I provided the API key and click on save, it breaks Mautic.

What can I do?

Thanks again for the time helping me.

stfnet commented 4 months ago

I have tried to install this plugin but as soon as I do mautic:plugins:reload mautic breaks There are too many redirect in the web server, trying to check logs I can't spot any interesting errors...just a lot of http requests

Mautic 4.4.12 Postal 3.3.4

any idea ?