Simple php-imap integration for Symfony.
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 |
Consider using our random code generator service at codito.io.
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.
From the command line run
composer require secit-pl/imap-bundle
To set up your mailbox configuration open the config/packages/imap.yaml
and adjust its content.
Here is the example configuration:
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:
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"
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.
imap:
connections:
example:
imap_path: '%env(EXAMPLE_CONNECTION_MAILBOX)%'
username: '%env(EXAMPLE_CONNECTION_USERNAME)%'
password: '%env(EXAMPLE_CONNECTION_PASSWORD)%'
php bin/console debug:config imap
php bin/console secit:imap:validate-connections
Result:
+------------------+---------------------+---------------------------------+--------------------+---------+
| 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.
php bin/console secit:imap:validate-connections example example2
Let's say your config looks like this
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.
<?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:
<?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
<?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
$mailbox = $exampleConnection->getMailbox();
$mailbox->getMailboxInfo();
To quickly test the connection to the server you can use the testConnection()
method
// 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.
Version 3.0.0 introduces some BC breaks.
To better match PhpImap\Mailbox constructor arguments the mailbox
configuration parameter was renamed to imap_path
.
Previous version:
imap:
connections:
example_connection:
mailbox: ...
username: ...
password: ...
Current version:
imap:
connections:
example:
imap_path: ...
username: ...
password: ...
Previously to get the connection, you had to inject the SecIT\ImapBundle\Service\Imap
service and get a connection from it.
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
use SecIT\ImapBundle\Connection\ConnectionInterface;
public function index(ConnectionInterface $exampleConnection)
{
$mailbox = $exampleConnection->getMailbox();
}
or use Target attribute
use SecIT\ImapBundle\ConnectionInterface;
use Symfony\Component\DependencyInjection\Attribute\Target;
public function index(
#[Target('exampleConnection')]
ConnectionInterface $customName,
) {
$mailbox = $customName->getMailbox();
}
The command changes its name from imap-bundle:validate
to secit:imap:validate-connections
.