Repository URL to install this package:
|
Version:
6.2.1 ▾
|
| serialization |
| Service |
| Entity |
| DependencyInjection |
| services.xml |
| DigitalAsceticMailBundle.php |
| .htaccess |
| CHANGELOG.md |
| README.md |
| composer.json |
The DigitalAsceticMailBundle adds support for a mail management in Symfony. It provides a collection of entities and a interface for extending mail providers. Also it has a default ImapMailProviderService for mail management based on IMAP protocol.
Require the bundle with composer:
composer require digitalascetic/mail
Enable the bundle in the kernel:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new DigitalAscetic\MailBundle\DigitalAsceticMailBundle(), // ... ); }
Add the following configuration to your config.yml file according to your purposes:
digital_ascetic_mail: enabled: true providers: IMAP: digital_ascetic_mail.service.provider.imap
If you need to handle other mail providers like Gmail (for gmail you can use digitalascetic/google-api-client), you can implement your own custom provider by implementing the MailProviderInterface.
This interface has a set of methods to be able send, receive, delete or batchDelete emails.
If you have enabled JMSSerializerBundle, this bundle add a set of metadata for each entity. You can see at Resources/serializer.
You need to set up your mail configuration, this config must implement MailConfigInterface. This entity have to handle the mail account configuration, like email address, or host and port for imap and smtp protocol.
The only requirement you must do is set a relationship between MailConfig and a User based on Symfony UserInterace.
When you have implemented the MailConfig set up, you can use the MailProvider. To do, you can use our MainMailService that provides a simple method to get a MailProvider based on a key.
For example, based on this config.yml example you can implement your MailService like:
<?php namespace App\Service; use App\Entity\MailConfig; use App\Entity\Security\User; use DigitalAscetic\MailBundle\Entity\Message; use DigitalAscetic\MailBundle\Entity\MessageAddress; use DigitalAscetic\MailBundle\Service\MailProviderInterface; use DigitalAscetic\MailBundle\Service\MainMailService; class MailService { /** @var MainMailService */ private $mainMailService; public function __construct(ContainerInterface $container, MainMailService $mainMailService) { parent::__construct($container); $this->mainMailService = $mainMailService; } /** * @param User $user * @return MailConfig|null */ public function getMailConfigByUser(User $user) { return $this->getEntityManager()->getRepository(MailConfig::class)->findOneBy(['user' => $user]); } /** * @param User $user * @param string $externalId * @param string|null $mailbox * @return Message */ public function getMessage(User $user, string $externalId, string $mailbox = null) { $mailConfig = $this->getMailConfigByUser($user); if ($mailConfig) { /** @var MailProviderInterface $provider */ $provider = $this->mainMailService->getMailProvider($mailConfig->getProvider()); if ($provider) { $params = array(); $params['id'] = $externalId; if (isset($mailbox)) { $params['in'] = $mailbox; } return $provider->getMessage($mailConfig, $params); } } } public function sendMessage(User $user, Message $message): bool { $mailConfig = $this->getMailConfigByUser($user); if ($mailConfig) { if (empty($message->getFrom())) { $message->setFrom(new MessageAddress($mailConfig->getEmail(), $mailConfig->getName())); } /** @var MailProviderInterface $provider */ $provider = $this->mainMailService->getMailProvider($mailConfig->getProvider()); if ($provider) { return $provider->sendMessage($mailConfig, $message); } } return false; } }
Run test executing:
./vendor/bin/simple-phpunit
Remember clear cache folder var/cache/