symfony联系人表单已提交,但电子邮件未到达

时间:2019-04-03 13:34:39

标签: symfony smtp symfony4 mailer

我需要一些有关在symfony 4中发送电子邮件的帮助。

我在symfony 4中制作了联系表格。 我在哪里使用formbuilder和swiftmailer。联系人表格有效,它可以验证并且我可以提交。但是电子邮件没有到达,再加上发送后我在联系页面上没有收到确认消息。 我与网站本身的主机一起使用邮件程序。该网站处于生产模式。

请参阅下面我使用的代码。也许我错过了一些设置?预先感谢!

ContactType.php

<?php
// your-path-to-types/ContactType.php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name',TextType::class);
        $builder->add('email',EmailType::class);
        $builder->add('subject',TextType::class);
        $builder->add('body',TextareaType::class);
    }

    public function getBlockPrefix()
    {
        return 'contact';
    }
}

Contact.php我的实体

<?php

// src/Entity/Contact.php


namespace App\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Doctrine\Common\Collections\ArrayCollection;  

class Contact

{

    protected $name;
    protected $email;
    protected $subject;
    protected $body;
    public function getName()

    {
        return $this->name;
    }



    public function setName($name)

    {
        $this->name = $name;
    }



    public function getEmail()

    {
        return $this->email;
    }



    public function setEmail($email)

    {
        $this->email = $email;
    }



    public function getSubject()

    {
        return $this->subject;
    }



    public function setSubject($subject)

    {
        $this->subject = $subject;
    }



    public function getBody()

    {
        return $this->body;
    }



    public function setBody($body)

    {
        $this->body = $body;

    }

    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('name', new NotBlank());

        $metadata->addPropertyConstraint('email', new Email());

        $metadata->addPropertyConstraint('subject', new NotBlank());
        $metadata->addPropertyConstraint('subject', new Length(array('max'=> 50)));

        $metadata->addPropertyConstraint('body', new Length(array('min'=> 5)));
    }

}

和我的控制器

<?php
namespace App\Controller;

use App\Entity\Contact;
use App\Form\ContactType;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;


final class PagesController extends DefaultController

{


    /**
     * @Route("/contact", name="contact")
     * @Method("GET|POST")
     */
    public function contact(Request $request,  \Swift_Mailer $mailer)
    {

        $enquiry = new Contact();
        $form = $this->createForm(ContactType::class, $enquiry);

         $this->request = $request;
            if ($request->getMethod() == 'POST') {
            $form->handleRequest($request);

            if ($form->isValid()) {


                        $message = (new \Swift_Message('Hello Email'))
                            ->setFrom('contact@example.com')
                            ->setTo('xxxxxxx@gmail.com')
                            ->setBody($this->renderView('contact/contactEmail.txt.twig', array('enquiry' => $enquiry)));
                        $mailer->send($message);

                    $this->get('session')->getFlashbag('blog-notice', 'Your contact enquiry was successfully sent. Thank you!');


                // Redirect - This is important to prevent users re-posting
                // the form if they refresh the page
                return $this->redirect($this->generateUrl('contact'));

            }
        }

        return $this->render('pages/contact.html.twig', array(
            'form' => $form->createView(),
        ));
    }


}

在我的 main.env 文件中,我有这个

MAILER_URL=smtp://localhost:25?encryption=ssl&auth_mode=login&username=xxx&password=xxxx

因此不会发送电子邮件。提交后,我被转回到联系页面,也没有Flash消息:/ 我还想念其他东西吗?

示例:

  • 我的用户名是否必须是我也需要解码的完整电子邮件地址,等等?
  • 我还需要更改服务器上的某些内容吗?请告知。

3 个答案:

答案 0 :(得分:0)

好的,所以我现在设置了Gmail帐户。是的,它现在发送了电子邮件:)

所以我猜我的代码还不错。只是服务器设置不好,但是现在可以使用gmail :) 谢谢大家的帮助:)

答案 1 :(得分:0)

这是与电子邮件设置有关的另一个问题。通过更改为gmail设置,我可以将我的gmail地址作为发件人。但是我想将网站地址用作发件人(例如no-reply @)

这是如何工作的?我该如何设置?

答案 2 :(得分:0)

好的,我可以回答我自己的原始问题。

我让smtp工作了:

在swiftmailer.yaml中放置了以下内容,而我在yaml中禁用了邮件程序。

swiftmailer:
    transport:        smtp
    username:         null
    password:         null
    host:             localhost
    port:             465
    encryption:       ssl
    auth-mode:        login
    spool: { type: 'memory' }
    stream_options:
        ssl:
            allow_self_signed: true
            verify_peer: false
            verify_peer_name: false

电子邮件到达我的邮箱,发件人是xxx@website.com