使用twig和swiftmailer设置一个苗条的3应用程序。
我制作了一个MailController从客户表中获取电子邮件地址和名称 - 它运行良好,swiftmailer使用我创建的twig-template向正确的地址发送电子邮件 - 但问题是它无法获取我在树枝上设置的数据。
我的控制器的代码在这里
<?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);
}
}
我的树枝模板她
<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>
问题可能在于缺少将mailorder变量传递给twig模板 - 但由于我在另一个页面中使用了相同的配置和变量 - 我无法弄清楚为什么它不会进入树枝模板
希望有人有线索......谢谢
答案 0 :(得分:0)
据我所知,您没有渲染模板。相反,你只需将twig文件传递给swiftmailer。
我不确定它是如何工作的,但你可能需要做类似的事情
$twig->render($welcomeEmail, ['mailorder' => $mailorder]);
然后将结果传递给swiftmailer。