Yii Framework Forum: Appropriate place to put email sending methods - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Appropriate place to put email sending methods

#1 User is offline   edx 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 23-March 17

Posted 24 November 2017 - 01:13 AM

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?
0

#2 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,493
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 24 November 2017 - 02:18 AM

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();

0

#3 User is offline   edx 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 23-March 17

Posted 27 November 2017 - 04:10 PM

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

#4 User is offline   jkofsky 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 688
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 27 November 2017 - 08:56 PM

You can look at the code for the advanced template, which has the first two of your requirements.
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#5 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,493
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 28 November 2017 - 12:44 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users