I asked a question about that the action.class.php is getting very huge. There were a lot of useful answers. You can read here: Multiple action.class.php
Because of that the action.class.php is getting big there might be a problem that there is to much stuff in it. Okay, I got the model, so e.g. I can put the queries into lib/model/doctrine/… and move couple forms.
What is the best way to move the action concerning emails in symfony! Where should I put the code? Is there a better way to send emails in symfony?
Thanks!
Craphunter
My example:
if ($this->form->isValid()) {
$formData = $this->form->getValues();
$firstname = $this->userdata->getFirstname();
$lastname = $this->userdate->getLastname();
try {
$message = Swift_Message::newInstance()
->setFrom('man@example.com')
->setTo($this->shopdata->getEmail())
->addReplyTo($this->userdata->getEmail())
->setSubject($formData['subject'])
->setBody($this->getPartial('emailcontact', $arguments = array(
'firstname' => $firstname,
'lastname' => $lastname,
'message' => $formData['message'],
)));
$this->getMailer()->send($message);
$this->getUser()->setFlash('shop_name', $formData['email']);
$this->getUser()->setFlash('name', $formData['name']);
} catch (Exception $e) {
$this->getUser()->setFlash('mailError', 'Technical Problem');
}
$this->redirect('aboutus/sendfeedbackthankyou');
I find it useful to put the sending code in a main class that extends sfActions. I usually throw there everything that is common used throughout the project (code for helping ajax use, etc).
Particularly for email, I have a static method
why is the method static? Well, I’ve also been using this method from another contexts like event listeners. A typical call for sending e-mail looks like
It also enforces the use of sending both versions of an email (html and text). With such a code you can also use partials to deliver email messages. I find it very useful.