Check for bounced emails and take action on them.
molnix/bounced-mail-manager is a Laravel package for check for bounced emails and take action on them..
It currently has 5 GitHub stars and 1.144 downloads on Packagist (latest version v3.1.0).
Install it with composer require molnix/bounced-mail-manager.
Discover more Laravel packages by molnix
or browse all Laravel packages to compare alternatives.
Last updated
A package can read IMAP and parse mails to check if they are bounced emails. When used with laravel, can notifiy the original sender.
composer require molnix/bounced-mail-manager
Add the custom headers created by the getCustomHeaders method to outgoing mails.
Molnix\BouncedMailManager\Message\Header::getCustomHeaders(
string $sender, // Sender email to send notificaitons
string $sentTo, // send to email
string $subject ='' optional subject
): array
Package can be customized with optional env variables
| Variable | Description |
| ---------------------------- | ------------------------------------------------------------------- |
| BOUNCEMAIL_HOST | IMAP host url, default: MAIL_HOST from .env |
| BOUNCEMAIL_PORT | IMAP port, default: 993 |
| BOUNCEMAIL_USERNAME | IMAP username, default: MAIL_USERNAME from .env |
| BOUNCEMAIL_PASSWORD | IMAP password, default: MAIL_PASSWORD from .env |
| BOUNCEMAIL_MAILBOX | Mailox name, default: INBOX |
| BOUNCEMAIL_DELETE_MODE | False will use read/unread instead of deleting, default: true |
| BOUNCEMAIL_TYPE | Mailbox type: null. imap,o365 , default: imap |
| BOUNCEMAIL_OAUTH_CLIENT_ID | Oauth client id , default: null |
| BOUNCEMAIL_OAUTH_SECRET | Oauth secret , default: null |
| BOUNCEMAIL_AZURE_TENANT_ID | Azure tenant id if using office 365 , default: null |
The package will automatically register a service provider. You can publish for further customization.
| Tag | Description |
| -------------- | --------------------------- |
| config | Configuration |
| views | Notification email markdown |
| translations | Bounce reason translations |
Optionally publish files if further customization is required.
php artisan vendor:publish --provider="Molnix\BouncedMailManager\BounceManagerServiceProvider" --tag="config"
php artisan vendor:publish --provider="Molnix\BouncedMailManager\BounceManagerServiceProvider" --tag="views"
php artisan vendor:publish --provider="Molnix\BouncedMailManager\BounceManagerServiceProvider" --tag="translations"
Use Molnix\BouncedMailManager\Traits\BounceMailHeaders trait in Mailable to add headers to outgoing headers.
Option 1: With setup
// PostMail
use BounceMailHeaders;
public function __construct(Comment $comment)
{
$this->comment = $comment;
$this->setupBounceManager();
}
public function build(){
$this->subject('Comment added')
->markdown('emails.comment')
->addBounceManagerHeaders();
}
Option 2: Without setup
// PostMail
use BounceMailHeaders;
public function __construct(Comment $comment, $sender)
{
$this->comment = $comment;
$this->sender = $sender;
}
public function build(){
$this->subject('Comment added')
->markdown('emails.comment')
->addBounceManagerHeaders($this->sender);
}
Add this command to the scheduler.
php artisan bouncemanager:run
#### Create instance
```php
use Molnix\BouncedMailManager\BounceManager;
$manager = new BounceManager(
'imap.example.com',
'993',
'[email protected]',
'emailpassword',
string $mailbox = 'INBOX',
array $options = [] // extra optional options
);
or
use Molnix\BouncedMailManager\BounceManager;
use Molnix\BouncedMailManager\Clients\ImapClient;
$manager->setClient(new ImapClient(
'imap.example.com',
'993',
'[email protected]',
'emailpassword',
string $mailbox = 'INBOX',
array $options = [] // extra optional options
));
or Office 365 way
use Molnix\BouncedMailManager\BounceManager;
use Molnix\BouncedMailManager\Clients\O365Client;
$manager = new BounceManager();
$manager->setClient(new O365Client('[email protected]', $tenant_id, $client_id, $client_secret));
$manager->setMailbox('OtherMailbBoxName');
// Use -1 for all mails.
$manager->setDaysFrom(10); // Since last 10 days
$manager->enableDeleteMode(); // Since last 10 days
$manager->toArray(); // Returns simple array of bounces.
$manager->get(); // Returns array of bounces objects.
The setup needed to be done in two section,
API permissions -> APIs my organization uses tab -> serach Office 365 Exchange Online -> Application permissions -> IMAP.AccessAsAppGrant admin consent by clicking the buttonRun these commands in powershell in order one by one. Remember to replace values for $AppId with Application (client) ID, $TenantId with Directory (tenant) ID. These can be found in the app page in azure web. Your email ID with the mailbox email you grant access to.
Install-Module -Name AzureAD
Install-Module -Name ExchangeOnlineManagement
$AppId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
$TenantId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Import-module AzureAD
Connect-AzureAd -Tenant $TenantId
($Principal = Get-AzureADServicePrincipal -filter "AppId eq '$AppId'")
$PrincipalId = $Principal.ObjectId
$DisplayName = "Bounce manager IMAP Access"
Import-module ExchangeOnlineManagement
Connect-ExchangeOnline -Organization $TenantId
New-ServicePrincipal -AppId $AppId -ServiceId $PrincipalId -DisplayName $DisplayName
Add-MailboxPermission -User $PrincipalId -AccessRights FullAccess -Identity "Your email ID"
Usage
use Molnix\BouncedMailManager\BounceManager;
use Molnix\BouncedMailManager\Clients\O365;
$manager = new BounceManager();
$manager->setClient(new O365('[email protected]', $tenant_id, $client_id, $client_secret));
$manager->get();