<p>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.</p>
<p>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.</p>
<p>my code:</p>
<pre><code>Event::on(Orders::class, Orders::EVENT_AFTER_PROCESS_WEBHOOK, function(WebhookEvent $e) {
                $data = $e-&gt;stripeData;
                $order = $e-&gt;order;

                if($order) {
                    $user = Craft::$app-&gt;getUsers()-&gt;getUserByUsernameOrEmail($order-&gt;email);
                    
                    if($user) {
                        switch ($data['type']) {
                            case 'checkout.session.completed':
                                $subscription = $order-&gt;getSubscription();
                                $oldGroups = $user-&gt;getGroups();
                                if($user-&gt;isInGroup('newUsers')) {
                                    $newUserGroup = [];
                                    Craft::$app-&gt;getUsers()-&gt;assignUserToGroups($user-&gt;id, $newUserGroup);
                                }
                                if($subscription-&gt;planNickName == 'Light') {
                                    $newGroups = [1];
                                }
                                if($subscription-&gt;planNickname == 'Standard') {
                                    $newGroups = [2];
                                }
                                if($subscription-&gt;planNickName == 'Premium') {
                                    $newGroups = [3];
                                }
                                //send mail
                                $message = (new Message())
                                    -&gt;setSubject('A subscription has been completed!')
                                    -&gt;setHtmlBody('....')
                                    -&gt;setTo('email');
                                Craft::$app-&gt;getMailer()-&gt;send($message);
                                Craft::$app-&gt;getUsers()-&gt;assignUserToGroups($user-&gt;id, $newGroups);
                                break;
                        }
                    }
                }
            });
</code></pre>

CC BY-SA 4.0
2022-08-09T12:52:41.127Z
anyone has done this before? I need an example, which I'm somehow not able to find.
aren
CC BY-SA 4.0
2022-08-09T13:32:49.537Z
Can you edit your question to say what the actual problem is? Does your code not work? Do you get any error messages?
James Smith
CC BY-SA 4.0
2022-08-10T07:12:50.517Z
@JamesSmith done.
aren
CC BY-SA 4.0
2022-08-10T10:14:21.483Z
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`: https://github.com/craftcms/commerce-stripe#receivewebhook - not sure if that might help at all.
James Smith
CC BY-SA 4.0
2022-08-10T10:21:23.057Z
@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
<plugin-development><emails>
How to send e-mail on an event
# How to send e-mail on an event

(Asked by: username_0 on 2022-08-09)

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;
                        }
                    }
                }
            });

**Tags:** plugin-development, emails
> **Comments:**
> **username_0**: anyone has done this before? I need an example, which I'm somehow not able to find.  
> **username_1**: Can you edit your question to say what the actual problem is? Does your code not work? Do you get any error messages?  
> **username_0**: @JamesSmith done.  
> **username_1**: 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`: https://github.com/craftcms/commerce-stripe#receivewebhook - not sure if that might help at all.  
> **username_0**: @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.  

---

## 0 answers

