Testing email

This commit is contained in:
R. Eric Wheeler 2016-07-15 10:02:18 -07:00
parent 86732924bb
commit 2b33856e17
9 changed files with 104 additions and 52 deletions

View File

@ -117,6 +117,7 @@ class App extends Application
public function registerExtenders() public function registerExtenders()
{ {
if (!$this['debug']) { if (!$this['debug']) {
$this->log('In Error handler.');
$this->error(function (\Exception $e, \Symfony\Component\HttpFoundation\Request $request, $code) { $this->error(function (\Exception $e, \Symfony\Component\HttpFoundation\Request $request, $code) {
switch ($code) { switch ($code) {
case 405: case 405:
@ -129,10 +130,13 @@ class App extends Application
$matches = 'Available methods are unknown.'; $matches = 'Available methods are unknown.';
} }
$message = json_encode(['status' => 'error', 'message' => 'Method not allowed', 'allowedMethods' => $matches, 'requestedMethod' => $request->getMethod(), 'code' => $code]); $message = json_encode(['status' => 'error', 'message' => 'Method not allowed', 'allowedMethods' => $matches, 'requestedMethod' => $request->getMethod(), 'code' => $code]);
//$message = 'Sorry bout that.<br />' . $e->getMessage(); break;
case 500:
$message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]);
break; break;
default: default:
$message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]); $message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]);
break;
} }
return new \Symfony\Component\HttpFoundation\Response($message, $code); return new \Symfony\Component\HttpFoundation\Response($message, $code);
}); });

View File

@ -10,3 +10,7 @@ app:
captcha_sitekey: 6LcvmSQTAAAAAMmf9w6mhCbpdLvknuD9SGVHT0q- captcha_sitekey: 6LcvmSQTAAAAAMmf9w6mhCbpdLvknuD9SGVHT0q-
captcha_secret: 6LcvmSQTAAAAAITkvYJjgLar1LqGGLz-ic0ZMiXo captcha_secret: 6LcvmSQTAAAAAITkvYJjgLar1LqGGLz-ic0ZMiXo
theme: default theme: default
smtp_host: smtp.gmail.com
smtp_port: 465
smtp_user: eric@rewiv.com
smtp_password: 'P*8ic32!100023&p'

View File

@ -9,7 +9,8 @@ app:
captcha_sitekey: 6LcvmSQTAAAAAMmf9w6mhCbpdLvknuD9SGVHT0q- captcha_sitekey: 6LcvmSQTAAAAAMmf9w6mhCbpdLvknuD9SGVHT0q-
captcha_secret: 6LcvmSQTAAAAAITkvYJjgLar1LqGGLz-ic0ZMiXo captcha_secret: 6LcvmSQTAAAAAITkvYJjgLar1LqGGLz-ic0ZMiXo
theme: default theme: default
#twig_paths: smtp_host: localhost
# - views smtp_port: 465
#twig_template: uikit.html.twig smtp_user: root
smtp_password: 123

View File

@ -29,6 +29,8 @@ use Silex\Provider\{
VarDumperServiceProvider, VarDumperServiceProvider,
WebProfilerServiceProvider WebProfilerServiceProvider
}; };
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Bridge\Monolog\Logger; use Symfony\Bridge\Monolog\Logger;
use WhoopsPimple\WhoopsServiceProvider; use WhoopsPimple\WhoopsServiceProvider;
@ -36,6 +38,9 @@ $app->register(new ConfigServiceProvider(), [
'config.path' => $app->getConfDirectory() . '/config.yml', 'config.path' => $app->getConfDirectory() . '/config.yml',
]); ]);
ErrorHandler::register();
ExceptionHandler::register(false);
$app->setDebug(); $app->setDebug();
@ -60,21 +65,32 @@ $app
'monolog.level' => $app->getDebug() ? Logger::DEBUG : Logger::INFO, 'monolog.level' => $app->getDebug() ? Logger::DEBUG : Logger::INFO,
'monolog.handler' => new \Monolog\Handler\StreamHandler('php://stderr'), 'monolog.handler' => new \Monolog\Handler\StreamHandler('php://stderr'),
]) ])
->register(new \Silex\Provider\SwiftmailerServiceProvider()); ->register(new \Silex\Provider\SwiftmailerServiceProvider())
->register(new ConsoleServiceProvider(), [
'console.name' => 'Resume.PHP',
'console.version' => '0.0.1',
'console.project_directory' => $app->getAppDirectory(),
]);
if(false === getenv('SPARKPOST_API_KEY')) { if(false === getenv('SPARKPOST_API_KEY') && null !== $app->config('app.smtp_host')) {
$app['swiftmailer.transport'] = new Swift_SendmailTransport(); $app['swiftmailer.options'] = [
$app->log('We are local'); 'host' => $app->config('app.smtp_host'),
} else { 'port' => $app->config('app.smtp_post'),
'username' => $app->config('app.smtp_user'),
'password' => $app->config('app.smtp_password'),
];
$app->log('Setting up local email.');
} elseif (false !== getenv('SPARKPOST_API_KEY')) {
$app['swiftmailer.options'] = [ $app['swiftmailer.options'] = [
'host' => getenv('SPARKPOST_SMTP_HOST'), 'host' => getenv('SPARKPOST_SMTP_HOST'),
'port' => getenv('SPARKPOST_SMTP_PORT'), 'port' => getenv('SPARKPOST_SMTP_PORT'),
'username' => getenv('SPARKPOST_SMTP_USERNAME'), 'username' => getenv('SPARKPOST_SMTP_USERNAME'),
'password' => getenv('SPARKPOST_SMTP_PASSWORD'), 'password' => getenv('SPARKPOST_SMTP_PASSWORD'),
'encryption' => 'tls', 'encryption' => 'tls',
'auth_mode' => 'plain',
]; ];
$app->log('We are on heroku.'); $app->log('Setting up sparkpost email.');
} else {
$app['swiftmailer.transport'] = new Swift_SendmailTransport();
} }
$app->register(new RoutingServiceProvider()) $app->register(new RoutingServiceProvider())
@ -86,12 +102,7 @@ if ($app['debug'] || 0 === strcasecmp($app['env'], 'dev')) {
'profiler.cache_dir' => $app->getDataDirectory() . '/cache/profiler', 'profiler.cache_dir' => $app->getDataDirectory() . '/cache/profiler',
]) ])
->register(new WhoopsServiceProvider()) ->register(new WhoopsServiceProvider())
->register(new VarDumperServiceProvider()) ->register(new VarDumperServiceProvider());
->register(new ConsoleServiceProvider(), [
'console.name' => 'Resume.PHP',
'console.version' => '0.0.1',
'console.project_directory' => $app->getAppDirectory(),
]);
} }
if (null === $app->config('app.schema')) { if (null === $app->config('app.schema')) {
$app->config('app.schema', 'https://raw.githubusercontent.com/jsonresume/resume-schema/v1.0.0/schema.json'); $app->config('app.schema', 'https://raw.githubusercontent.com/jsonresume/resume-schema/v1.0.0/schema.json');

View File

@ -19,6 +19,7 @@
<script src="{{ asset('js/vendor/uikit.min.js') }}"></script> <script src="{{ asset('js/vendor/uikit.min.js') }}"></script>
<script src="{{ asset('js/vendor/sticky.min.js') }}"></script> <script src="{{ asset('js/vendor/sticky.min.js') }}"></script>
<script src="{{ asset('js/vendor/notify.min.js') }}"></script> <script src="{{ asset('js/vendor/notify.min.js') }}"></script>
<script src="{{ asset('js/vendor/es6-shim.min.js') }}"></script>
{% block javascripts_head %}{% endblock %} {% block javascripts_head %}{% endblock %}
{% block inline_js_head %}{% endblock %} {% block inline_js_head %}{% endblock %}
</head> </head>

View File

@ -257,6 +257,13 @@
</ul> </ul>
</div> </div>
</div> </div>
<div id="spinner-modal" class="uk-modal">
<div class="uk-modal-dialog uk-animation-scale-up uk-animation-bottom-center">
<p class="uk-text-center uk-h2 uk-margin uk-text-primary">Sending message ...
<i class="uk-margin-left uk-icon uk-icon-spinner uk-icon-spin"></i>
</p>
</div>
</div>
{% endblock %} {% endblock %}
{% block javascripts_foot %} {% block javascripts_foot %}

View File

@ -105,7 +105,7 @@ class ApiControllerProvider implements ControllerProviderInterface {
], ],
'_token' => [ '_token' => [
new EqualTo(['value' => $csrf, 'message' => 'Invalid token.']), new EqualTo(['value' => $csrf, 'message' => 'Invalid token.']),
] ],
] ]
), ),
]; ];
@ -114,9 +114,13 @@ class ApiControllerProvider implements ControllerProviderInterface {
$valid = $app['validator']->validate($contactFormData, new Collection($constraints)); $valid = $app['validator']->validate($contactFormData, new Collection($constraints));
if(count($valid) > 0) { if (count($valid) > 0) {
$sanitizeProperty = function() use ($valid) { $sanitizeProperty = function () use ($valid) {
return str_replace(['][', '[', ']'], ['_','',''], $valid[0]->getPropertyPath()); return str_replace(['][', '[', ']'], [
'_',
'',
'',
], $valid[0]->getPropertyPath());
}; };
return new JsonResponse([ return new JsonResponse([
@ -124,43 +128,50 @@ class ApiControllerProvider implements ControllerProviderInterface {
'message' => $valid[0]->getMessage(), 'message' => $valid[0]->getMessage(),
'id' => $sanitizeProperty(), 'id' => $sanitizeProperty(),
'const' => $valid[0]->getCode(), 'const' => $valid[0]->getCode(),
'code' => 256 'code' => 256,
], 256); ], 256);
} else { }
else {
$contactFormName = $contactFormData['contact']['name']; $contactFormName = $contactFormData['contact']['name'];
$contactFormEmail = $contactFormData['contact']['email']; $contactFormEmail = $contactFormData['contact']['email'];
$contactFormMessage = $contactFormData['contact']['message']; $contactFormMessage = $contactFormData['contact']['message'];
$request->getSession()->remove('_csrf/contact'); $request->getSession()->remove('_csrf/contact');
$failures = '';
try { $sent = $app['mailer']->send(\Swift_Message::newInstance()
$app->mail(\Swift_Message::newInstance()
->setSubject('[Resume] Message') ->setSubject('[Resume] Message')
->setFrom([$contactFormEmail => $contactFormName]) ->setFrom([$contactFormEmail => $contactFormName])
->setTo($app->config('app.email')) ->setTo($app->config('app.email'))
->setBody($contactFormMessage) ->setBody($contactFormMessage)
); , $failures);
$returnData = [ if($sent > 0) {
return new JsonResponse([
'status' => 'success', 'status' => 'success',
'message' => 'Message successfully sent.', 'message' => 'Message successfully sent.',
'code' => 201, 'code' => 201,
'data' => $contactFormData, 'data' => $contactFormData,
]; 'failed' => $failures,
} catch(\Exception $e) { 'sent' => $sent,
$returnData = [ ], 200);
} else {
return new JsonResponse([
'status' => 'error', 'status' => 'error',
'message' => 'Could not send message.', 'message' => 'There was an error sending the message.',
'code' => 256, 'code' => 255,
'data' => $e->getMessage(), 'data' => $contactFormData,
]; 'failed' => $failures,
'sent' => $sent,
], 255);
}
} }
return new JsonResponse($returnData, 201);
}
})->method('GET|POST')->bind('api_message'); })->method('GET|POST')->bind('api_message');
$controllers->get('/v1/emailTest', function(Request $request) use ($app) {
$controllers->get('/v1/emailTest', function (Request $request) use ($app) {
try { try {
$app->mail(\Swift_Message::newInstance() $app->mail(\Swift_Message::newInstance()
->setSubject('[Resume] Message') ->setSubject('[Resume] Message')
@ -168,10 +179,10 @@ class ApiControllerProvider implements ControllerProviderInterface {
->setTo('eric@ericwheeler.net') ->setTo('eric@ericwheeler.net')
->setBody('Testing message.') ->setBody('Testing message.')
); );
} } catch (\Exception $e) {
catch(\Exception $e) {
dump($e->getMessage()); dump($e->getMessage());
} }
return new Response('Hello'); return new Response('Hello');
}); });

View File

@ -34,6 +34,8 @@ jq(document).ready(function (jq) {
}); });
}); // Phone form }); // Phone form
jq('form[name=contact]').on('submit', function(event) { jq('form[name=contact]').on('submit', function(event) {
contactModal = UIkit.modal('#spinner-modal', { modal:false, center:true, bgclose:false });
contactModal.show();
jq.post(jq(this).attr('action'), jq(this).serialize(), function(response) { jq.post(jq(this).attr('action'), jq(this).serialize(), function(response) {
if(response.status !== 'success') { if(response.status !== 'success') {
jq('#' + response.id).addClass('uk-form-danger'); jq('#' + response.id).addClass('uk-form-danger');
@ -54,6 +56,14 @@ jq(document).ready(function (jq) {
$wrapper.empty().append($thankYouText).append($button); $wrapper.empty().append($thankYouText).append($button);
jq('a[href="#contact-form-wrapper"]').replaceWith('eric@ericwheeler.net'); jq('a[href="#contact-form-wrapper"]').replaceWith('eric@ericwheeler.net');
} }
}).fail(function(response, code, resp) {
console.log(response);
console.log(code);
console.log(resp);
contactModal.hide();
}).done(function () {
contactModal.hide();
}); });
}); });

View File

@ -9,12 +9,15 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../vendor/autoload.php';
$app = new App(); $app = new App();
require_once $app->getAppDirectory() . '/providers.php'; require_once $app->getAppDirectory() . '/providers.php';
$app->mount('/', new \Sikofitt\Controller\ResumeControllerProvider()); $app->mount('/', new \Sikofitt\Controller\ResumeControllerProvider());
$app->mount('/api', new \Sikofitt\Controller\ApiControllerProvider()); $app->mount('/api', new \Sikofitt\Controller\ApiControllerProvider());
$app->run(); $app->run();