ehymel / imap-bundle
PHP-IMAP Symfony集成。
Requires
- php: >=8.0
- php-imap/php-imap: ~4.4|~5.0
- symfony/dependency-injection: ~4.4|~5.0|~6.0
- symfony/framework-bundle: ~4.4|~5.0|~6.0
README
为Symfony 4.4、5.x和6.x提供简单的php-imap集成。
版本2.0及以上需要PHP 8.0+和Symfony 4.4+。如果你仍然在使用之前的PHP或Symfony版本,请使用1.x版本。
版本1.5及以上仅与Symfony 4+兼容。之前的版本不再支持。如果你想在Symfony 2.8或3.x中使用,应该使用1.4版本,这是与Symfony 2.8和3.x兼容的最后一个版本。
想要支持这个包吗?
考虑购买我们的macOS应用程序,它可以让你自定义macOS菜单栏。
Infinite Menu Bar 允许你将自定义元素添加到菜单栏。想要显示当前IP(本地或外部)、MacBook电池状态、比特币价格,或者甚至是从任何HTTP URL或API获取的定制内容?没问题!这个应用程序可以做到这些,还有很多更多!
安装
1. Composer
在命令行运行
$ composer require secit-pl/imap-bundle
如果你使用Symfony Flex,你已经完成了,可以转到配置部分。否则,你必须手动注册这个包。
2. 注册包
如果你不使用Symfony Flex,你必须手动在/config/bundles.php中注册这个包,通过添加包声明。
return [ ... new SecIT\ImapBundle\ImapBundle(), ];
配置
要设置你的邮箱配置,打开config/packages/imap.yaml
并调整其内容。
以下是一个示例配置
imap: connections: example_connection: mailbox: "{localhost:993/imap/ssl/novalidate-cert}INBOX" username: "email@example.com" password: "password" another_connection: mailbox: "{localhost:143}INBOX" username: "username" password: "password" attachments_dir: "%kernel.project_dir%/var/imap/attachments" server_encoding: "UTF-8" full_config_connection: mailbox: "{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"
如果你使用Symfony连接到Microsoft 365商业环境,你很可能想连接到一个共享邮箱。在这种情况下,你需要指定authuser
和user
参数。其中shared_account是域外的用户名,例如
imap: connections: example_connection: mailbox: "{outlook.office365.com:993/imap/ssl/authuser=first.last@example.com/user=shared_account}Root/Folder" username: "email@example.com" password: "password"
安全
不要将敏感数据(如邮箱、用户名和密码)直接设置在配置文件中。你可能需要编码这些值。请根据基于环境变量的配置和在配置文件中引用机密进行设置。更好的方法是设置在.env.local
中,使用Symfony Secrets或CI-Secrets。
imap: connections: example_connection: mailbox: '%env(EXAMPLE_CONNECTION_MAILBOX)%' username: '%env(EXAMPLE_CONNECTION_USERNAME)%' password: '%env(EXAMPLE_CONNECTION_PASSWORD)%'
实际配置转储
php bin/console debug:config imap
验证邮箱是否可以正确连接
php bin/console imap-bundle:validate
结果
+--------------------------+-------------------+-------------------------------+--------------------+
| Connection | Connect Result | Mailbox | Username |
+--------------------------+-------------------+-------------------------------+--------------------+
| example_connection | SUCCESS | {imap.strato.de:993/imap/ssl} | user@mail.com |
| example_WRONG_connection | FAILED: Reason... | {imap.strato.de:993/imap/ssl} | WRONG |
+--------------------------+-------------------+-------------------------------+--------------------+
如果任何连接失败,此命令可能需要一些时间。这是由于长连接超时。如果你在CI-Pipeline中使用此命令,请添加参数-q
。出于安全原因,密码不会显示。你可以设置一个连接数组以进行验证。
php bin/console imap-bundle:validate example_connection example_connection2
用法
自动装配
在你的控制器中
<?php namespace App\Controller; use SecIT\ImapBundle\Service\Imap; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class IndexController extends AbstractController { public function indexAction(Imap $imap) { $exampleConnection = $imap->get('example_connection'); $anotherConnection = $imap->get('another_connection'); ... } ... }
服务容器(仅适用于Symfony < 6)
在你的控制器中
<?php namespace App\Controller; use SecIT\ImapBundle\Service\Imap; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class IndexController extends Controller { public function indexAction() { $exampleConnection = $this->get('secit.imap')->get('example_connection'); $anotherConnection = $this->get('secit.imap')->get('another_connection'); ... } ... }
从这一点开始,你可以使用php-imap库提供的任何方法。例如
$exampleConnection = $imap->get('example_connection'); $exampleConnection->getMailboxInfo();
要快速测试到服务器的连接,你可以使用testConnection()
方法
// testing with a boolean response $isConnectable = $imap->testConnection('example_connection'); var_dump($isConnectable); // testing with a full error message try { $isConnectable = $imap->testConnection('example_connection', true); } catch (\Exception $exception) { echo $exception->getMessage(); }
请注意,这将在成功时断开你的当前连接并创建一个新的连接。在大多数情况下,这不是问题。