Search code examples
phphtmlsymfonysonata-adminswiftmailer

swiftmailer dynamic attach Symfony


I have a small question I try to create a template that allows to print a standard letter with inside the client information. I have already done a batch action to send emails that work but when I try to send this mail it does not work there must be a problem on the side of the attachment. I currently use Sonata Admin on Symphony 3.3. So at the reception of this mail we can open the attachment html and then print the said page html. Do you think it can work? What to do to make it work? Is this solution viable? I do not see any other .... You can find my code there :

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();

So there the main problem is in the controller, in the fucntion sendletter. I have to concatenate html file in $data but I don't know how to do it. Thanks in advance, if you need anything else ask I will try to give it to you


Solution

  • Did you check SwiftMailer documentation : Attaching Dynamic Content

    I think you have to render the file "attach.html.twig" and use the content in 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');
    
    }