2

I want to send a welcome email to the user when they sign up (not an activation email).

I can see the email and edit its text in the Control Panel, but the email doesn't send when a new user registers.

Here is my code:

Event::on(SystemMessages::class, SystemMessages::EVENT_REGISTER_MESSAGES, function(RegisterEmailMessagesEvent $event) {
    $event->messages[] = [
        'key' => 'after_activate_user',
        'heading' => 'When someone registers:',
        'subject' => 'Subject',
        'body' => 'Message text',
    ];
});

My question, is there anything else I need to do to get this email to send? Do I need to define a recipient?

1 Answer 1

2

This will only register your message with Craft. To then send it you would do something like this:

Event::on(
    Elements::class, 
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(Event $event) {
        if ($event->element instanceof \craft\elements\User) {
            $user = $event->element;
            if($event->isNew) {
                Craft::$app
                    ->getMailer()
                    ->composeFromKey('after_activate_user')
                    ->setTo($user)
                    ->send();
            }
        }
    }
);

This will listen for the EVENT_AFTER_SAVE_ELEMENT event, make sure it's a User being saved, check that it's a new user then send the message.

1
  • Where should this be setup? New to craft here and looking for something similar to functions.php like in wp :) Commented Jun 22, 2021 at 16:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.