phpmysqldatabasesymfonyswiftmailer

SwiftMailer: How To Send Result Report Of Student To His Guardian


I'm trying to send result reports of student, and fees reminders to their parents. But the problem is that I don't understand how to get Daily Report one by one from DailyReport entity and email of their parents from Parent entity.

I spent hours to get it working but didn't get any success.

Because SwiftMailerBunlde only renders those twig templates which are in SchoolBundle/Resources/views/Default/email.html.twig and my twig templates are in schoolFinal/app/Resources/views/dailyreport/index.html.twig.

I tried by creating object of Student entity but it throws error that student does not exists in SchoolBundle/Resources/views/Default/email.html.twig.

        $message= \Swift_Message::newInstance()
        ->setSubject('Testing Mail')
        ->setFrom('test@gmail.com')
        ->setTo($student->getGuardian()->getEmail())
        ->setCharset('utf-8')
        ->setContentType('text/html')
        ->setBody($this->renderView('SchoolSMSBundle:student:dailyreport.html.twig'));

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

This is my dailyreport.html.twig file

<h1>Daily Report</h1>

<table>
    <tbody>
    <tr>
        <th>Id</th>
        <td>{{ dailyReport.id }}</td>
    </tr>
    <tr>
        <th>Total Marks</th>
        <td>{{ dailyReport.total }}</td>
    </tr>
    <tr>
        <th>Obtained</th>
        <td>{{ dailyReport.obtained }}</td>
    </tr>
    </tbody>
</table>

<ul>
    <li>
        <a href="{{ path('school_dailyReport_index') }}">Back to the list</a>
    </li>
    <li>
        <a href="{{ path('school_dailyReport_edit', { 'id': dailyReport.id }) }}">Edit</a>
    </li>
    <li>
        {{ form_start(delete_form) }}
        <input type="submit" value="Delete">
        {{ form_end(delete_form) }}
    </li>
</ul>

Solution

  • After passing the data in the renderView as below, it worked.

     ->setBody($this->renderView('SchoolSMSBundle:Default:dailyReport.html.twig', array('dailyReport'=>$dailyReport)));