因此,假如你有這樣的控制器操作:
public function sendAction()
{
// some logic
$this->sendConfirmationMail();
}
使用Soflomo \ Mail和SlmMail,你可以同時連結以下兩個途徑。
首先,SlmMail提供運輸。有一個Mandrill運輸,所以先按照installation instructions。如果您複製了配置並設置了憑據,則可以使用名爲SlmMail\Mail\Transport\MandrillTransport
的服務。
private function sendConfirmationMail()
{
$mail = new \Zend\Mail\Message;
$transport = $this->getServiceLocator()
->get('SlmMail\Mail\Transport\MandrillTransport');
$transport->send($mail);
}
很明顯,這不是最好的方法。最好的是與dependency injection instead of the service locator pattern合作。還有Soflomo\Mail
來幫助完成此設置。
然後,您將Mandrill傳輸附加爲Soflomo \ Mail傳輸。在您的配置中:
'service_manager' => [
'aliases' => [
'Soflomo\Mail\Transport' => 'SlmMail\Mail\Transport\MandrillTransport',
],
],
這樣,如果Soflomo \ Mail加載傳輸,它會從SlmMail加載傳輸。接下來,Soflomo \郵件提供了一個門面的服務,你可以加載到你的控制器:
use Soflomo\Mail\Service\MailServiceInterface;
class RegistrationController extends AbstractActionController
{
private $service;
public function __construct(MailServiceInterface $service)
{
$this->service = $service;
}
public function sendAction()
{
// some logic
$this->sendConfirmationMail();
}
private function sendConformationMail()
{
$this->service->send([
/* here all your options*/
]);
}
}
剩下的唯一的事情就是在控制器中注入服務。這在ZF2中是相當標準的程序,在這裏我不會解釋。如果您需要此信息,則可在ZF2 manual中完美解釋。只記得你想注入的服務叫做。
https://packagist.org/packages/netglue/zf2-mandrill-module –