<p>Rather than assembling the email yourself, you can just call the <a href="https://docs.craftcms.com/api/v3/craft-services-users.html#method-sendactivationemail" rel="nofollow noreferrer">sendActivationEmail</a> method in Craft:</p>
<pre><code>if ($event-&gt;isNew) {
    Craft::$app-&gt;getUsers()-&gt;sendActivationEmail($user);
}
</code></pre>
<p>That will automatically generate the activation link for you.</p>

CC BY-SA 4.0
2021-07-12T14:52:49.460Z
2021-07-12T14:52:49.460Z
Martin Spain
<p>If you want full control on the activation email site/language/template you just can't rely on sendActivationEmail(). You can do this instead:</p>
<pre><code>use craft\mail\Message;

// get activation link
$verifyUrl = Craft::$app-&gt;users-&gt;getEmailVerifyUrl($user);

// create email
$msg = new Message();
$msg-&gt;setTo($user-&gt;email);
$msg-&gt;setSubject('Activate your account');

// compose the HTML body where you will output verifyUrl link
$template = Craft::$app-&gt;getView()-&gt;renderTemplate('path/to/template', [
   'user' =&gt; $user,
   'verifyUrl' =&gt; $verifyUrl
]); 
$msg-&gt;setHtmlBody($template);

// finally send the email
$msg-&gt;send();

</code></pre>

CC BY-SA 4.0
2021-07-20T07:54:29.777Z
2021-07-20T07:54:29.777Z
gioppe
<p>On my site a type of user can add new clients and I wanted to automate the activation email, so that when a new user is added then the client will get the activation email.
But I'm getting an error that the activation $link is missing any idea how to get that parameter? This is what I have so far.</p>
<pre><code>

Craft::$app-&gt;elements-&gt;on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) {

      if ($event-&gt;element instanceof \craft\elements\User) {
        
        $user = $event-&gt;element;
       

        if($event-&gt;isNew) {
          
          Craft::$app
          -&gt;getMailer()
          -&gt;composeFromKey('after_activate_user')
          -&gt;setTo($user)
          -&gt;send();
        }
      }
    });

</code></pre>
<p>Digging through some code I saw that you can pass a link parameter like this:</p>
<pre><code> -&gt;composeFromKey('after_activate_user', ['link' =&gt; Template::raw($link)])
</code></pre>
<p>But not sure where I can retrieve this $link</p>
<p>Any help is appreciated.</p>
<p>Thanks!</p>

<craft3><modules><craft-pro>
Get System Activation email link
# Get System Activation email link

(Asked by: username_0 on 2021-07-12)

On my site a type of user can add new clients and I wanted to automate the activation email, so that when a new user is added then the client will get the activation email.
But I'm getting an error that the activation $link is missing any idea how to get that parameter? This is what I have so far.


Craft::$app->elements->on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) {

      if ($event->element instanceof \craft\elements\User) {
        
        $user = $event->element;
       

        if($event->isNew) {
          
          Craft::$app
          ->getMailer()
          ->composeFromKey('after_activate_user')
          ->setTo($user)
          ->send();
        }
      }
    });


Digging through some code I saw that you can pass a link parameter like this:
 ->composeFromKey('after_activate_user', ['link' => Template::raw($link)])

But not sure where I can retrieve this $link
Any help is appreciated.
Thanks!

**Tags:** craft3, modules, craft-pro


---

## 2 answers

### Answer by username_1 on 2021-07-12. Score: 2

Rather than assembling the email yourself, you can just call the sendActivationEmail method in Craft:
if ($event->isNew) {
    Craft::$app->getUsers()->sendActivationEmail($user);
}

That will automatically generate the activation link for you.


---

### Answer by username_2 on 2021-07-20. Score: 2

If you want full control on the activation email site/language/template you just can't rely on sendActivationEmail(). You can do this instead:
use craft\mail\Message;

// get activation link
$verifyUrl = Craft::$app->users->getEmailVerifyUrl($user);

// create email
$msg = new Message();
$msg->setTo($user->email);
$msg->setSubject('Activate your account');

// compose the HTML body where you will output verifyUrl link
$template = Craft::$app->getView()->renderTemplate('path/to/template', [
   'user' => $user,
   'verifyUrl' => $verifyUrl
]); 
$msg->setHtmlBody($template);

// finally send the email
$msg->send();


---

