我尝试了关于swiftmailer symfony here的文档,但即使这一切对我来说都是正确的,我也无法使其正常工作。
我更改了paramaters.yml并没有按照说明触及config.yml。 似乎错误来自我的控制器,但万一我会复制你编写/更改的代码:
parameters.yml
parameters:
//
mailer_host: localhost
mailer_user: mymail@gmail.com
mailer_password: mypassword
config.yml
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
我的表单
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('mail', EmailType::class, array(
'label' => 'your mail'
))
->add('message', TextareaType::class, array(
'label' => 'your message'
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Contact::class,
));
}
}
我的控制器
class MainController extends Controller
{
public function indexAction(Request $request)
{
$contact = new Contact();
$formContact = $this->createForm(ContactType::class, $contact);
$formContact->handleRequest($request);
$data = $formContact->getData();
if ($formContact->isSubmitted() && $formContact->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($contact);
$em->flush();
$message = \Swift_Message::newInstance();
$message->setFrom($data->getMail())
->setTo('mymail@gmail.com')
->setBody($data->getMessage() . $data->getMail());
$this->get('mailer')->send($message);
}
return $this->render('app/main/index.html.twig', array(
'form' => $formContact->createView()
));
}
}
我不知道它是否有帮助,或者您是否需要更多信息,但很乐意分享,谢谢。