Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony Swiftmailer:有条件地假脱机电子邮件或立即发送_Symfony_Email_Conditional Statements_Swiftmailer_Spool - Fatal编程技术网

Symfony Swiftmailer:有条件地假脱机电子邮件或立即发送

Symfony Swiftmailer:有条件地假脱机电子邮件或立即发送,symfony,email,conditional-statements,swiftmailer,spool,Symfony,Email,Conditional Statements,Swiftmailer,Spool,有没有人在Symfony设置过使用Swiftmailer假脱机电子邮件的条件? 我希望可以选择立即发送电子邮件,或者将其假脱机到文件中,具体取决于我正在运行的功能 我将电子邮件服务抽象到自己的包中,并在需要时调用其他包中的sendmail函数。但对于某些捆绑包/功能,我希望立即发送电子邮件,而对于其他捆绑包/功能,后台处理也可以。我考虑在sendEmail函数中使用spool参数,因此,如果在调用该函数时将该参数设置为true,电子邮件将被假脱机,如果设置为false,则会立即发送 或者一个简单

有没有人在Symfony设置过使用Swiftmailer假脱机电子邮件的条件? 我希望可以选择立即发送电子邮件,或者将其假脱机到文件中,具体取决于我正在运行的功能

我将电子邮件服务抽象到自己的包中,并在需要时调用其他包中的sendmail函数。但对于某些捆绑包/功能,我希望立即发送电子邮件,而对于其他捆绑包/功能,后台处理也可以。我考虑在sendEmail函数中使用spool参数,因此,如果在调用该函数时将该参数设置为true,电子邮件将被假脱机,如果设置为false,则会立即发送

或者一个简单的if条件就足够了?]

任何想法、技巧、经验等都会很棒

更新

在my config.yml中:

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:
          type: file
          path: /srv/http/test/spool
通过在参数中指定spool选项,Swiftmailers将使用一个实例,该实例将通过向队列发送消息而不是直接向世界发送消息来管理spool。通过传输对象,您可以访问Spool实例a或a,并强制Swiftmailer刷新队列

// custom function to send an email
// inject \Swift_Mailer like you normally would
public function sendMessage($name, \Swift_Mailer $mailer, $bypassSpool = false)
{
    $message = new \Swift_Message('Hello Email')
        ->setFrom(/* from */)
        ->setTo(/* to */)
        ->setBody(/* render view */);

    $mailer->send($message); // pushes the message to the spool queue

    if($bypassSpool) {
        $spool = $mailer->getTransport->getSpool()
        $spool->flushQueue(new Swift_SmtpTransport(
            /* Get host, username and password from config */
        ));
    }
}

Swiftmailer支持,您只需设置一个cron作业定期运行以清除假脱机,但如果我不希望我的电子邮件被假脱机呢?我只希望它们在某些情况下被假脱机。这就是我想对我的问题说的。这是可能的,你能展示一下Swift_Mailer实例是如何加载/配置的吗?是的,我将它添加到了我的问题@WilliamPerron:在我标记你的答案之前,有一个简单的问题:是否真的有必要将参数添加到新的传输实例中?我在没有参数的情况下尝试了它,结果成功了,但我想知道添加参数有什么区别?哦,还有一个问题:有没有办法只刷新当时创建的消息?因为当我现在刷新队列时,很明显,我的spool文件中的所有消息都会被发送出去。@sonja回答你的第一个问题:也许吧。我还没有尝试过它,并坚持使用官方文档中给出的答案,但如果它默认为config参数,我也不会感到惊讶。对于第二个问题,当然可以,但是每次都需要创建一个新的Swift\u FileSpool实例,因为更改保存路径的唯一方法是通过构造函数,然后创建一个新的Swift\u Mailer实例,因为一旦创建它,就无法更改它的传输initialized@sonja,在我看来,这样做的复杂性是不值得的。我会简单地使用MemorySpool,当内核终止时,即当请求结束时,它会刷新它的队列,这样它就不会阻塞请求了。非常感谢!我曾考虑为那个场合添加第二个spool文件,但找不到其他人已经这样做了。你知道这是否有效或如何有效吗?