PHPackages                             shieldon/messenger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. shieldon/messenger

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

shieldon/messenger
==================

Sending messages to the third-party services made easy for PHP.

1.6.2(5y ago)2330.4k↑27.6%92MITPHPPHP &gt;=7.1.0CI failing

Since Jul 11Pushed 5y ago4 watchersCompare

[ Source](https://github.com/terrylinooo/messenger)[ Packagist](https://packagist.org/packages/shieldon/messenger)[ Docs](https://github.com/terrylinooo/messenger)[ RSS](/packages/shieldon-messenger/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (2)

Messenger
=========

[](#messenger)

Sending messages to the third-party services made easy for PHP.

*Supported modules:*

- Telegram
- Line Notify
- Rocket Chat
- Slack
- Slack Webhook
- Mail
- SMTP
- SendGrid
- MailGun
- MailGun (SMTP)
- Gmail (SMTP)
- Yahoo (SMTP)
- Outlook (SMTP)

More modules will come in the future...

Installation
------------

[](#installation)

Use PHP Composer:

```
composer require shieldon/messenger
```

Or, download it and include the Messenger autoloader.

```
require './autoload.php';
```

Basic Usage
-----------

[](#basic-usage)

Public API methods:

- send
- debugMode
- printResult

Other than the Mailer classes, the only one public API is `send()`. The only thing you need to do is to assign the required data fields into constructor when initializing instance.

### Telegram

[](#telegram)

Open your Telegram App, add `BotFather` to start a new conversation. Type command `/newbot` to obtain your API key.

Remember, make sure your channel type is public. If you want to send messages to your private channel, googling will find solutions.

```
$apiKey = 'your_api_key';
$channel = '@your_channel';

$telegram = new \Shieldon\Messenger\Telegram($apiKey, $channel);

if ($telegram->send('say something!')) {
    echo 'Message has been sent to your Telegram group.';
} else {
    echo 'Failed to send message.' . "\n";
    echo $telegram->printResult();
}
```

### Line Notify

[](#line-notify)

The access token can be obtained by clicking `Generate token` button at this [signup page](https://notify-bot.line.me/my/).

Once you have obtained your developer access token for the chat group you choose, invite `Line Notify` bot join your Line group, then the following code will work as expected.

```
$accessToken = 'your_access_token';

$line = new \Shieldon\Messenger\LineNotify($accessToken);

if ($line->send('say something!')) {
    echo 'Message has been sent to your Line group.';
} else {
    echo 'Failed to send message.' . "\n";
    echo $line->printResult();
}
```

### RocketChat

[](#rocketchat)

```
$accessToken = 'your_auth_token';
$userId = 'your_user_id';
$serverUrl = 'https://your_rocket_chat.com:3000';
$channel = '#general';

$rocketChat = new \Shieldon\Messenger\RocketChat($accessToken, $userId, $serverUrl, $channel);

if ($rocketChat->send('say something!')) {
    echo 'Message has been sent to your RocketChat channel.';
} else {
    echo 'Failed to send message.' . "\n";
    echo $rocketChat->printResult();
}
```

### Slack

[](#slack)

Please clearfully read Slack's official [API docs](https://api.slack.com/messaging/sending) to find out things you need.

Guide:

- Create a App
- Assign `channels:read` and `chat:write:bot` permissions to your App.
- Assign your APP to your workspace.
- Obtain bot's access token.
- Add your App to the channel you would like to send messages.

```
$botToken = 'xoxb-551837935968-920623655894-TI1zWtaDLCkTzZaFFuyfzL56';
$channel = '#general';

$slack = new \Shieldon\Messenger\Slack($botToken, $channel);

if ($slack->send('say something!')) {
    echo 'Message has been sent to your Slack channel.';
} else {
    echo 'Failed to send message.' . "\n";
    echo $slack->printResult();
}
```

### Slack Webhook

[](#slack-webhook)

This would be the simplest way for messaging. Please clearfully read Slack's official [API docs](https://api.slack.com/messaging/webhooks) to find out things you need.

```
$webhook = 'https://hooks.slack.com/services/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';

$slack = new \Shieldon\Messenger\SlackWebhook($webhook);

if ($slack->send('say something!')) {
    echo 'Message has been sent to your Slack channel.';
} else {
    echo 'Failed to send message.' . "\n";
    echo $slack->printResult();
}
```

ok.

---

Mailer Usage
------------

[](#mailer-usage)

Public API methods:

- send
- addTo
- addCc
- addBcc
- addReplyTo
- addRecipient
- setRecipients
- setSubject
- setSender
- debugMode
- printResult

There is no need to metion content type when using Mailer, the content type is automatically detected.

### Mail

[](#mail)

Native PHP mail function. To use this class, be sure you have set the settings right in your `php.ini`.

```
$mail = new \Shieldon\Messenger\Mail();
$mail->addSender('example.sender@gmail.com');
$mail->addRecipient('example.recipient@gmail.com');
$mail->setSubject('Foo, bar.');

if ($mail->send('say something!')) {
    echo 'Email has been delivered via PHP\'s native mail function.';
} else {
    echo 'Failed to send email.' . "\n";
    echo $maingun->printResult();
}
```

### SMTP

[](#smtp)

A very simple SMTP client.

```
$user = 'email@your_domain.com';
$pass = '12345678';
$host = '127.0.0.1';
$port = '25';

$mail = new \Shieldon\Messenger\Smtp($user, $pass, $host, $port);

$mail->addSender('email@your_domain.com');
$mail->addRecipient('do-not-reply@gmail.com');
$mail->setSubject('Foo, bar.');

if ($mail->send('say something!')) {
    echo 'Email has been delivered via SMTP.';
} else {
    echo 'Failed to send email.' . "\n";
    echo $maingun->printResult();
}
```

Note:

If you would like to use **SMTPS** or **STARTTLS**, the `$host` should have a prefix.

For example:

```
$host = 'ssl://smtp.gmail.com'; // SMTPS
$host = 'tls://smtp.gmail.com'; // STARTTLS
```

### SendGrid

[](#sendgrid)

If you have SendGrid API key, you can also send messages via SendGrid easily.

```
$apiKey = 'your_api_key';

$sendgrid = new \Shieldon\Messenger\Sendgrid($apiKey);
$sendgrid->addSender('example.sender@gmail.com');
$sendgrid->addRecipient('example.recipient@gmail.com');
$sendgrid->setSubject('Foo, bar.');

if ($sendgrid->send('say something!')) {
    echo 'Email has been delivered via SendGrid API.';
} else {
    echo 'Failed to send email.' . "\n";
    echo $maingun->printResult();
}
```

### MailGun

[](#mailgun)

```
$apiKey = 'your_api_key';
$domain = 'your_domain_name';

$maingun = new \Shieldon\Messenger\Mailgun($apiKey, $domain);
$maingun->addSender('example.sender@gmail.com');
$maingun->addRecipient('example.recipient@gmail.com');
$maingun->setSubject('Foo, bar.');

if ($maingun->send('say something!')) {
    echo 'Email has been delivered via MailGun API.';
} else {
    echo 'Failed to send email.' . "\n";
    echo $maingun->printResult();
}
```

### MailGun SMTP

[](#mailgun-smtp)

Extended from `Smtp`, a ready-to-use MailGun SMTP client.

```
$user = 'your@gmail.com';
$pass = 'your_password';

$maingun = new \Shieldon\Messenger\Smtp\Mailgun($user, $pass);

$maingun->addSender('example.sender@gmail.com');
$maingun->addRecipient('example.recipient@gmail.com');
$maingun->setSubject('Foo, bar.');

if ($maingun->send('say something!')) {
    echo 'Email has been delivered via MainGun SMTP server.';
} else {
    echo 'Failed to send email.' . "\n";
    echo $maingun->printResult();
}
```

### Gmail

[](#gmail)

Extended from `Smtp`, a ready-to-use Gmail SMTP client.

```
$user = 'your@gmail.com';
$pass = 'your_password';

$gmail = new \Shieldon\Messenger\Smtp\Gmail($user, $pass);

$gmail->addSender('your@gmail.com');
$gmail->addRecipient('test@gmail.com');
$gmail->setSubject('Foo, bar.');

if ($gmail->send('say something!')) {
    echo 'Email has been delivered via Gmail SMTP server.';
} else {
    echo 'Failed to send email.' . "\n";
    echo $gmail->printResult();
}
```

Note:

Google doesn't like people use their SMTP server to sending email by scripts, to make sure it can work without problems, you have to set the settings right:

- Check your Google Accounts -&gt; Access for less secure apps -&gt; Turn on
- Use your host where you use to send email with your Google account and confirm that you have trusted the device on.

### Yahoo Mail

[](#yahoo-mail)

Extended from `Smtp`, a ready-to-use Yahoo SMTP client.

```
$user = 'your@yahoo.com';
$pass = 'your_password';

$yahooMail = new \Shieldon\Messenger\Smtp\Yahoo($user, $pass);

$yahooMail->addSender('your@yahoo.com');
$yahooMail->addRecipient('test@gmail.com');
$yahooMail->setSubject('Foo, bar.');

if ($yahooMail->send('say something!')) {
    echo 'Email has been delivered via Yahoo SMTP server.';
} else {
    echo 'Failed to send email.' . "\n";
    echo $yahooMail->printResult();
}
```

Note: You can use your account password but if you are facing the following error:

```
(#AUTH005) Too many bad auth attempts error when trying to send email.

```

That is because that Yahoo might not allow 3rd-party products access the SMTP server by default. To resolve this problem:

- Go to [Account Security](https://login.yahoo.com/account/security),
- Under **Manage App password** section, create a password for that App.
- Use your App password instead of your account password.

### Outlook Mail (Office365)

[](#outlook-mail-office365)

Extended from `Smtp`, a ready-to-use Yahoo SMTP client.

```
$user = 'your@outlook.com';
$pass = 'your_password';

$outlook = new \Shieldon\Messenger\Smtp\Outlook($user, $pass);

$outlook->addSender('your@outlook.com');
$outlook->addRecipient('test@gmail.com');
$outlook->setSubject('Foo, bar.');

if ($outlook->send('say something!')) {
    echo 'Email has been delivered via Office365 SMTP server.';
} else {
    echo 'Failed to send email.' . "\n";
    echo $yahooMail->printResult();
}
```

Note:

When sending email via Office365 SMTP server at the first time, you will receive a notification email from *Outlook.com Team* to confirm your activity.

[![](https://camo.githubusercontent.com/f1537249d22ee775006877043368b71f70bf96472714539efe33affada533713/68747470733a2f2f692e696d6775722e636f6d2f4634596c4b6b672e706e67)](https://camo.githubusercontent.com/f1537249d22ee775006877043368b71f70bf96472714539efe33affada533713/68747470733a2f2f692e696d6775722e636f6d2f4634596c4b6b672e706e67)

Once you have completed the validation, you will be able to send email via Office365 SMTP server.

---

Debug
-----

[](#debug)

### debugMode()

[](#debugmode)

If you would like to catch exceptions, you use turn `debugMode` on. This option will throw exceptions when error occurred.

For example:

```
$mail = new \Shieldon\Messenger\Smtp($user, $pass, $host, $port);

$mail->debugMode(true);

$mail->addSender('email@your_domain.com');
$mail->addRecipient('do-not-reply@gmail.com');
$mail->setSubject('Foo, bar.');

try {
    $mail->send('say something!');

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
```

### printResult()

[](#printresult)

If you would like to print the executed results, you can use `printResult()`.

For example:

```
$mail = new \Shieldon\Messenger\Smtp($user, $pass, $host, $port);
$mail->addSender('email@your_domain.com');
$mail->addRecipient('do-not-reply@gmail.com');
$mail->setSubject('Foo, bar.');
$mail->send('say something!');

echo $mail->printResult();
```

If the email is sent successfully, the result will look like the text below:

```
success: true
message: Email is sent.
--- result ---
connection: 220 smtp.gmail.com ESMTP x11sm6715821pfn.53 - gsmtp
hello: 250 smtp.gmail.com at your service
auth_type: 334 VXNlcm5hbWU6
user: 334 UGFzc3dvcmQ6
pass: 235 2.7.0 Accepted
from: 250 2.1.0 OK x11sm6715821pfn.53 - gsmtp
to: 250 2.1.5 OK x11sm6715821pfn.53 - gsmtp
cc: 250 2.1.5 OK x11sm6715821pfn.53 - gsmtp
bcc: 250 2.1.5 OK x11sm6715821pfn.53 - gsmtp
data: 354 Go ahead x11sm6715821pfn.53 - gsmtp
send: 250 2.0.0 OK 1579887885 x11sm6715821pfn.53 - gsmtp
quit: 221 2.0.0 closing connection x11sm6715821pfn.53 - gsmtp

```

---

Author
------

[](#author)

Messenger library is brought to you by [Terry L.](https://terryl.in) from Taiwan.

Shieldon Messenger is initially designed for a part of the [Shieldon Firewall](https://github.com/terrylinooo/shieldon), sending notifications to webmasters or developers when their web applications are possibly under attacks. If you are looking for a web security library to protect your website, Shieldon [Firewall](https://shieldon.io) might be a good choice for you.

License
-------

[](#license)

MIT

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity39

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2137d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/fca418a834057a4203c6856e316ca4d7c71fa1b9076977e08e5c4d5a9cc9aec6?d=identicon)[terrylinooo](/maintainers/terrylinooo)

---

Top Contributors

[![terrylinooo](https://avatars.githubusercontent.com/u/11989371?v=4)](https://github.com/terrylinooo "terrylinooo (48 commits)")

---

Tags

gmail-smtpphp-mailerphp-messengersmtp-clientmessagemailersmtpMessenger

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/shieldon-messenger/health.svg)

```
[![Health](https://phpackages.com/badges/shieldon-messenger/health.svg)](https://phpackages.com/packages/shieldon-messenger)
```

###  Alternatives

[aplus/email

Aplus Framework Email Library

2461.6M3](/packages/aplus-email)[nette/mail

📧 Nette Mail: A handy library for creating and sending emails in PHP.

5389.8M246](/packages/nette-mail)[mageplaza/module-smtp

SMTP Extension for Magento 2 helps the owner of store simply install SMTP (Simple Mail Transfer Protocol) server which transmits the messages into codes or numbers

3015.9M8](/packages/mageplaza-module-smtp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
