Appropriate place to put email sending methods

Hey forum,

So I have to make available sending 3 types of emails:

  1. With activation link after registration.

  2. Forgotten password.

  3. Notifications.

My question is where is the appropriate place to put those functions?

I’m thinking about to make them as 3 different methods. Is this the best way of doing it? If it is not, how must I proceed?

Also, where I can find and see good programming practices when I have such questions as the one above?

what I usually do if a have only few then model is where I usually put them, in case I have several then I create a directory called mailers each email gets a dedicated class for example you might have WelcomeMailer, or VerificationMailer.




<?php

namespace app\mailers;


class WelcomeMailer {

  public function send()

  {

  }

}


// some controller or model

$welcomeMailer = new app\mailers\WelcomeMailer;

$welcomeMailer->send();

Thanks for the answer, but why every mail type gets its own class? What’s your reason behind this?

You can look at the code for the advanced template, which has the first two of your requirements.

I put each mailer in a separate class because IMO it is more flexible approach, if my mailers get complicated i can break it down to several methods and test them, at the same time keep the logic in one place.