Search code examples
phpsymfonyswiftmailer

Address in mailbox given [Doctrine\ORM\Query_state] does not comply with RFC 2822, 3.6.2


I'm developing a blog under symfony and to send emails I use swiftMailer.

I use this code in my controller to send to all my users to warn them that a new article was added but the problem is that it shows me an error message.

My controller:

/**
 * @Route("/admin/ajout")
 * @param  Request $request
 * @return Response
 * @throws \Exception
 */

 public function add(Request $request, \Swift_Mailer $mailer): Response {

    $article = new Articles();
    $addArticle = $this->createForm(ArticlesType::class, $article);
    $addArticle->handleRequest($request);

    $info = $this->getDoctrine()->getRepository(InfoMore::class)->findByInfo(2);

     $em    = $this->get('doctrine.orm.entity_manager');
     $dql   = "SELECT email FROM App:user";
     $query = $em->createQuery($dql);

    if($addArticle->isSubmitted() && $addArticle->isValid()) {

        $article = $addArticle->getData();
        $manager = $this->getDoctrine()->getManager();
        $manager->persist($article);
        $manager->flush();

        $message = (new \Swift_Message('Un nouvelles articles est publier'))
            ->setFrom('contact@al-houria.com')
            ->setTo($query)
            ->setBody(
                $this->renderView(
                    'email/article_publish.html.twig'
                ),
                'text/html'
            )
        ;
        $mailer->send($message);

        $this->addFlash('article', 'L\'article a bien étais ajouter');
        return $this->redirectToRoute('app_backoffice_admin');
    }

    return $this->render('backOffice/CRUD/add.html.twig', [
        'title' => 'Ajouter un article a votre blog',
        'info' => $info,
        'addArticle' => $addArticle->createView()
    ]);

 } 

and the message error:

Address in mailbox given [Doctrine\ORM\Query_state] does not comply with RFC 2822, 3.6.2

It tells me that the address is not valid and in this case how to add multiple email addresses?

Thank you for your help!


Solution

  • Assuming your User entity is in App\Entity\User, you can select this:

    $emails = $em
        ->createQuery('SELECT u.email FROM App\Entity\User u')
        ->getResult()
    ;
    

    This will return an array of email addresses from your users. Then you just pass that to your setTo() method:

    $message = (new \Swift_Message('Un nouvelles articles est publier'))
        ->setFrom('contact@al-houria.com')
        ->setTo($emails)
        // ...
    ;
    

    See the Doctrine DQL SELECT Examples for further help. If you wanted to be a bit fancier, you could add the user's name to their email addresses and send that out. Let's assume you have a getName() function in your User entity. You could instead generate your email addresses like so:

    $emails = [];
    $users = $this->getDoctrine()->getRepository(User::class)->findAll();
    
    foreach ($users as $user) {
        $emails[$user->getEmail()] = $user->getName();
    }
    

    Then when you called ->setTo($emails) it would have both their name and email address.

    One last note, I would use $em = $this->getDoctrine()->getManager(); rather than $this->get('doctrine.orm.entity_manager');