duanbei2914 2016-10-18 08:09
浏览 24
已采纳

Twig模板数据swiftmailer

Just set up an slim 3 application with twig and swiftmailer.

I made a MailController to fetch the emailaddress and name from a customers table - it works great and swiftmailer send an email to the right address - using the twig-template I created - but the problem is that it doesn't fetch the data that I have setup in twig.

The code for my controller is here

<?php

namespace Cart\Controllers;

use Slim\Router;
use Slim\Views\Twig;
use Cart\Models\Order;
use Cart\Models\Product;
use Cart\Models\Address;
use Cart\Models\Delivery;
use Cart\Models\Customer;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
 * @return MailController
 */
class MailController
{
    protected $transport;
    protected $mailer;
    private $view;

    public function __construct(Twig $view)
    {
        $this->view = $view;

        // Create Transport
        $https['ssl']['verify_peer'] = FALSE;
        $https['ssl']['verify_peer_name'] = FALSE;

        $this->transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
           ->setUsername(username)
           ->setPassword(password)
           ->setStreamOptions($https)
           ;
        // Create Mailer with our Transport.
        $this->mailer = \Swift_Mailer::newInstance($this->transport);

    }

    public function mailLisbeth($hash)
    {

        // // Here I'm fetching my email template from my template directory.
        $welcomeEmail = $this->view->fetch('mail/order.twig');

        $mailorder = Order::with('customer', 'address', 'products' )->where('hash', $hash)->first();

        // Setting all needed info and passing in my email template.
        $message = \Swift_Message::newInstance('Ordre fra Webshop')
                        ->setFrom(array('kent@grafiosaurerne.dk' => 'Kent'))
                        ->setTo(array(
                            $mailorder->customer->email => $mailorder->customer->name,
                        ))
                        ->setBody($welcomeEmail)
                        ->setContentType("text/html");
        // Send the message
        $results = $this->mailer->send($message);
    }
}

And my twig template her

  <div class="row">
    <div class="col-md-12">
      <h3>Order #{{ mailorder.order.id }}TEST</h3>
      <hr>
      <div class="row">
        <div class="col-md-6">
          <h4>Kunde:</h4>
          {{ mailorder.customer.name }}<br>
          {{ mailorder.customer.email }}<br>
          <h4>Leveringsadresse:</h4>
          {{ mailorder.address.address1 }}<br>
          {{ mailorder.address.address2 }}<br>
          {{ mailorder.address.city }}<br>
          {{ mailorder.address.postal_code }}<br>
          <h4>Leveringstidspunkt:</h4>
          {{ mailorder.delivery.delivery }}<br>
          {{ mailorder.delivery.deliverydate }}<br>
          {{ mailorder.delivery.deliverytime }}<br>

        </div>
        <div class="col-md-6"></div>
          <h4>Varer:</h4>
          {% for product in customer.products %}
            <a href="{{ path_for('product.get', {slug: product.ID })}}">{{ product.Navn }}</a> ( x {{ product.pivot.quantity}})<br>
          {% endfor %}
      </div>
      <hr>
      <p>
        Levering: DKK 150,00 <br>
        <strong>Order total: DKK {{ order.total | number_format(2,',','.')}}</strong>
      </p>
    </div>
  </div>

The problem could lie in a missing passing of the mailorder variable to the twig template - but since I have used the same config and variable in another page - I can't figure out why it doesn't get into the twig template.

Hope someone has a clue... Thanks

  • 写回答

1条回答 默认 最新

  • douyanxing6054 2016-10-18 08:19
    关注

    As far as I can see, you are not rendering your template. Instead you just pass the twig file to swiftmailer.

    I am not sure how it works exactly in slim but you probably have to do something like

    $twig->render($welcomeEmail, ['mailorder' => $mailorder]);
    

    Then pass the result to swiftmailer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作