Correct way to retrieve mails by IMAP in symfony2 Correct way to retrieve mails by IMAP in symfony2 symfony symfony

Correct way to retrieve mails by IMAP in symfony2


This has nothing to do with symfony "standards". But you can make your code more OOP if you move all login to a service class and use symfony DepencyInjection to create and get your service:

public function indexAction($name){    $user = 'adress@gmail.com';    $password = 'password';    $mailbox = "{imap.gmail.com:993/imap/ssl}INBOX";    $mails = $this->get("mail.checker")->receive($user, $password, $mailbox);    return $this->render('HtstMailBundle:Mail:index.html.twig',array('name'=>$name,'mail'=>$mails));}

Class declaration:

class MailChecker{    public function receive($user, $password, $mailbox)    {        ...imap_check()...    }}

service declartion:

services:    mail.checker:        class: Project\YourBundle\Service\MailChecker


You can also use this Symfony bundle for that and use it as a service. I is designed for old Symfony2 but tested it with Symfony 3 and works :)