# Creating reusable emails

**URL:** <https://discourse.cakephp.org/t/creating-reusable-emails/6259>\
**Category:** Need Help\
**Created:** [June 14, 2019, 8:25am UTC](https://discourse.cakephp.org/t/creating-reusable-emails/6259 "2019-06-14T08:25:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [June 14, 2019, 8:25am UTC](https://discourse.cakephp.org/t/creating-reusable-emails/6259/1 "2019-06-14T08:25:00Z")

</div>

I am looking at this [https://book.cakephp.org/3.0/en/core-libraries/email.html#creating-reusable-emails](https://book.cakephp.org/3.0/en/core-libraries/email.html#creating-reusable-emails)

I follow the manual to create something like this:

```auto
<?php 
namespace App\Mailer;

use Cake\Mailer\Mailer;

class ArticleMailer extends Mailer
{
    public function implementedEvents()
    {
        return [
            'Model.afterSave' => 'onPost'
        ];
    }
    
    public function onPost(Event $event, EntityInterface $entity, ArrayObject $options)
    {
        if ($entity->isNew()) {
            $this->send('newarticle', [$entity]);
        }
    }
    
    public function newarticle($article)
    {
        $this
            ->setTo('info@gmail.com')
            ->setSubject(sprintf('New article posted %s', $article->name))
            ->setViewVars($article->toArray())
            ->setTemplate('newArticle');
    }
}

```

According to the manual this should work fine, but when adding a new article it does not get triggered? Am I missing some extra step I need to do that is not mentioned in the manual?

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [June 15, 2019, 5:29am UTC](https://discourse.cakephp.org/t/creating-reusable-emails/6259/2 "2019-06-15T05:29:08Z")

</div>

So I need to register the Mailer class on the model?

```auto
$articleMailer = new ArticleMailer();
$this->getEventManager()->on($ArticleMailer);
```

Now when I add an article it gets triggered but it is showing errors:

# Argument 1 passed to App\Mailer\ArticleMailer::onPost() must be an instance of App\Mailer\Event, instance of Cake\Event\Event given

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [June 28, 2019, 8:07pm UTC](https://discourse.cakephp.org/t/creating-reusable-emails/6259/3 "2019-06-28T20:07:00Z")

</div>

In your `ArticleMailer` implementation file, you need to `use Cake\Event\Event`, so that it knows the correct class in `onPost(Event $event`; as it stands, it expects to find that class in your existing namespace, and the thing that gets passed to it doesn’t match. You’ll also need to do similar things for `EntityInterface` and `ArrayObject`.
