Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP sender example #15

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .doc/example-php/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.idea

###> symfony/framework-bundle ###
/.env
/public/bundles/
/var/
/vendor/
###< symfony/framework-bundle ###

###> friendsofphp/php-cs-fixer ###
/.php_cs
/.php_cs.cache
###< friendsofphp/php-cs-fixer ###
.DS_Store
app/bin/*
!app/bin/console
81 changes: 81 additions & 0 deletions .doc/example-php/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"name": "landsman/unicorn-mailing-example-php",
"license": "MIT",
"require": {
"php": "7.2.*",
"ext-ctype": "*",
"ext-dom": "*",
"ext-fileinfo": "*",
"ext-iconv": "*",
"ext-json": "*",
"guzzlehttp/psr7": "^1.4",
"mailgun/mailgun-php": "^2.5",
"nette/utils": "^2.5",
"php-http/curl-client": "^1.7",
"php-http/guzzle6-adapter": "^1.1",
"php-http/message": "^1.6",
"sentry/sentry-symfony": "^2.0",
"symfony/asset": "^4.0",
"symfony/console": "4.2.*",
"symfony/flex": "^1.1",
"symfony/framework-bundle": "4.2.*",
"symfony/yaml": "4.2.*"
},
"require-dev": {
"symfony/dotenv": "4.2.*",
"symplify/easy-coding-standard": "^4.3",
"phpstan/phpstan": "^0.10.6",
"rregeer/phpunit-coverage-check": "^0.1.6",
"slevomat/coding-standard": "~4.0"
},
"config": {
"bin-dir": "bin/",
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
},
"phpstan": "vendor/bin/phpstan analyse --level max src tests",
"php-insights": "./vendor/bin/phpinsights",
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.2.*"
}
}
}
11 changes: 11 additions & 0 deletions .doc/example-php/easy-coding-standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
imports:
- { resource: 'vendor/symplify/easy-coding-standard/config/symfony.yml' }

parameters:
indentation: tab # "spaces" by default
exclude_checkers:
# from PHP CS Fixer Symfony set
- 'PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer'
- 'PhpCsFixer\Fixer\Operator\NewWithBracesFixer'
- 'PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer'
- 'PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer'
7 changes: 7 additions & 0 deletions .doc/example-php/example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MAILGUN_API_KEY=xxxx
MAILGUN_DOMAIN=mg.gotham.com
MAILGUN_FROM="Batman <[email protected]>"
MAILGUN_REPLY_TO="Batman <[email protected]>"
STATUS=dev
[email protected]
APP_PUBLIC_DIR=/home/user/unicorn-mailing/.doc/example-php
222 changes: 222 additions & 0 deletions .doc/example-php/src/service/Mailgun/Sender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php
declare(strict_types=1);

namespace Service\Mailgun;

use Mailgun\Mailgun;
use Mailgun\Model\Message\SendResponse;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class Sender
{
public const SUCCESS_RESPONSE = 'Queued. Thank you.';

private $key;
private $domain;
private $from;
private $to;
private $cc;
private $bcc;
private $reply_to;
private $subject;
private $attachment;
private $template;
private $inline;
private $public_dir;
private $variables;

/** @var SendResponse */
private $response = null;

private function __construct(): void
{
$this->public_dir = getenv('APP_PUBLIC_DIR');
$this->key = getenv('MAILGUN_API_KEY');
$this->domain = getenv('MAILGUN_DOMAIN');
$this->from = getenv('MAILGUN_FROM');
$this->reply_to = getenv('MAILGUN_REPLY_TO');
$this->to = null;
$this->subject = null;
$this->template = null;
$this->attachment = [];
$this->inline = [];
$this->variables = [];
}

public function addAttachment(string $path): self
{
if(!file_exists($path)){
throw new \RuntimeException("Attachment: `{$path}` not exists!");
}

$this->attachment[] = ['filePath'=> $path, 'filename'=> basename($path)];
return $this;
}

public function setVariables(array $vars): self
{
$this->variables = $vars;
return $this;
}


/**
* one image naming
* @param $path
* @return string
*/
private static function inlineImageName($path): string
{
return pathinfo($path, PATHINFO_BASENAME);
}

/**
* @param array $array
* @return Sender
*/
public function setInlineImages(array $array): self
{
$inline = [];
foreach ($array as $path){
$inline[] = [
'filePath' => "{$this->public_dir}/{$path}",
'filename' => self::inlineImageName($path),
];
}

$this->inline = $inline;
return $this;
}

public function setReceiver(string $to): self
{
$this->to = $to;
return $this;
}

public function getReceiver(): string
{
return $this->to;
}

public function setCc(string $email): self
{
$this->cc = $email;
return $this;
}

public function getCc(): ?string
{
return $this->cc;
}

public function removeCc(): self
{
$this->cc = null;
return $this;
}

public function setBcc(string $email): self
{
$this->bcc = $email;
return $this;
}

public function getBcc(): ?string
{
return $this->bcc;
}

public function removeBcc(): self
{
$this->bcc = null;
return $this;
}

public function setReplyTo(string $string): self
{
$this->reply_to = $string;
return $this;
}

public function setSubject(string $txt): self
{
$this->subject = $txt;
return $this;
}

public function getSubject(): string
{
return $this->subject;
}

public function matchSuccessResponse(string $response_message): bool
{
$result = strpos(strtolower(self::SUCCESS_RESPONSE), strtolower($response_message)) !== false;
if($result){
return true;
}

return false;
}

public function sendEmail(): bool
{
if(null === $this->subject || strlen($this->subject) < 5){
// @todo: console log of sender object
throw new \RuntimeException('Subject should be defined!');
}

$mg = Mailgun::create($this->key);
$params = [
'from' => $this->from,
'to' => $this->to,
'subject' => $this->subject,
'template' => $this->template,
't:text' => 'yes',
'o:tracking' => 'yes',
'o:tracking-opens' => 'yes',
'o:dkim' => 'yes',
'h:Content-Type' => 'multipart/related',
'h:X-Mailgun-Variables' => $this->variables,
];

if(null !== $this->cc)
{
$params['cc'] = $this->getCc();
}

if(null !== $this->bcc)
{
$params['bcc'] = $this->getBcc();
}

if(count($this->attachment) > 0)
{
$params['attachment'] = $this->attachment;
}

if(count($this->inline) > 0)
{
$params['inline'] = $this->inline;
}

if(!empty($this->reply_to))
{
$params['h:Reply-To'] = $this->reply_to;
}

/** @var SendResponse */
$this->response = $mg->messages()->send($this->domain, $params);

// clear our object for cycle using
$this->setDefaultValues();

return $this->matchSuccessResponse($this->response->getMessage());
}

public function getResponse(): SendResponse
{
return $this->response;
}
}