Masoud Motallebipour
Masoud Motallebipour

Reputation: 414

Using swiftmailer in a repository class

I'm trying to send email in a repository class in symfony2 but I get this error:

Error:

Undefined method 'get'. The method name must start with either findBy or findOneBy! 

the code is :

public function sendMail()
{
    $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('[email protected]')
        ->setTo('[email protected]')
        ->setBody("Salam dadash kheili khosh omadi");
    $this->get('mailer')->send($message);
}

what should I change or do? tnx

Upvotes: 0

Views: 277

Answers (1)

Piotr Pasich
Piotr Pasich

Reputation: 2639

It's not good idea to do it from repository class. It should be moved to some service and called as an event or in the simplest way - from Controller. The repository class really do only database things.

In your case the issue is in $this->get('mailer'), because you are in repository you cannot access the containter. Move this part into controller class and everything will be all right.

Upvotes: 2

Related Questions