SwiftMailer 调用未定义的方法
SwiftMailer calling an undefined method
我在尝试将 SwiftMailer 与 Symfony 结合使用时遇到错误。
我正在学习这里的教程:http://symblog.site90.net/docs/validators-and-forms.html
错误代码为:
FatalErrorException: Error: Call to undefined method Swift_Message::setMessage() in C:\xampp\htdocs\TP\src\TP\MainBundle\Controller\DefaultController.php line 327
我的操作是:
public function newAction()
{
$contacto = new Contacto();
$form = $this->createForm(new ContactoType(), $contacto);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$message = \Swift_Message::newInstance()
->setFrom('enquiries@myweb.com.ar')
->setTo($this->container->getParameter('tp_main.emails.contact_email'))
//this is the line 327 related with the error
->setMessage($this->renderView('TPMainBundle:Default:contactEmail.txt.twig', array('contacto' => $contacto)));
//
$this->get('mailer')->send($message);
$this->get('session')->setFlash('blogger-notice', 'Your contact enquiry was successfully sent. Thank you!');
return $this->redirect($this->generateUrl('contacto_new'));
}
}
return $this->render('TPMainBundle:Default:contact.html.twig', array(
'form' => $form->createView(),));}
错误说它是未定义的,但是,
我的方法 setMessage() 在我的 class Contacto.php 中定义 getter 和 setter :
/**
* @var string
*
* @ORM\Column(name="message", type="text")
*/
private $message;
/**
* Set message
*
* @param string $message
* @return Contacto
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}
/**
* Get message
*
* @return string
*/
public function getMessage()
{
return $this->message;
}
根据我阅读的内容尝试自己解决,问题可能与 SwiftMailer 的版本有关吗?
谢谢
方法 "setMessage" 确实引用了您的 Contacto 实体,但是要使用 SwiftMailer 设置消息的内容,您必须使用 setBody();
示例:
$mail = \Swift_Message::newInstance();
$mail->setFrom('me@mail.com')
->setTo('you@mail.com')
->setSubject('Email subject')
->setBody('email body, can be swift template')
->setContentType('text/html');
$this->get('mailer')->send($mail);
编辑:将电子邮件内容保存在数据库中并将其发送到 Twig 模板
我在你的函数中添加了一些代码,实际将电子邮件内容保存在数据库中。这不是强制性的,但我想您稍后会希望在您的应用程序中检索此信息。无论如何,我没有对此进行测试,但它应该可以工作:您的控制器将 SwiftMailer 消息正文设置为 Twig 模板。您的模板应该能够解析 "contacto" 实体。
public function newAction()
{
$contacto = new Contacto();
$form = $this->createForm(new ContactoType(), $contacto);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
// set your "contacto" entity
$contacto->setMessage($form->get('message')->getData());
// do the rest of $contact->set{......}
// save the message in database (if you need to but I would do it)
$em = $this->getDoctrine()->getManager();
$em->persist($contacto);
$em->flush();
// SEND THE EMAIL
$message = \Swift_Message::newInstance();
$message->setFrom('enquiries@myweb.com.ar')
->setTo($this->container->getParameter('tp_main.emails.contact_email'))
->setSubject('YOUR SUBJECT')
->setBody($this->renderView('TPMainBundle:Default:contactEmail.txt.twig', array('contacto' => $contacto)))
->setContentType('text/html');
$this->get('mailer')->send($message);
$this->get('session')->setFlash('blogger-notice', 'Your contact enquiry was successfully sent. Thank you!');
return $this->redirect($this->generateUrl('contacto_new'));
}
}
return $this->render('TPMainBundle:Default:contact.html.twig', array('form' => $form->createView()));
}
我在尝试将 SwiftMailer 与 Symfony 结合使用时遇到错误。 我正在学习这里的教程:http://symblog.site90.net/docs/validators-and-forms.html
错误代码为:
FatalErrorException: Error: Call to undefined method Swift_Message::setMessage() in C:\xampp\htdocs\TP\src\TP\MainBundle\Controller\DefaultController.php line 327
我的操作是:
public function newAction()
{
$contacto = new Contacto();
$form = $this->createForm(new ContactoType(), $contacto);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$message = \Swift_Message::newInstance()
->setFrom('enquiries@myweb.com.ar')
->setTo($this->container->getParameter('tp_main.emails.contact_email'))
//this is the line 327 related with the error
->setMessage($this->renderView('TPMainBundle:Default:contactEmail.txt.twig', array('contacto' => $contacto)));
//
$this->get('mailer')->send($message);
$this->get('session')->setFlash('blogger-notice', 'Your contact enquiry was successfully sent. Thank you!');
return $this->redirect($this->generateUrl('contacto_new'));
}
}
return $this->render('TPMainBundle:Default:contact.html.twig', array(
'form' => $form->createView(),));}
错误说它是未定义的,但是, 我的方法 setMessage() 在我的 class Contacto.php 中定义 getter 和 setter :
/**
* @var string
*
* @ORM\Column(name="message", type="text")
*/
private $message;
/**
* Set message
*
* @param string $message
* @return Contacto
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}
/**
* Get message
*
* @return string
*/
public function getMessage()
{
return $this->message;
}
根据我阅读的内容尝试自己解决,问题可能与 SwiftMailer 的版本有关吗?
谢谢
方法 "setMessage" 确实引用了您的 Contacto 实体,但是要使用 SwiftMailer 设置消息的内容,您必须使用 setBody();
示例:
$mail = \Swift_Message::newInstance();
$mail->setFrom('me@mail.com')
->setTo('you@mail.com')
->setSubject('Email subject')
->setBody('email body, can be swift template')
->setContentType('text/html');
$this->get('mailer')->send($mail);
编辑:将电子邮件内容保存在数据库中并将其发送到 Twig 模板
我在你的函数中添加了一些代码,实际将电子邮件内容保存在数据库中。这不是强制性的,但我想您稍后会希望在您的应用程序中检索此信息。无论如何,我没有对此进行测试,但它应该可以工作:您的控制器将 SwiftMailer 消息正文设置为 Twig 模板。您的模板应该能够解析 "contacto" 实体。
public function newAction()
{
$contacto = new Contacto();
$form = $this->createForm(new ContactoType(), $contacto);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
// set your "contacto" entity
$contacto->setMessage($form->get('message')->getData());
// do the rest of $contact->set{......}
// save the message in database (if you need to but I would do it)
$em = $this->getDoctrine()->getManager();
$em->persist($contacto);
$em->flush();
// SEND THE EMAIL
$message = \Swift_Message::newInstance();
$message->setFrom('enquiries@myweb.com.ar')
->setTo($this->container->getParameter('tp_main.emails.contact_email'))
->setSubject('YOUR SUBJECT')
->setBody($this->renderView('TPMainBundle:Default:contactEmail.txt.twig', array('contacto' => $contacto)))
->setContentType('text/html');
$this->get('mailer')->send($message);
$this->get('session')->setFlash('blogger-notice', 'Your contact enquiry was successfully sent. Thank you!');
return $this->redirect($this->generateUrl('contacto_new'));
}
}
return $this->render('TPMainBundle:Default:contact.html.twig', array('form' => $form->createView()));
}