Desacople e implementacion de email
This commit is contained in:
@ -1,12 +1,63 @@
|
||||
<?php
|
||||
namespace ProVM\KI\Common\Controller\Web;
|
||||
|
||||
use Psr\Container\ContainerInterface as Container;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Slim\Views\Blade as View;
|
||||
use ProVM\KI\Common\Alias\Message;
|
||||
use ProVM\KI\Common\Alias\Mailer;
|
||||
use ProVM\KI\Common\Alias\View;
|
||||
|
||||
class Contacto {
|
||||
public function __invoke(Request $request, Response $response, View $view) {
|
||||
public function __invoke(Request $request, Response $response, View $view): Response {
|
||||
return $view->render($response, 'contacto');
|
||||
}
|
||||
public function enviar(Request $request, Response $response, Container $container, Mailer $mailer): Response {
|
||||
$post = $request->getParsedBody();
|
||||
$body = [];
|
||||
$filename = implode(DIRECTORY_SEPARATOR, [
|
||||
$container->get('folders.templates'),
|
||||
'contacto',
|
||||
'email.php'
|
||||
]);
|
||||
$html = file_get_contents($filename);
|
||||
foreach ($post as $key => $val) {
|
||||
$body []= ucwords($key) . ': ' . $val;
|
||||
$html = str_replace('{{$' . $key . '}}', $val, $html);
|
||||
}
|
||||
$body = implode(PHP_EOL, $body);
|
||||
$subject = 'Contacto Web - ' . $post['nombre'];
|
||||
|
||||
$message = $container->make(Message::class)
|
||||
->setFrom($container->get('emails')[0])
|
||||
->addTo($container->get('emails')[0])
|
||||
->addCc($container->get('emails')[1])
|
||||
->setSubject($subject)
|
||||
->setBody($body)
|
||||
->setHTMLBody($html);
|
||||
$status = $mailer->send($message);
|
||||
$message = $container->make(Message::class)
|
||||
->setFrom($container->get('emails')[0])
|
||||
->addTo($post['mail'], $post['nombre'])
|
||||
->setSubject($subject)
|
||||
->setBody('Su correo a CapitalInvestments ha sido recibido.');
|
||||
$mailer->send($message);
|
||||
|
||||
$output = [
|
||||
'informacion' => $post,
|
||||
'estado' => $status,
|
||||
'mail' => [
|
||||
'from' => $post['mail'],
|
||||
'to' => $container->get('emails')[0],
|
||||
'cc' => $container->get('emails')[1],
|
||||
'asunto' => $subject,
|
||||
'mensaje' => $body,
|
||||
'html' => $html
|
||||
]
|
||||
];
|
||||
$response->getBody()->write(json_encode($output));
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withStatus(201);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user