在我的数据库中,我存储了包含onError()
等个性化主题行的电子邮件模板。
系统用户应该能够编写包含Dear {{ firstname }} {{ lastname }}, thank you for registration!
,firstname
,lastname
等不同变量的新主题行,这些变量也存储在数据库中。
我不想使用其他问题中解释的salutation
函数来解决此问题。
这是我用来发送电子邮件的控制器:
template_from_string
我没有任何用于创建电子邮件的twig文件,因为它们是由sonata管理包生成的<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\Lead;
use AppBundle\Form\LeadType;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class RegisterController extends Controller
{
/**
* @Route("/{url}")
*/
public function submitFormAction($url, Request $request)
{
$frontend_vars = $this->getDoctrine()
->getRepository('AppBundle:Publisher')
->findOneByUrl($url);
if($frontend_vars == null) {
return $this->render('frontend/register_error.html.twig');
}
$lead = new Lead();
$form = $this->createForm('AppBundle\Form\LeadType', $lead, array(
'frontend_vars' => $frontend_vars
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Generate Unique User Hash
// $lead->setUniqueUserHash() = 'julius';
$em = $this->getDoctrine()->getManager();
$em->persist($lead);
$em->flush();
// Send Mail
$message = \Swift_Message::newInstance()
->setSubject($frontend_vars->getMailsubject())
->setFrom('$frontend_vars->getMailfrom()')
->setTo('me@mycompany.com')
->setBody($frontend_vars->getMailcontent()),
'text/html'
);
$this->get('mailer')->send($message);
return $this->redirectToRoute('lastPage', array('url' => $frontend_vars->getUrl()));
}
return $this->render('frontend/register.html.twig', array(
'content' => $frontend_vars,
'logos' => $logopath,
'form' => $form->createView(),
));
}
因此,在奏鸣曲管理包生成的表单字段中,用户应该从地址和内容中输入主题:
“亲爱的{{firstname}} {{lastname}},感谢您注册!”