81 lines
3.0 KiB
PHP
Executable File
81 lines
3.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Contact\Providers;
|
|
|
|
use Kernel\Application;
|
|
use Symfony\Component\Mailer\Mailer;
|
|
use Symfony\Component\Mailer\Transport;
|
|
use Symfony\Component\Mime\Email;
|
|
use Modules\Contact\Models\Contact;
|
|
|
|
class ContactServiceProvider
|
|
{
|
|
public function register()
|
|
{
|
|
}
|
|
|
|
public function boot()
|
|
{
|
|
$app = Application::getInstance();
|
|
$app->addPublicPath('/contact*');
|
|
|
|
if (function_exists('add_filter')) {
|
|
add_filter('contact.subject_sanitize', function ($subject) {
|
|
$subject = is_string($subject) ? $subject : '';
|
|
$subject = strip_tags($subject);
|
|
$subject = preg_replace('/\s+/', ' ', trim($subject));
|
|
return $subject;
|
|
}, 10);
|
|
|
|
add_filter('contact.message_sanitize', function ($message) {
|
|
$message = is_string($message) ? $message : '';
|
|
$message = strip_tags($message);
|
|
$message = trim($message);
|
|
return $message;
|
|
}, 10);
|
|
}
|
|
|
|
if (function_exists('add_action')) {
|
|
add_action('contact.submitted', function (Contact $contact) {
|
|
$dsn = $_ENV['MAILER_DSN'] ?? '';
|
|
if (!$dsn) {
|
|
$transport = Transport::fromDsn(sprintf(
|
|
'smtp://%s:%s@%s:%s?encryption=%s',
|
|
$_ENV['MAIL_USERNAME'] ?? '',
|
|
$_ENV['MAIL_PASSWORD'] ?? '',
|
|
$_ENV['MAIL_HOST'] ?? 'localhost',
|
|
$_ENV['MAIL_PORT'] ?? '25',
|
|
$_ENV['MAIL_ENCRYPTION'] ?? ''
|
|
));
|
|
} else {
|
|
$transport = Transport::fromDsn($dsn);
|
|
}
|
|
|
|
$mailer = new Mailer($transport);
|
|
$to = $_ENV['MAIL_TO_ADDRESS'] ?? $_ENV['MAIL_FROM_ADDRESS'] ?? null;
|
|
if (!$to) {
|
|
return;
|
|
}
|
|
|
|
$email = (new Email())
|
|
->from($_ENV['MAIL_FROM_ADDRESS'] ?? 'no-reply@localhost')
|
|
->to($to)
|
|
->subject('Nova mensagem de contato: ' . ($contact->subject ?? ''))
|
|
->html(
|
|
'<h3>Nova mensagem de contato</h3>' .
|
|
'<p><strong>Nome:</strong> ' . htmlspecialchars($contact->name) . '</p>' .
|
|
'<p><strong>Email:</strong> ' . htmlspecialchars($contact->email) . '</p>' .
|
|
'<p><strong>Telefone:</strong> ' . htmlspecialchars((string)$contact->phone) . '</p>' .
|
|
'<p><strong>Assunto:</strong> ' . htmlspecialchars($contact->subject) . '</p>' .
|
|
'<p><strong>Mensagem:</strong><br>' . nl2br(htmlspecialchars($contact->message)) . '</p>'
|
|
);
|
|
|
|
try {
|
|
$mailer->send($email);
|
|
} catch (\Throwable $e) {
|
|
}
|
|
}, 10);
|
|
}
|
|
}
|
|
}
|