【问题标题】:Swift Mailer and Symfony2 - no mail in the spoolSwift Mailer 和 Symfony2 - 假脱机中没有邮件
【发布时间】:2013-01-20 15:42:36
【问题描述】:

从官方 Swift Mailer 组交叉发布....

我正在尝试使用 Swift Mailer 包和文件假脱机发送电子邮件,但是当我告诉控制台发送邮件时,它告诉我假脱机中有 0 条消息。假脱机应该由 Swift Mailer 自动创建,还是我需要手动创建文件?因为我在任何地方都看不到假脱机文件。

我的 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"

我如何发送邮件:

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('mail@mysite.com')
                    ->setTo('me@myrealaddress.com')
                    ->setBody($data['message']);

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

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

我做错了什么?根据 Symfony 文档,这应该可以工作。

【问题讨论】:

  • 如果将 spool conf 更改为 path: "%kernel.cache_dir%/swiftmailer/spool" 会怎样? (之后不要忘记清除缓存)
  • 太棒了,成功了!有点希望文档有那个而不是"%kernel.root_dir%/spool"。让它成为一个答案,我会投票/标记为已解决。
  • 我想确定您的问题是什么:如果您放回%kernel.root_dir%/spool 并在您的 Symfony 根文件夹中手动创建目录并授予运行您的 Web 服务器的用户完全权限它是否有效?
  • 我测试过了,好像真的是权限问题。

标签: php symfony swiftmailer


【解决方案1】:

在标准安装中,path: "%kernel.root_dir%/spool" 等同于app/spool,并且要在此目录中创建和写入,运行您的 Web 服务器的用户需要特定的权限。

但是,如果您不想更改目录的权限,您始终可以使用 cache 文件夹并进行以下配置:

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

请注意,这样做时,您在清除缓存时必须非常小心,因为如果您有未发送的电子邮件,它们将与其他缓存文件一起消失。

【讨论】:

  • 很抱歉没有回复您之前的建议。我必须弄清楚权限。我在共享主机上,所以我的选择有点有限。
  • 嗯...仔细观察告诉我,在我的初始测试期间,昨晚在应用程序 (app/spool) 中创建了一个假脱机目录 。但由于某种原因,没有电子邮件发送给它....嗯。我猜需要更多的测试。我真的很喜欢它,所以如果我能提供帮助,我的缓存中不在中。
  • 如何继续设置app/cache 文件夹的权限?你能在app/spool 上做同样的事情吗?
  • 现在app/spool 中的线轴工作正常。 O_o 我有一种感觉,在我昨晚的匆忙中,我忘记在我的测试中设置--env=prod 标志。核对缓存中的线轴后,我仔细检查了“正确”线轴,并验证其中 一条消息,并且我 能够通过控制台发送它.所以,看起来一切正常。谢谢你的帮助! :)
猜你喜欢
  • 1970-01-01
  • 2017-12-13
  • 1970-01-01
  • 1970-01-01
  • 2011-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多