Migrated Mail from native to Sendgrid API

Change-Id: I2dbc7947710f3da301e8361842a5fd0ffd026f6c
This commit is contained in:
smarcet 2019-04-09 19:46:54 -03:00
parent e37dd1a70b
commit ae650777ef
8 changed files with 276 additions and 35 deletions

View File

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

View File

@ -0,0 +1,188 @@
<?php namespace App\Http\Utils\Logs;
/**
* 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

@ -1,5 +1,6 @@
<?php namespace App\Providers;
use App\Http\Utils\Logs\LaravelMailerHandler;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
@ -7,7 +8,6 @@ use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use models\main\ChatTeamPermission;
use models\main\PushNotificationMessagePriority;
use Monolog\Handler\NativeMailerHandler;
/**
* Class AppServiceProvider
@ -96,8 +96,8 @@ class AppServiceProvider extends ServiceProvider
$from = Config::get('log.from_email', '');
if (!empty($to) && !empty($from)) {
$subject = 'openstackid-resource-server error';
$handler = new NativeMailerHandler($to, $subject, $from);
$subject = Config::get('log.email_subject', 'openstackid-resource-server error');
$handler = new LaravelMailerHandler($to, $subject, $from);
$handler->setLevel(Config::get('log.email_level', 'error'));
$logger->pushHandler($handler);
}

View File

@ -15,24 +15,25 @@
],
"require": {
"php": "^7.1.3",
"ext-json": "*",
"ext-pdo": "*",
"cocur/slugify": "^2.3",
"ezyang/htmlpurifier": "4.7.0",
"fideloper/proxy": "^4.0",
"glenscott/url-normalizer": "^1.4",
"google/apiclient": "^2.2",
"guzzlehttp/guzzle": "^6.3",
"idct/sftp-client": "dev-master",
"laravel-doctrine/extensions": "1.0.*",
"laravel-doctrine/orm": "1.4.*",
"laravel/framework": "5.6.*",
"laravel/tinker": "^1.0",
"php-opencloud/openstack": "dev-master",
"predis/predis": "1.0.*",
"ext-json":"*",
"ext-pdo":"*",
"ezyang/htmlpurifier": "4.7.0",
"glenscott/url-normalizer" : "^1.4",
"laravel-doctrine/orm":"1.4.*",
"laravel-doctrine/extensions": "1.0.*",
"cocur/slugify": "^2.3",
"guzzlehttp/guzzle": "^6.3",
"google/apiclient": "^2.2",
"s-ichikawa/laravel-sendgrid-driver": "^2.0",
"smarcet/caldavclient": "1.1.6",
"smarcet/outlook-rest-client": "dev-master",
"idct/sftp-client": "dev-master",
"symfony/yaml": "4.2.2",
"php-opencloud/openstack": "dev-master"
"symfony/yaml": "4.2.2"
},
"require-dev": {
"filp/whoops": "^2.0",

62
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "f8c3fdad0f0742b4f15e8dfee1f3034c",
"content-hash": "d9c8d933d23b34781d4010039da200c7",
"packages": [
{
"name": "cocur/slugify",
@ -3271,6 +3271,62 @@
],
"time": "2018-07-19T23:38:55+00:00"
},
{
"name": "s-ichikawa/laravel-sendgrid-driver",
"version": "2.0.6",
"source": {
"type": "git",
"url": "https://github.com/s-ichikawa/laravel-sendgrid-driver.git",
"reference": "5092c3f856581bd61b691d7e86ebb9fcb6867683"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/s-ichikawa/laravel-sendgrid-driver/zipball/5092c3f856581bd61b691d7e86ebb9fcb6867683",
"reference": "5092c3f856581bd61b691d7e86ebb9fcb6867683",
"shasum": ""
},
"require": {
"guzzlehttp/guzzle": "~5.3|~6.2",
"illuminate/mail": "~5.5"
},
"require-dev": {
"illuminate/container": "~5.5",
"illuminate/filesystem": "~5.5",
"phpunit/phpunit": "~5.7"
},
"suggest": {
"s-ichikawa/sendgrid-api-builder": "support to build json for sendgrid api"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Sichikawa\\LaravelSendgridDriver\\SendgridTransportServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Sichikawa\\LaravelSendgridDriver\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "shingo.ichikawa",
"email": "ichikawa.shingo.0829@gmail.com"
}
],
"description": "This library adds a 'sendgrid' mail driver to Laravel.",
"keywords": [
"laravel",
"sendgrid"
],
"time": "2019-04-09T14:22:02+00:00"
},
{
"name": "sabre/uri",
"version": "1.2.0",
@ -6613,9 +6669,9 @@
"aliases": [],
"minimum-stability": "dev",
"stability-flags": {
"smarcet/outlook-rest-client": 20,
"idct/sftp-client": 20,
"php-opencloud/openstack": 20
"php-opencloud/openstack": 20,
"smarcet/outlook-rest-client": 20
},
"prefer-stable": true,
"prefer-lowest": false,

View File

@ -144,7 +144,6 @@ return [
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
/*
* Application Service Providers...
*/
@ -158,6 +157,7 @@ return [
App\Http\Utils\UtilsProvider::class,
libs\utils\CustomDoctrineServiceProvider::class,
LaravelDoctrine\Extensions\BeberleiExtensionsServiceProvider::class,
Sichikawa\LaravelSendgridDriver\SendgridTransportServiceProvider::class,
],
/*
@ -172,7 +172,6 @@ return [
*/
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
'Artisan' => Illuminate\Support\Facades\Artisan::class,
'Auth' => Illuminate\Support\Facades\Auth::class,

View File

@ -1,13 +1,14 @@
<?php
return array(
return [
/**
* EMAIL ERROR LOG CONFIGURATION
*/
//The receiver of the mail
'to_email' => env('LOG_EMAIL_TO'),
'to_email' => env('LOG_EMAIL_TO'),
//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)
'level' => env('LOG_LEVEL', 'error'),
'email_level' => env('LOG_EMAIL_LEVEL', 'error'),
);
'level' => env('LOG_LEVEL', 'error'),
'email_level' => env('LOG_EMAIL_LEVEL', 'error'),
'email_subject' => env('LOG_EMAIL_SUBJECT', ''),
];

View File

@ -13,18 +13,15 @@ return [
| to have a conventional place to find your various credentials.
|
*/
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
'ses' => [
'key' => env('SES_KEY'),
'secret' => env('SES_SECRET'),
'region' => 'us-east-1',
],
'sparkpost' => [
'secret' => env('SPARKPOST_SECRET'),
],
@ -34,5 +31,7 @@ return [
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
'sendgrid' => [
'api_key' => env('SENDGRID_API_KEY'),
],
];