Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to retrieve mails by IMAP in symfony2

Tags:

symfony

imap

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"?

like image 597
WombaT Avatar asked Feb 07 '12 19:02

WombaT


People also ask

How do I send an email in Symfony?

This option was introduced in Symfony 5.2. To send an email, get a Mailer instance by type-hinting MailerInterface and create an Email object:

What happens if Symfony email is not delivered?

The message can later be lost or not delivered because of some problem in your provider, but that's out of reach for your Symfony application. If there's an error when handing over the email to your transport, Symfony throws a TransportExceptionInterface .

Does Symfony Mailer support high availability?

Symfony's mailer supports high availability via a technique called "failover" to ensure that emails are sent even if one mailer server fails. A failover transport is configured with two or more transports and the failover keyword:

How to debug a sent message in Symfony?

The getMessageId () method from SentMessage always returns the definitive ID of the message (being the original random ID generated by Symfony or the new ID generated by the mailer provider). The exceptions related to mailer transports (those which implement TransportException) also provide this debug information via the getDebug () method.


1 Answers

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
like image 122
meze Avatar answered Oct 14 '22 04:10

meze