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()
{
if (!$this['debug']) {
$this->log('In Error handler.');
$this->error(function (\Exception $e, \Symfony\Component\HttpFoundation\Request $request, $code) {
switch ($code) {
case 405:
@ -129,10 +130,13 @@ class App extends Application
$matches = 'Available methods are unknown.';
}
$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;
default:
$message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]);
break;
}
return new \Symfony\Component\HttpFoundation\Response($message, $code);
});

View File

@ -10,3 +10,7 @@ app:
captcha_sitekey: 6LcvmSQTAAAAAMmf9w6mhCbpdLvknuD9SGVHT0q-
captcha_secret: 6LcvmSQTAAAAAITkvYJjgLar1LqGGLz-ic0ZMiXo
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_secret: 6LcvmSQTAAAAAITkvYJjgLar1LqGGLz-ic0ZMiXo
theme: default
#twig_paths:
# - views
#twig_template: uikit.html.twig
smtp_host: localhost
smtp_port: 465
smtp_user: root
smtp_password: 123

View File

@ -29,6 +29,8 @@ use Silex\Provider\{
VarDumperServiceProvider,
WebProfilerServiceProvider
};
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Bridge\Monolog\Logger;
use WhoopsPimple\WhoopsServiceProvider;
@ -36,6 +38,9 @@ $app->register(new ConfigServiceProvider(), [
'config.path' => $app->getConfDirectory() . '/config.yml',
]);
ErrorHandler::register();
ExceptionHandler::register(false);
$app->setDebug();
@ -60,21 +65,32 @@ $app
'monolog.level' => $app->getDebug() ? Logger::DEBUG : Logger::INFO,
'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')) {
$app['swiftmailer.transport'] = new Swift_SendmailTransport();
$app->log('We are local');
} else {
if(false === getenv('SPARKPOST_API_KEY') && null !== $app->config('app.smtp_host')) {
$app['swiftmailer.options'] = [
'host' => $app->config('app.smtp_host'),
'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'] = [
'host' => getenv('SPARKPOST_SMTP_HOST'),
'port' => getenv('SPARKPOST_SMTP_PORT'),
'username' => getenv('SPARKPOST_SMTP_USERNAME'),
'password' => getenv('SPARKPOST_SMTP_PASSWORD'),
'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())
@ -86,12 +102,7 @@ if ($app['debug'] || 0 === strcasecmp($app['env'], 'dev')) {
'profiler.cache_dir' => $app->getDataDirectory() . '/cache/profiler',
])
->register(new WhoopsServiceProvider())
->register(new VarDumperServiceProvider())
->register(new ConsoleServiceProvider(), [
'console.name' => 'Resume.PHP',
'console.version' => '0.0.1',
'console.project_directory' => $app->getAppDirectory(),
]);
->register(new VarDumperServiceProvider());
}
if (null === $app->config('app.schema')) {
$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/sticky.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 inline_js_head %}{% endblock %}
</head>

View File

@ -257,6 +257,13 @@
</ul>
</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 %}
{% block javascripts_foot %}

View File

@ -75,9 +75,9 @@ class ApiControllerProvider implements ControllerProviderInterface {
// Set some validation constraints
$constraints = [
'contact' => new Collection([
'name' => [
'name' => [
new Length([
'min' => 4,
'min' => 4,
'minMessage' => 'Name must be at least 4 characters.',
]
),
@ -86,7 +86,7 @@ class ApiControllerProvider implements ControllerProviderInterface {
]
),
],
'email' => [
'email' => [
new Email([
'message' => 'Invalid email',
]),
@ -96,16 +96,16 @@ class ApiControllerProvider implements ControllerProviderInterface {
],
'message' => [
new Length([
'min' => 20,
'min' => 20,
'minMessage' => 'Message must be at least 20 characters.',
]),
new NotBlank([
'message' => 'Message must not be blank',
]),
],
'_token' => [
'_token' => [
new EqualTo(['value' => $csrf, 'message' => 'Invalid token.']),
]
],
]
),
];
@ -114,53 +114,64 @@ class ApiControllerProvider implements ControllerProviderInterface {
$valid = $app['validator']->validate($contactFormData, new Collection($constraints));
if(count($valid) > 0) {
$sanitizeProperty = function() use ($valid) {
return str_replace(['][', '[', ']'], ['_','',''], $valid[0]->getPropertyPath());
if (count($valid) > 0) {
$sanitizeProperty = function () use ($valid) {
return str_replace(['][', '[', ']'], [
'_',
'',
'',
], $valid[0]->getPropertyPath());
};
return new JsonResponse([
'status' => 'error',
'status' => 'error',
'message' => $valid[0]->getMessage(),
'id' => $sanitizeProperty(),
'const' => $valid[0]->getCode(),
'code' => 256
'id' => $sanitizeProperty(),
'const' => $valid[0]->getCode(),
'code' => 256,
], 256);
} else {
}
else {
$contactFormName = $contactFormData['contact']['name'];
$contactFormEmail = $contactFormData['contact']['email'];
$contactFormMessage = $contactFormData['contact']['message'];
$request->getSession()->remove('_csrf/contact');
$failures = '';
try {
$app->mail(\Swift_Message::newInstance()
$sent = $app['mailer']->send(\Swift_Message::newInstance()
->setSubject('[Resume] Message')
->setFrom([$contactFormEmail => $contactFormName])
->setTo($app->config('app.email'))
->setBody($contactFormMessage)
);
$returnData = [
'status' => 'success',
'message' => 'Message successfully sent.',
'code' => 201,
'data' => $contactFormData,
];
} catch(\Exception $e) {
$returnData = [
'status' => 'error',
'message' => 'Could not send message.',
'code' => 256,
'data' => $e->getMessage(),
];
, $failures);
if($sent > 0) {
return new JsonResponse([
'status' => 'success',
'message' => 'Message successfully sent.',
'code' => 201,
'data' => $contactFormData,
'failed' => $failures,
'sent' => $sent,
], 200);
} else {
return new JsonResponse([
'status' => 'error',
'message' => 'There was an error sending the message.',
'code' => 255,
'data' => $contactFormData,
'failed' => $failures,
'sent' => $sent,
], 255);
}
}
return new JsonResponse($returnData, 201);
}
})->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 {
$app->mail(\Swift_Message::newInstance()
->setSubject('[Resume] Message')
@ -168,10 +179,10 @@ class ApiControllerProvider implements ControllerProviderInterface {
->setTo('eric@ericwheeler.net')
->setBody('Testing message.')
);
}
catch(\Exception $e) {
} catch (\Exception $e) {
dump($e->getMessage());
}
return new Response('Hello');
});

View File

@ -34,6 +34,8 @@ jq(document).ready(function (jq) {
});
}); // Phone form
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) {
if(response.status !== 'success') {
jq('#' + response.id).addClass('uk-form-danger');
@ -54,6 +56,14 @@ jq(document).ready(function (jq) {
$wrapper.empty().append($thankYouText).append($button);
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.
*/
require_once __DIR__ . '/../vendor/autoload.php';
$app = new App();
require_once $app->getAppDirectory() . '/providers.php';
$app->mount('/', new \Sikofitt\Controller\ResumeControllerProvider());
$app->mount('/api', new \Sikofitt\Controller\ApiControllerProvider());
$app->run();