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;
}
}
}
});
EVENT_RECEIVE_WEBHOOK
: github.com/craftcms/commerce-stripe#receivewebhook - not sure if that might help at all.