Major Productions
Major Productions

Reputation: 6062

Swift Mailer and Symfony2 - no mail in the spool

Cross-posted from the official Swift Mailer group....

I'm trying to send emails using the Swift Mailer bundle and a file spool, but when I tell the console to send the mail, it tells me there are 0 messages in the spool. Is the spool supposed to be created by Swift Mailer automatically, or do I need to manually create a file? Because I don't see a spool file anywhere.

My config.yml:

# Swiftmailer Configuration
swiftmailer:
    transport: sendmail
    host:      /usr/bin/sendmail
    username:  %mailer_user%
    password:  %mailer_password%
    spool:
        type: file
        path: "%kernel.root_dir%/spool"

How I'm sending the mail:

public function contactAction(Request $request)
{
    $form = $this->createFormBuilder()
        ->add('name', 'text', array('label' => 'Name:'))
        ->add('email', 'email', array('label' => 'Email Address:'))
        ->add('subject', 'text', array('label' => 'Subject:'))
        ->add('message', 'textarea', array('label' => 'Message:'))
        ->getForm();

    if ($request->isMethod('POST')) {
        $form->bind($request);
        $data = $form->getData();

        if (!empty($data['name'])) {
            $data['message'] = str_replace("\n.", "\n..", $data['message']);

            $emailValidator = new Email();
            $emailValidator->message = "Invalid email address";

            $error = $this->get('validator')->validateValue($data['email'], $emailValidator);

            if (count($error) == 0) {
                $mail = \Swift_Message::newInstance()
                    ->setSubject($data['subject'])
                    ->setFrom('[email protected]')
                    ->setTo('[email protected]')
                    ->setBody($data['message']);

                $this->get('mailer')->send($mail);

                return $this->redirect($this->generateUrl('_success'));
            } else {
                return $this->redirect($this->generateUrl('_failure'));
            }
        }
    }

What am I doing wrong? According to the Symfony docs, this should be working.

Upvotes: 3

Views: 6121

Answers (1)

cheesemacfly
cheesemacfly

Reputation: 11762

In a standard installation, path: "%kernel.root_dir%/spool" is equivalent to app/spool and to create and write inside this directory, the user running your web server needs the specific permissions.

However, if you don't want to change the permissions on your directory, you can always use the cache folder with the following configuration:

# Swiftmailer Configuration
swiftmailer:
    transport: sendmail
    host:      /usr/bin/sendmail
    username:  %mailer_user%
    password:  %mailer_password%
    spool:
        type: file
        path: "%kernel.cache_dir%/swiftmailer/spool"

Please note that by doing so, you have to be really careful when you clear your cache because if you have unsent emails, they will vanish with the other cache files.

Upvotes: 4

Related Questions