swiftmailer动态附加Symfony

时间:2017-07-21 11:41:07

标签: php html symfony sonata-admin swiftmailer

我有一个小问题我尝试创建一个模板,允许在客户信息中打印标准字母。我已经完成了批量操作以发送有效的电子邮件,但是当我尝试发送此邮件时,它无法正常工作,附件一侧必定存在问题。我目前在Symphony 3.3上使用Sonata Admin。所以在接收这封邮件时我们可以打开附件html,然后打印所述页面html。你认为它能起作用吗?怎么办才能让它发挥作用?这个解决方案可行吗?我没有看到任何其他.... 你可以在那里找到我的代码:

ClientAdmin.php:

public function configureBatchActions($actions)
{
if (
    $this->hasRoute('edit') && $this->hasAccess('edit') &&
    $this->hasRoute('delete') && $this->hasAccess('delete')
) {
    $actions['send_mail'] = [
        'label' => 'Send E-mail',
        'ask_confirmation' => true
    ];
}

{
$actions['send_letter'] = [
    'label' => 'Send Letter',
    'ask_confirmation' => true
];
}

return $actions;

ClientAdminController.php:

class ClientAdminController extends BaseController
{


public function batchActionSendLetter(ProxyQueryInterface $query)
{
    $this->admin->checkAccess('batchDelete');

    try {
        $clients = $query->execute();

        $this->sendLetter($clients);

        $this->addFlash('sonata_flash_success', 'E-mail send');
    } catch (ModelManagerException $e) {
        $this->handleModelManagerException($e);
        $this->addFlash('sonata_flash_error', 'Error');
    }

    return new RedirectResponse($this->admin->generateUrl(
        'list',
        ['filter' => $this->admin->getFilterParameters()]
    ));
}

public function sendLetter($clients)
{


    foreach ($clients as $client) {
        $data = \Swift_Attachment::fromPath('attach.html.twig', 'application/html');
}
        $message = \Swift_Message::newInstance()
            ->setSubject("Test")
            ->setFrom(' mymail')
            ->setTo('mymail@')
            ->setBody(
                $this->renderView('sendmail.html.twig',
                    array('client' => $client)),
                'text/html');
        $message->attach($data);

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


    return $this->redirect('/admin/dashboard');

}

}

service.yml:

admin.clients:
    class: AppBundle\Admin\ClientAdmin
    tags:
        - { name: sonata.admin, manager_type: orm,group : Register, label: Clients, show_mosaic_button: false}
    arguments:
        - ~
        - AppBundle\Entity\Client
        - AppBundle:ClientAdmin
    calls:
        - [ setTemplate, ['edit', "print.html.twig"]]

attach.html.twig:

<script>
var win = window.open();
var output = '';
var print = function() {
        output =
            '<div> Dear ' +
            'this is a letter to you.<br/>' +
            'Yours sincerely, .... '+
            '{{ client.surmane }} <br/>' +
            '{{ client.address }}</div>'+
            '<div style="page-break-after:always"></div>';
    win.document.write(output);
}
print();

因此主要问题出在控制器中,在fucntion sendletter中。我必须在$data中连接html文件,但我不知道该怎么做。 在此先感谢,如果您还需要其他任何问题我会尽力给您

2 个答案:

答案 0 :(得分:1)

您是否查看了SwiftMailer文档:Attaching Dynamic Content

我认为你必须渲染文件“attach.html.twig”并使用Swift_Attachment中的内容:

public function sendLetter($clients)
{
    foreach ($clients as $client) {
        $htmlContent = $this->renderView('attach.html.twig', array('clicent' => $client);
        $data = \Swift_Attachment::fromPath($htmlContent, 'letter.html', 'application/html');
        $message = \Swift_Message::newInstance()
            ->setSubject("Test")
            ->setFrom(' mymail')
            ->setTo('mymail@')
            ->setBody(
                $this->renderView('sendmail.html.twig',
                    array('client' => $client)),
                'text/html');
        $message->attach($data);

        $this->get('mailer')->send($message);
    }

    return $this->redirect('/admin/dashboard');

}

答案 1 :(得分:1)

所以我可能找到了一个解决方案,当你说你可以创建和下载而不是通过电子邮件发送它时,我考虑下载.xml中的列表然后使用excel和word之间的脚本来使用excel数据字。您可以找到文档there。这不是最好的解决方案,但它仍然是一个解决方案。如果有人有想法,我还在等待最好的一个。