Swiftmailer remove attachment after send

前端 未结 3 1976
情深已故
情深已故 2021-01-15 03:53

I am trying to remove the attachment files after sending an email with Symfony 2.1 and Swiftmailer but if I delete the file before returnin

3条回答
  •  被撕碎了的回忆
    2021-01-15 04:03

    In order to complete the very good answer of james_t, if you use multiple mailers some changes are needed.

    Replace

    //  Default mailer
    $mailer = $this->container->get('mailer');
    
    $subject  = '...';
    $from     = '...';
    $to       = '...';
    $body     = '...';
    
    $message = \Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom($from)
        ->setTo($to)
        ->setBody($body, 'text/html')
    ;
    
    //  Put e-mail in spool
    $result = $mailer->send($message);
    
    //  Flush spool queue
    $transport      = $mailer->getTransport();  
    $spool          = $transport->getSpool();
    $realTransport  = $this->container->get('swiftmailer.transport.real')
    $spool->flushQueue($realTransport);
    

    By

    //  Custom mailer
    $mailerServiceName  = 'myCustomMailer';
    $customMailer       = $this->container->get("swiftmailer.mailer.".$mailerServiceName);
    
    $subject  = '...';
    $from     = '...';
    $to       = '...';
    $body     = '...';
    
    $message = \Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom($from)
        ->setTo($to)
        ->setBody($body, 'text/html')
    ;
    
    //  Put e-mail in spool
    $result = $customMailer->send($message);
    
    //  Flush spool queue
    $transport      = $customMailer->getTransport();  
    $spool          = $transport->getSpool();
    $realTransport  = $this->container->get('swiftmailer.mailer.'.$mailerServiceName.'.transport.real');
    $spool->flushQueue($realTransport);
    

提交回复
热议问题