定制 secit-pl/imap-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

secit-pl/imap-bundle

Composer 安装命令:

composer require secit-pl/imap-bundle

包简介

PHP-IMAP Symfony integration.

README 文档

README

Simple php-imap integration for Symfony.

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.

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.

Random Code Generator

Installation

1. Composer

From the command line run

composer require secit-pl/imap-bundle

Configuration

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"

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.

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:

+------------------+---------------------+---------------------------------+--------------------+---------+
| 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

Usage

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\Connection\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\Connection\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\Connection\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.

Migration guide

Version 3.0.0 introduces some BC breaks.

Configuration

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

Connections getting

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\Connection\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.

secit-pl/imap-bundle 适用场景与选型建议

secit-pl/imap-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.56M 次下载、GitHub Stars 达 71, 最近一次更新时间为 2017 年 07 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「bundle」 「imap」 「php-imap」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 secit-pl/imap-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 secit-pl/imap-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 secit-pl/imap-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.56M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 71
  • 点击次数: 11
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 71
  • Watchers: 3
  • Forks: 19
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-04