2017 © Pedro Peláez
 

symfony-bundle imap-bundle

PHP-IMAP Symfony integration.

image

secit-pl/imap-bundle

PHP-IMAP Symfony integration.

  • Tuesday, July 17, 2018
  • by secit
  • Repository
  • 2 Watchers
  • 17 Stars
  • 15,456 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 7 Versions
  • 38 % Grown

The README.md

PHP-IMAP integration bundle

Simple php-imap integration for Symfony., (*1)

Compatibility matrix

Bundle version Maintained Symfony versions Min. PHP version
3.x Yes 6.4 to 7.x 8.1.0
2.1 No 4.4 to 6.4 8.0.0
1.5 No 4.0 to 4.3 5.4.0
1.4 No 2.8 to 3.4 5.4.0

Want to support this bundle?

Consider using our random code generator service at codito.io., (*2)

With codito.io you can generate up to 250,000 codes in the format of your choice for free. You can use the generated codes for purposes such as promotional codes (which you can, for example, print on the inside of packaging), serial numbers, one-time or multi-use passwords, lottery coupons, discount codes, vouchers, random strings and much more - for more use cases see our examples. If 250,000 codes are not enough for you, you can use our commercial code generation service., (*3)

Random Code Generator, (*4)

Installation

1. Composer

From the command line run, (*5)

composer require secit-pl/imap-bundle

Configuration

To set up your mailbox configuration open the config/packages/imap.yaml and adjust its content., (*6)

Here is the example configuration:, (*7)

imap:
    connections:
        example:
            imap_path: "{localhost:993/imap/ssl/novalidate-cert}INBOX"
            username: "email@example.com"
            password: "password"

        another:
            imap_path: "{localhost:143}INBOX"
            username: "username"
            password: "password"
            attachments_dir: "%kernel.project_dir%/var/imap/attachments"
            server_encoding: "UTF-8"

        full_config:
            imap_path: "{localhost:143}INBOX"
            username: "username"
            password: "password"
            attachments_dir: "%kernel.project_dir%/var/imap/attachments"
            create_attachments_dir_if_not_exists: true # default true
            created_attachments_dir_permissions: 777 # default 770
            server_encoding: "UTF-8"
            enabled: true

If you're using Symfony to connect to a Microsoft 365 business environment, there's a good chance you'll want to connect to a shared mailbox. In that case you need to specify the parameters authuser and user. Where shared_account is the username without domain, like:, (*8)

imap:
    connections:
        example:
            imap_path: "{outlook.office365.com:993/imap/ssl/authuser=first.last@example.com/user=shared_account}Root/Folder"
            username: "email@example.com"
            password: "password"

Security

It's good practice to do not set the sensitive data like mailbox, username and password directly in the config-files. You may have to encode the values. Configuration Based on Environment Variables Referencing Secrets in Configuration Files Better set them in .env.local, use Symfony Secrets or CI-Secrets., (*9)

imap:
    connections:
        example:
            imap_path:  '%env(EXAMPLE_CONNECTION_MAILBOX)%'
            username: '%env(EXAMPLE_CONNECTION_USERNAME)%'
            password: '%env(EXAMPLE_CONNECTION_PASSWORD)%'

Dump actual config:

php bin/console debug:config imap

Validate if the mailboxes can connect correct

php bin/console secit:imap:validate-connections

Result:, (*10)

+------------------+---------------------+---------------------------------+--------------------+---------+
| Connection       | Connect Result      | Mailbox                         | Username           | Enabled |
+------------------+---------------------+---------------------------------+--------------------+---------+
| example          | SUCCESS             | {imap.example.com:993/imap/ssl} | user@mail.com      | YES     |
| example_WRONG    | FAILED: Reason..... | {imap.example.com:993/imap/ssl} | WRONG              | YES     |
| example_DISABLED | DISABLED            | {imap.example.com:993/imap/ssl} | user2@mail.com     | NO      |
+------------------+---------------------+---------------------------------+--------------------+---------+

This command can take some while if any connection failed. That is because of a long connection-timeout. If you use this in CI-Pipeline add the parameter -q. Password is not displayed for security reasons. You can set an array of connections to validate., (*11)

php bin/console secit:imap:validate-connections example example2

Usage

Let's say your config looks like this, (*12)

imap:
    connections:
        example:
            imap_path: ...

        second:
            imap_path: ...

        connection3:
            imap_path: ...

You can get the connection inside a class by using service autowiring and using camelCased connection name + Connection as parameter name., (*13)

<?php

namespace App\Controller;

use SecIT\ImapBundle\ConnectionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class IndexController extends AbstractController
{
    public function index(
        ConnectionInterface $exampleConnection,
        ConnectionInterface $secondConnection,
        ConnectionInterface $connection3Connection,
    ) {
        $mailbox = $exampleConnection->getMailbox(); // instance of PhpImap\Mailbox
        $isConnectable = $secondConnection->testConnection();
        $connectionName = $connection3Connection->getName(); // connection3

        ...
    }

    ...
}

Connections can also be injected thanks to their name and the Target attribute:, (*14)

<?php

namespace App\Controller;

use SecIT\ImapBundle\ConnectionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Target;

class IndexController extends AbstractController
{
    public function index(
        #[Target('exampleConnection')] 
        ConnectionInterface $example,
        #[Target('secondConnection')] 
        ConnectionInterface $customName,
        #[Target('connection3Connection')] 
        ConnectionInterface $connection,
    ) {
        $mailbox = $exampleConnection->getMailbox(); // instance of PhpImap\Mailbox
        $isConnectable = $secondConnection->testConnection();
        $connectionName = $connection3Connection->getName(); // connection3

        ...
    }

    ...
}

To get all connections you can use AutowireIterator, (*15)

<?php

namespace App\Controller;

use SecIT\ImapBundle\ConnectionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;

class IndexController extends AbstractController
{
    public function index(
        #[AutowireIterator('secit.imap.connection')]
        iterable $connections,
    ) {
        foreach ($connections as $connection) {
            $mailbox = $connection->getMailbox();
        }

        ...
    }

    ...
}

From this point you can use any of the methods provided by the php-imap library. For example, (*16)

$mailbox = $exampleConnection->getMailbox();
$mailbox->getMailboxInfo();

To quickly test the connection to the server you can use the testConnection() method, (*17)

// testing with a boolean response
$isConnectable = $exampleConnection->testConnection();
var_dump($isConnectable);

// testing with a full error message
try {
    $isConnectable = $exampleConnection->testConnection(true);
} catch (\Exception $exception) {
    echo $exception->getMessage();
}

Be aware that this will disconnect your current connection and create a new one on success. In most cases this is not a problem., (*18)

Migration guide

Version 3.0.0 introduces some BC breaks., (*19)

Configuration

To better match PhpImap\Mailbox constructor arguments the mailbox configuration parameter was renamed to imap_path., (*20)

Previous version:, (*21)

imap:
    connections:
        example_connection:
            mailbox: ...
            username: ...
            password: ...

Current version:, (*22)

imap:
    connections:
        example:
            imap_path: ...
            username: ...
            password: ...

Connections getting

Previously to get the connection, you had to inject the SecIT\ImapBundle\Service\Imap service and get a connection from it., (*23)

public function index(Imap $imap)
{
    $mailbox = $imap->get('example_connection')->getConnection();
}

After migration, you should use autowiring to inject dynamically created services for each connection, (*24)


use SecIT\ImapBundle\Connection\ConnectionInterface; public function index(ConnectionInterface $exampleConnection) { $mailbox = $exampleConnection->getMailbox(); }

or use Target attribute, (*25)


use SecIT\ImapBundle\ConnectionInterface; use Symfony\Component\DependencyInjection\Attribute\Target; public function index( #[Target('exampleConnection')] ConnectionInterface $customName, ) { $mailbox = $customName->getMailbox(); }

Console command

The command changes its name from imap-bundle:validate to secit:imap:validate-connections., (*26)

The Versions

17/07 2018

dev-master

9999999-dev http://secit.pl

PHP-IMAP Symfony integration.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

bundle symfony imap php-imap

17/07 2018

1.3.2

1.3.2.0 http://secit.pl

PHP-IMAP Symfony integration.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

bundle symfony imap php-imap

02/02 2018

1.3.1

1.3.1.0 http://secit.pl

PHP-IMAP Symfony integration.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

bundle symfony imap php-imap

19/01 2018

1.3.0

1.3.0.0 http://secit.pl

PHP-IMAP Symfony integration.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

bundle symfony imap php-imap

23/10 2017

1.2.0

1.2.0.0 http://secit.pl

PHP-IMAP Symfony integration.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

bundle symfony imap php-imap

03/10 2017

1.1.0

1.1.0.0 http://secit.pl

PHP-IMAP Symfony integration.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

bundle symfony imap php-imap

04/07 2017

1.0.0

1.0.0.0 http://secit.pl

PHP-IMAP Symfony integration.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

bundle symfony imap php-imap