Correct way to retrieve mails by IMAP in symfony2

戏子无情 提交于 2019-12-19 12:25:26

问题


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 = 'adress@gmail.com';
    $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"?


回答1:


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



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



来源:https://stackoverflow.com/questions/9182364/correct-way-to-retrieve-mails-by-imap-in-symfony2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!