-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First commit of the MailTracker. Outgoing mail is being written to th…
…e database, pings and clicks are being tracked (as far as I can tell at this point).
- Loading branch information
Jon Baker
committed
Mar 1, 2016
0 parents
commit 8b04c44
Showing
16 changed files
with
564 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
; This file is for unifying the coding style for different editors and IDEs. | ||
; More information at http://editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_size = 4 | ||
indent_style = space | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
preset: psr2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Contributor Code of Conduct | ||
|
||
As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. | ||
|
||
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. | ||
|
||
Examples of unacceptable behavior by participants include: | ||
|
||
* The use of sexualized language or imagery | ||
* Personal attacks | ||
* Trolling or insulting/derogatory comments | ||
* Public or private harassment | ||
* Publishing other's private information, such as physical or electronic addresses, without explicit permission | ||
* Other unethical or unprofessional conduct. | ||
|
||
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. | ||
|
||
This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community in a direct capacity. Personal views, beliefs and values of individuals do not necessarily reflect those of the organisation or affiliated individuals and organisations. | ||
|
||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. | ||
|
||
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Contributing | ||
|
||
Contributions are **welcome** and will be fully **credited**. | ||
|
||
We accept contributions via Pull Requests on [Github](https://github.com/jdavidbakr/MailTracker). | ||
|
||
|
||
## Pull Requests | ||
|
||
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). | ||
|
||
- **Add tests!** - Your patch won't be accepted if it doesn't have tests. | ||
|
||
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. | ||
|
||
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. | ||
|
||
- **Create feature branches** - Don't ask us to pull from your master branch. | ||
|
||
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. | ||
|
||
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. | ||
|
||
|
||
## Running Tests | ||
|
||
``` bash | ||
$ composer test | ||
``` | ||
|
||
|
||
**Happy coding**! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# The MIT License (MIT) | ||
|
||
Copyright (c) 2016 J David Baker <[email protected]> | ||
|
||
> Permission is hereby granted, free of charge, to any person obtaining a copy | ||
> of this software and associated documentation files (the "Software"), to deal | ||
> in the Software without restriction, including without limitation the rights | ||
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
> copies of the Software, and to permit persons to whom the Software is | ||
> furnished to do so, subject to the following conditions: | ||
> | ||
> The above copyright notice and this permission notice shall be included in | ||
> all copies or substantial portions of the Software. | ||
> | ||
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
> THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# MailTracker | ||
|
||
[![Latest Version on Packagist][ico-version]][link-packagist] | ||
[![Software License][ico-license]](LICENSE.md) | ||
[![Build Status][ico-travis]][link-travis] | ||
[![Coverage Status][ico-scrutinizer]][link-scrutinizer] | ||
[![Quality Score][ico-code-quality]][link-code-quality] | ||
[![Total Downloads][ico-downloads]][link-downloads] | ||
|
||
MailTracker will hook into all outgoing emails from Laravel and inject a tracking code into it. It will also store the rendered email in the database. There is also an interface to view sent emails. | ||
|
||
## Install | ||
|
||
Via Composer | ||
|
||
``` bash | ||
$ composer require jdavidbakr/MailTracker | ||
``` | ||
|
||
Add the following to the providers array in config\app.php: | ||
|
||
``` php | ||
jdavidbakr\MailTracker\MailTrackerServiceProvider::class, | ||
``` | ||
|
||
Publish the config file and migration | ||
``` bash | ||
$ php artisan vendor publish | ||
``` | ||
|
||
Run the migration | ||
``` bash | ||
$ php artisan migrate | ||
``` | ||
|
||
## Usage | ||
|
||
Once installed, all outgoing mail will be logged to the database. | ||
|
||
## TODO | ||
|
||
Currently this plugin is only tracking the outgoing mail. There is no view yet to explore the existing data. | ||
|
||
## Contributing | ||
|
||
Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. | ||
|
||
## Security | ||
|
||
If you discover any security related issues, please email [email protected] instead of using the issue tracker. | ||
|
||
## Credits | ||
|
||
- [J David Baker][link-author] | ||
- [All Contributors][link-contributors] | ||
|
||
## License | ||
|
||
The MIT License (MIT). Please see [License File](LICENSE.md) for more information. | ||
|
||
[ico-version]: https://img.shields.io/packagist/v/jdavidbakr/MailTracker.svg?style=flat-square | ||
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square | ||
[ico-travis]: https://img.shields.io/travis/jdavidbakr/MailTracker/master.svg?style=flat-square | ||
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/jdavidbakr/MailTracker.svg?style=flat-square | ||
[ico-code-quality]: https://img.shields.io/scrutinizer/g/jdavidbakr/MailTracker.svg?style=flat-square | ||
[ico-downloads]: https://img.shields.io/packagist/dt/jdavidbakr/MailTracker.svg?style=flat-square | ||
|
||
[link-packagist]: https://packagist.org/packages/jdavidbakr/MailTracker | ||
[link-travis]: https://travis-ci.org/jdavidbakr/MailTracker | ||
[link-scrutinizer]: https://scrutinizer-ci.com/g/jdavidbakr/MailTracker/code-structure | ||
[link-code-quality]: https://scrutinizer-ci.com/g/jdavidbakr/MailTracker | ||
[link-downloads]: https://packagist.org/packages/jdavidbakr/MailTracker | ||
[link-author]: https://github.com/jdavidbakr | ||
[link-contributors]: ../../contributors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "jdavidbakr/mail-tracker", | ||
"type": "library", | ||
"description": "Logs and tracks all outgoing emails from Laravel", | ||
"keywords": [ | ||
"jdavidbakr", | ||
"MailTracker" | ||
], | ||
"homepage": "https://github.com/jdavidbakr/MailTracker", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "J David Baker", | ||
"email": "[email protected]", | ||
"homepage": "http://www.jdavidbaker.com", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"illuminate/support": "~5.1", | ||
"php" : "~5.5|~7.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit" : "4.*", | ||
"scrutinizer/ocular": "~1.1", | ||
"squizlabs/php_codesniffer": "~2.3" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"jdavidbakr\\MailTracker\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"jdavidbakr\\MailTracker\\": "tests" | ||
} | ||
}, | ||
"scripts": { | ||
"test": "phpunit" | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.0-dev" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
return [ | ||
/** | ||
* To disable the pixel injection, set this to false. | ||
*/ | ||
'inject-pixel'=>true, | ||
|
||
/** | ||
* To disable injecting tracking links, set this to false. | ||
*/ | ||
'track-links'=>true, | ||
|
||
/** | ||
* Optionally expire old emails, set to 0 to keep forever. | ||
*/ | ||
'expire-days'=>60, | ||
|
||
/** | ||
* Where should the pingback URL route be? | ||
*/ | ||
'route' => [ | ||
'prefix' => 'email', | ||
'middleware' => [], | ||
], | ||
|
||
/** | ||
* Where should the admin route be? | ||
*/ | ||
'admin-route' => [ | ||
'prefix' => 'email-manager', | ||
'middleware' => 'super', | ||
], | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateSentEmailsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('sent_emails', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->timestamps(); | ||
$table->char('hash',32)->unique(); | ||
$table->text('headers'); | ||
$table->string('sender'); | ||
$table->string('recipient'); | ||
$table->string('subject'); | ||
$table->text('content'); | ||
$table->integer('opens'); | ||
$table->integer('clicks'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::drop('sent_emails'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace jdavidbakr\MailTracker; | ||
|
||
class MailTracker implements \Swift_Events_SendListener { | ||
|
||
protected $hash; | ||
/** | ||
* Inject the tracking code into the message | ||
*/ | ||
public function beforeSendPerformed(\Swift_Events_SendEvent $event) | ||
{ | ||
$message = $event->getMessage(); | ||
$headers = $message->getHeaders(); | ||
$hash = str_random(32); | ||
|
||
$original_content = $message->getBody(); | ||
|
||
if ($message->getContentType() === 'text/html' || | ||
($message->getContentType() === 'multipart/alternative' && $message->getBody()) | ||
) { | ||
$message->setBody($this->addTrackers($message->getBody(), $hash)); | ||
} | ||
|
||
foreach ($message->getChildren() as $part) { | ||
if (strpos($part->getContentType(), 'text/html') === 0) { | ||
$converter->setHTML($part->getBody()); | ||
$part->setBody($this->addTrackers($message->getBody(), $hash)); | ||
} | ||
} | ||
|
||
Model\SentEmail::create([ | ||
'hash'=>str_random(32), | ||
'headers'=>$headers->toString(), | ||
'sender'=>$headers->get('from')->getFieldBody(), | ||
'recipient'=>$headers->get('to')->getFieldBody(), | ||
'subject'=>$headers->get('subject')->getFieldBody(), | ||
'content'=>$original_content, | ||
]); | ||
|
||
// Purge old records | ||
if(config('mail-tracker.expire-days') > 0) { | ||
Model\SentEmail::where('created_at','<',\Carbon\Carbon::now()->subDays(config('mail-tracker.expire-days')))->delete(); | ||
} | ||
} | ||
|
||
public function sendPerformed(\Swift_Events_SendEvent $event) | ||
{ | ||
// | ||
} | ||
|
||
protected function addTrackers($html, $hash) | ||
{ | ||
if(config('mail-tracker.inject-pixel')) { | ||
$html = $this->injectTrackingPixel($html, $hash); | ||
} | ||
if(config('mail-tracker.track-links')) { | ||
$html = $this->injectLinkTracker($html, $hash); | ||
} | ||
|
||
return $html; | ||
} | ||
|
||
protected function injectTrackingPixel($html, $hash) | ||
{ | ||
// Append the tracking url | ||
$tracking_pixel = '<img src="'.action('\jdavidbakr\MailTracker\MailTrackerController@getT',[$hash])." />"; | ||
|
||
$linebreak = str_random(32); | ||
$html = str_replace("\n",$linebreak,$html); | ||
|
||
if(preg_match("/^(.*<body[^>]*>)(.*)$/", $html, $matches)) { | ||
$html = $matches[1].$tracking_pixel.$matches[2]; | ||
} else { | ||
$html = $tracking_pixel . $html; | ||
} | ||
$html = str_replace($linebreak,"\n",$html); | ||
|
||
return $html; | ||
} | ||
|
||
protected function injectLinkTracker($html, $hash) | ||
{ | ||
$this->hash = $hash; | ||
|
||
$html = preg_replace_callback("/(<a[^>]*href=['\"])([^'\"]*)/", | ||
array($this, 'inject_link_callback'), | ||
$html); | ||
|
||
return $html; | ||
} | ||
|
||
protected function inject_link_callback($matches) | ||
{ | ||
return $matches[1].action('\jdavidbakr\MailTracker\MailTrackerController@getL', | ||
[ | ||
urlencode($matches[2]), | ||
$this->hash | ||
]); | ||
} | ||
} |
Oops, something went wrong.