Files
KI/common/Controller/Web/Contacto.php

64 lines
2.0 KiB
PHP
Raw Normal View History

2020-05-19 15:20:42 -04:00
<?php
namespace ProVM\KI\Common\Controller\Web;
2020-06-09 18:06:36 -04:00
use Psr\Container\ContainerInterface as Container;
2020-05-19 15:20:42 -04:00
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
2020-06-09 18:06:36 -04:00
use ProVM\KI\Common\Alias\Message;
use ProVM\KI\Common\Alias\Mailer;
use ProVM\KI\Common\Alias\View;
2020-05-19 15:20:42 -04:00
class Contacto {
2020-06-09 18:06:36 -04:00
public function __invoke(Request $request, Response $response, View $view): Response {
2020-05-19 15:20:42 -04:00
return $view->render($response, 'contacto');
}
2020-06-09 18:06:36 -04:00
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);
}
2020-05-19 15:20:42 -04:00
}