Migrated Mail from native to Sendgrid API

Change-Id: I751c20bdf77bd9612633f97a4ad61c47227def90
This commit is contained in:
smarcet 2019-04-11 11:25:20 -03:00
parent e83daebda0
commit 7408fbb595
7 changed files with 211 additions and 21 deletions

View File

@ -36,11 +36,8 @@ SESSION_COOKIE_SECURE=false
QUEUE_DRIVER=sync QUEUE_DRIVER=sync
MAIL_DRIVER=smtp MAIL_DRIVER=sendgrid
MAIL_HOST=mailtrap.io SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY'
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
CORS_ALLOWED_HEADERS=origin, content-type, accept, authorization, x-requested-with CORS_ALLOWED_HEADERS=origin, content-type, accept, authorization, x-requested-with
CORS_ALLOWED_METHODS=GET, POST, OPTIONS, PUT, DELETE CORS_ALLOWED_METHODS=GET, POST, OPTIONS, PUT, DELETE

View File

@ -0,0 +1,188 @@
<?php namespace App\Http\Utils\Log;
/**
* Copyright 2019 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Illuminate\Mail\Message;
use Monolog\Handler\MailHandler;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Illuminate\Support\Facades\Mail;
/**
* Class LaravelMailerHandler
* @package App\Http\Utils\Logs
*/
final class LaravelMailerHandler extends MailHandler
{
/**
* The email addresses to which the message will be sent
* @var array
*/
protected $to;
/**
* The subject of the email
* @var string
*/
protected $subject;
/**
* Optional headers for the message
* @var array
*/
protected $headers = array();
/**
* Optional parameters for the message
* @var array
*/
protected $parameters = array();
/**
* The wordwrap length for the message
* @var int
*/
protected $maxColumnWidth;
/**
* The Content-type for the message
* @var string
*/
protected $contentType = 'text/plain';
/**
* The encoding for the message
* @var string
*/
protected $encoding = 'utf-8';
protected $from = null;
/**
* @param string|array $to The receiver of the mail
* @param string $subject The subject of the mail
* @param string $from The sender of the mail
* @param int $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param int $maxColumnWidth The maximum column width that the message lines will have
*/
public function __construct($to, $subject, $from, $level = Logger::ERROR, $bubble = true, $maxColumnWidth = 70)
{
parent::__construct($level, $bubble);
$this->from = $from;
$this->to = is_array($to) ? $to : array($to);
$this->subject = $subject;
$this->addHeader(sprintf('From: %s', $from));
$this->maxColumnWidth = $maxColumnWidth;
}
/**
* Add headers to the message
*
* @param string|array $headers Custom added headers
* @return self
*/
public function addHeader($headers)
{
foreach ((array) $headers as $header) {
if (strpos($header, "\n") !== false || strpos($header, "\r") !== false) {
throw new \InvalidArgumentException('Headers can not contain newline characters for security reasons');
}
$this->headers[] = $header;
}
return $this;
}
/**
* Add parameters to the message
*
* @param string|array $parameters Custom added parameters
* @return self
*/
public function addParameter($parameters)
{
$this->parameters = array_merge($this->parameters, (array) $parameters);
return $this;
}
/**
* {@inheritdoc}
*/
protected function send($content, array $records)
{
$content = wordwrap($content, $this->maxColumnWidth);
$subject = $this->subject;
if ($records) {
$subjectFormatter = new LineFormatter($this->subject);
$subject = $subjectFormatter->format($this->getHighestRecord($records));
}
foreach ($this->to as $to) {
Mail::raw($content, function(Message $message) use($to, $subject, $content){
$message
->to($to)
->subject($subject)
->setBody($content, 'text/html')
->setFrom($this->from);
});
}
}
/**
* @return string $contentType
*/
public function getContentType()
{
return $this->contentType;
}
/**
* @return string $encoding
*/
public function getEncoding()
{
return $this->encoding;
}
/**
* @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML
* messages.
* @return self
*/
public function setContentType($contentType)
{
if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) {
throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
}
$this->contentType = $contentType;
return $this;
}
/**
* @param string $encoding
* @return self
*/
public function setEncoding($encoding)
{
if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection');
}
$this->encoding = $encoding;
return $this;
}
}

View File

@ -14,10 +14,9 @@
use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Monolog\Handler\NativeMailerHandler;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Validators\CustomValidator; use Validators\CustomValidator;
use App\Http\Utils\Log\LaravelMailerHandler;
/** /**
* Class AppServiceProvider * Class AppServiceProvider
* @package App\Providers * @package App\Providers
@ -43,9 +42,8 @@ class AppServiceProvider extends ServiceProvider
$from = Config::get('log.from_email'); $from = Config::get('log.from_email');
if (!empty($to) && !empty($from)) { if (!empty($to) && !empty($from)) {
$subject = Config::get('log.email_subject', 'openstackid-resource-server error');
$subject = 'openstackid error'; $handler = new LaravelMailerHandler($to, $subject, $from);
$handler = new NativeMailerHandler($to, $subject, $from);
$handler->setLevel(Config::get('log.email_level', 'error')); $handler->setLevel(Config::get('log.email_level', 'error'));
$logger->pushHandler($handler); $logger->pushHandler($handler);
} }

View File

@ -13,22 +13,23 @@
"type": "project", "type": "project",
"require": { "require": {
"php": "^7.1.3", "php": "^7.1.3",
"ext-json": "*",
"ext-pdo": "*",
"fideloper/proxy": "^4.0", "fideloper/proxy": "^4.0",
"laravel/framework": "5.6.*", "glenscott/url-normalizer": "1.4.*",
"laravel/tinker": "^1.0",
"zendframework/zend-crypt": "3.3.0",
"zendframework/zend-math": "3.1.1",
"ircmaxell/random-lib": "1.1.*",
"greggilbert/recaptcha": "2.1.*", "greggilbert/recaptcha": "2.1.*",
"guzzlehttp/guzzle": "6.3.3", "guzzlehttp/guzzle": "6.3.3",
"smarcet/jose4php": "dev-feature/php7.2-migration", "ircmaxell/random-lib": "1.1.*",
"glenscott/url-normalizer": "1.4.*",
"jenssegers/agent": "2.3.*", "jenssegers/agent": "2.3.*",
"laravel/framework": "5.6.*",
"laravel/tinker": "^1.0",
"laravelcollective/html": "5.6.*", "laravelcollective/html": "5.6.*",
"phpseclib/phpseclib": "2.0.11", "phpseclib/phpseclib": "2.0.11",
"predis/predis": "1.0.*", "predis/predis": "1.0.*",
"ext-json":"*", "s-ichikawa/laravel-sendgrid-driver": "^2.0",
"ext-pdo":"*" "smarcet/jose4php": "dev-feature/php7.2-migration",
"zendframework/zend-crypt": "3.3.0",
"zendframework/zend-math": "3.1.1"
}, },
"require-dev": { "require-dev": {
"filp/whoops": "^2.0", "filp/whoops": "^2.0",

View File

@ -153,6 +153,7 @@ return [
Collective\Html\HtmlServiceProvider::class, Collective\Html\HtmlServiceProvider::class,
\Providers\OAuth2\ClientAuthContextValidatorFactoryProvider::class, \Providers\OAuth2\ClientAuthContextValidatorFactoryProvider::class,
Greggilbert\Recaptcha\RecaptchaServiceProvider::class, Greggilbert\Recaptcha\RecaptchaServiceProvider::class,
Sichikawa\LaravelSendgridDriver\SendgridTransportServiceProvider::class,
], ],
/* /*

View File

@ -20,6 +20,7 @@ return array(
//The sender of the mail //The sender of the mail
'from_email' => env('LOG_EMAIL_FROM'), 'from_email' => env('LOG_EMAIL_FROM'),
//Log Level (debug, info, notice, warning, error, critical, alert) //Log Level (debug, info, notice, warning, error, critical, alert)
'level' => env('LOG_LEVEL', 'error'), 'level' => env('LOG_LEVEL', 'error'),
'email_level' => env('LOG_EMAIL_LEVEL', 'error'), 'email_level' => env('LOG_EMAIL_LEVEL', 'error'),
'email_subject' => env('LOG_EMAIL_SUBJECT', ''),
); );

View File

@ -35,4 +35,8 @@ return [
'secret' => env('STRIPE_SECRET'), 'secret' => env('STRIPE_SECRET'),
], ],
'sendgrid' => [
'api_key' => env('SENDGRID_API_KEY'),
],
]; ];