0

I was wondering how to send an E-mail after an event. In this example I'm using stripe payments and I want a mail to be send after a transaction was completed.

Below I have tried to send a mail when a user is subscribed, but I don't receive an e-mail (setTo with an actual email), I don't get any errors either.

my code:

Event::on(Orders::class, Orders::EVENT_AFTER_PROCESS_WEBHOOK, function(WebhookEvent $e) {
                $data = $e->stripeData;
                $order = $e->order;

                if($order) {
                    $user = Craft::$app->getUsers()->getUserByUsernameOrEmail($order->email);
                    
                    if($user) {
                        switch ($data['type']) {
                            case 'checkout.session.completed':
                                $subscription = $order->getSubscription();
                                $oldGroups = $user->getGroups();
                                if($user->isInGroup('newUsers')) {
                                    $newUserGroup = [];
                                    Craft::$app->getUsers()->assignUserToGroups($user->id, $newUserGroup);
                                }
                                if($subscription->planNickName == 'Light') {
                                    $newGroups = [1];
                                }
                                if($subscription->planNickname == 'Standard') {
                                    $newGroups = [2];
                                }
                                if($subscription->planNickName == 'Premium') {
                                    $newGroups = [3];
                                }
                                //send mail
                                $message = (new Message())
                                    ->setSubject('A subscription has been completed!')
                                    ->setHtmlBody('....')
                                    ->setTo('email');
                                Craft::$app->getMailer()->send($message);
                                Craft::$app->getUsers()->assignUserToGroups($user->id, $newGroups);
                                break;
                        }
                    }
                }
            });
5
  • anyone has done this before? I need an example, which I'm somehow not able to find.
    – aren
    Commented Aug 9, 2022 at 12:52
  • Can you edit your question to say what the actual problem is? Does your code not work? Do you get any error messages? Commented Aug 9, 2022 at 13:32
  • @JamesSmith done.
    – aren
    Commented Aug 10, 2022 at 7:12
  • Your code looks fine to me. For what it's worth, I'm using a different event from the Stripe gateway class to do something similar - EVENT_RECEIVE_WEBHOOK: github.com/craftcms/commerce-stripe#receivewebhook - not sure if that might help at all. Commented Aug 10, 2022 at 10:14
  • @JamesSmith I'm using the stripe payments plugin, that's probably why the event is different. It's weird because I'm not receiving any e-mail, anyways thanks for your answer.
    – aren
    Commented Aug 10, 2022 at 10:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.