Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6189983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:30:25+00:00 2026-05-24T02:30:25+00:00

I asked a question about that the action.class.php is getting very huge. There were

  • 0

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');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:30:25+00:00Added an answer on May 24, 2026 at 2:30 am

    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

    public static function mail($options, $mailer) {
    
        sfProjectConfiguration::getActive()->loadHelpers('Partial');
    
        $required = array('subject', 'parameters', 'to_email', 'to_name', 'html', 'text');
        foreach ($required as $option)
        {
          if (!isset($options[$option])) {
            throw new sfException("Required option $option not supplied to ef3Actions::mail");
          }
        }
    
        $address = array();
        if (isset($options['from_name']) && isset($options['from_email'])) {
          $address['fullname'] = $options['from_name'];
          $address['email'] = $options['from_email'];
        } else
          $address = self::getFromAddress();
    
    
        if(!isset($options['body_is_partial']) || $options['body_is_partial']==true ){
    
          $message = Swift_Message::newInstance()
              ->setFrom(array($address['email'] => $address['fullname']))
              ->setTo(array($options['to_email'] => $options['to_name']))
              ->setSubject($options['subject'])
              ->setBody(get_partial($options['html'], $options['parameters']), 'text/html')
              ->addPart(get_partial($options['text'], $options['parameters']), 'text/plain');
    
        } else {
    
          $message = Swift_Message::newInstance()
              ->setFrom(array($address['email'] => $address['fullname']))
              ->setTo(array($options['to_email'] => $options['to_name']))
              ->setSubject($options['subject'])
              ->setBody($options['html'], 'text/html')
              ->addPart($options['text'], 'text/plain');
    
        }
    
        if (isset($options['cc']) && !is_null($options['cc'])) {
          if (is_array($options['cc'])) {
            foreach ($options['cc'] as $key => $value) {
              if (!is_number($key))
                $message->addCc($key, $value);
              else
                $message->addCc($value);
            }
          } elseif (is_string($options['cc'])) {
            $message->addCc($options['cc']);
          }
        }
    
        if (isset($options['bcc']) && !is_null($options['bcc'])) {
          if (is_array($options['bcc'])) {
            foreach ($options['bcc'] as $key => $value) {
              if (!is_number($key))
                $message->addBcc($key, $value);
              else
                $message->addBcc($value);
            }
          } elseif (is_string($options['bcc'])) {
            $message->addBcc($options['bcc']);
          }
        }
    
        if (isset($options['attachments'])) {
          $atts = $options['attachments'];
          if (is_array($atts)) {
            foreach ($atts as $att) {
              $message->attach(Swift_Attachment::fromPath($att));
            }
          } elseif (is_string($atts)) {
            $message->attach(Swift_Attachment::fromPath($atts));
          }
        }
    
        try
        {
          return $mailer->send($message);
        } catch (Exception $e)
        {
          throw new Exception("Erro ao tentar enviar um email: " . $e->getMessage());
        }
    
      }
    

    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

      // SEND EMAIL FROM ACTION
    $opts = array();
    $opts['from_name'] = 'Webmaster';
    $opts['from_email'] = 'from@email.com';
    
    $variables = array();
    // variables passed to the partial template
    $variables['%client_firstname%'] = $firstName;
    $variables['%client_lastname%'] = $lastName;
    $variables['%client_email%'] = $client_email;
    // preenche opcoes de envio
    $opts['parameters'] = array();
    $opts['body_is_partial'] = false;
    $opts['to_name'] = $variables['%client_firstname%'] . " " . $variables['%client_lastname%'];
    $opts['to_email'] = $variables['%client_email%'];
    $opts['subject'] = __($subject, $variables);
    $opts['html'] = __($message_html, $variables);
    $opts['text'] = __($message_text, $variables);
    if (isset($cc)) $opts['cc'] = $cc;
    $bcc = sfDoctrineKeyValueProjectStore::get("{$contexto}_message_bcc");
    if (isset($bcc)) $opts['bcc'] = $bcc;
    
    return dcActions::mail($opts, sfContext::getInstance()->getMailer());
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently asked a question about IIf vs. If and found out that there
I recently asked a question about functional programming, and received (good!) answers that prompted
I asked that question about PHP breaking. I need stop execution of php process
I recently asked a question about tracing Linq-to-Entities I think that one of the
I asked a question here about an Linq error that results from mixing Linq-To-SQL
I asked a similar question about this previously, but I did not specify that
I asked a question about different testing frameworks yesterday. This question can be found
I just asked a question that helps about using generics (or polymorphism) to avoid
I asked a question yesterday about a problem that Im having with a program
I asked a question yesterday about namespaces that got answered, and so I tarried

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.