Correct way to retrieve mails by IMAP in symfony2
Asked Answered
L

2

19

I have to develop simple mail client in symfony2 using IMAP. Im wondering what is best way to retrieve messages from server (lets take a gmail as example)?

I did something like this:

public function indexAction($name)
{
    $user = '[email protected]';
    $password = 'password';
    $mailbox = "{imap.gmail.com:993/imap/ssl}INBOX";
    $mbx = imap_open($mailbox , $user , $password);
    $ck = imap_check($mbx);
    $mails = imap_fetch_overview($mbx,"1:5");
    return $this->render('HtstMailBundle:Mail:index.html.twig',array('name'=>$name,'mail'=>$mails));
}

is this right way, or not? It works, but is it compatible with symfony "standards"?

Lutestring answered 7/2, 2012 at 19:13 Comment(0)
M
20

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 = '[email protected]';
    $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
Marcy answered 7/2, 2012 at 19:32 Comment(0)
G
2

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 :)

Girand answered 2/8, 2016 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.