| Server IP : 74.208.250.37 / Your IP : 216.73.216.114 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ubuntu 6.8.0-124-generic #124-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 13:00:45 UTC 2026 x86_64 User : miferval ( 1000) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/brainwavemx/app/Controller/ |
Upload File : |
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Model\ConfigModel;
use App\Model\ContactModel;
use Mosaico\Controller\BaseController;
use PHPMailer\PHPMailer\PHPMailer;
class MailerController extends BaseController
{
/**
* @var PHPMailer
*/
protected $mail;
/**
* @var string
*/
protected $address;
/**
* @var string
*/
protected $cc;
/**
* @var string
*/
protected $bcc;
protected $host;
protected $username;
protected $password;
protected $port;
protected $fromName;
protected $from;
protected $config;
protected $contact;
public function __construct()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$this->mail = new PHPMailer();
$this->config = new ConfigModel();
$this->contact = new ContactModel();
$this->address = 'miguelfernandezdelvalle@gmail.com';
$this->cc = '';
//$this->address = 'miguelfernandezdelvalle@gmail.com';
//$this->cc = '';
$this->bcc = 'miguelfernandezdelvalle@gmail.com';
$this->host = '';
$this->username = '';
$this->password = '';
$this->port = 465;
$this->fromName = '';
$this->from = '';
}
public function sendContact()
{
$env = $this->getEnv();
if (!empty($fields['position'])) {
$response['message'] = 'honeypot';
echo json_encode($response);
return false;
}
$data = $_POST;
$data['address'] = $data['email'];
$data['cc'] = '';
$data['bcc'] = $this->bcc;
$data['subject'] = $this->contact->getSubject($data['id_subject']);
$data['bcc'] = '';
$data['from'] = '';
$data['type'] = 'contact';
$data['ip'] = $this->getIp();
$data['content'] = '<p>Email: ' . $data['email'] . '</p>' .
'<p>' . $data['message'] . '</p>' .
'<p>IP: ' . $this->getIp() . '</p>';
$data['alt'] = '';
return $this->sendForm($data);
}
public function sendForm($data)
{
$env = $this->getEnv();
$address = $this->config->getValue('mail_address') ? : $env['MAIL_TO'];
$cc = $this->config->getValue('mail_cc') ? : $env['MAIL_CC'];
$bcc = $this->config->getValue('mail_bcc') ? : $env['MAIL_BCC'];
$host = $this->config->getValue('mail_host') ? : $env['MAIL_HOST'];
$username = $this->config->getValue('mail_username') ? : $env['MAIL_USER'];
$password = $this->config->getValue('mail_password') ? : $env['MAIL_PASS'];
$port = $this->config->getValue('mail_port') ? : $env['MAIL_PORT'];
$fromName = $this->config->getValue('mail_fromname') ? : $env['MAIL_FROM'];
$from = $this->config->getValue('mail_from') ? : $env['MAIL_FROM'];
isset($data['debug']) ?
$debug = true :
$debug = false;
!isset($_SESSION['token']) ?
$_SESSION['token'] = '' :
$_SESSION['token'] = $_SESSION['token'];
switch ($port) {
case '587':
$secure = 'tls';
$auth = true;
break;
case '465':
$secure = 'ssl';
$auth = true;
break;
default:
$secure = '';
$auth = false;
break;
}
if ($_SESSION['token'] == $data['token']) :
$this->mail->CharSet = "UTF-8";
$this->mail->isSMTP();
$this->mail->SMTPDebug = $debug;
$this->mail->Debugoutput = 'html';
$this->mail->Host = $host;
$this->mail->SMTPAuth = $auth;
$this->mail->Username = $username;
$this->mail->Password = $password;
$this->mail->SMTPSecure = $secure;
/*
$this->mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => FALSE,
'verify_peer_name' => FALSE,
'allow_self_signed' => TRUE
)
);
*/
$this->mail->Port = $port;
$this->mail->FromName = $fromName;
$this->mail->Subject = $data['subject'];
$this->mail->Body = "<br>" . $data['content'];
$this->mail->AltBody = $data['alt'];
$this->mail->AltBody = $data['content'];
$this->mail->From = $from;
$this->mail->AddAddress($address);
//Add carbon copy
if(!empty($cc)){
$cc = explode(',', $data['cc']);
foreach ($cc as $key => $value) {
$this->mail->AddCC($value);
}
}
//Add hidden copy
if(!empty($bcc)){
$this->mail->AddBCC($bcc);
}
$this->mail->SetFrom($data['from']);
if (!$this->mail->send()) {
$response['status'] = "error";
$response['message'] = "<div class='alert alert-danger center'>Mailer Error: " .
$this->mail->ErrorInfo .
//" | Host: " . $host .
//" | Username: " . $username .
//" | Password: " . $password .
//" | Port: " . $port .
" | From: " . $from .
"</div>";
} else {
$response['status'] = "ok";
switch ($data['type']) {
case 'contact':
$response['message'] = '<div class="alert alert-success center">
<div>Your message has been sent</div>
</div>';
break;
default:
$response['message'] = '<div class="alert alert-success center">
<div>Your message has been sent</div>
</div>';
break;
}
}
$this->resetMailToken();
else :
$response['status'] = 'token';
//$response['message'] = "<div class='alert alert-danger center'>Mailer Error: Token error</div>";
$response['message'] = "Token is not valid please refresh the page.";
endif;
//add contact
$data['send_status'] = $response['status'];
$this->addContact($data);
return json_encode($response);
//return $_SESSION['token'];
}
public function addContact($data)
{
$id_contact = $this->contact->getContactByEmail($data['email']);
if (empty($id_contact)) {
$id_contact = $this->contact->addContact($data);
}
$data['id_contact'] = $id_contact;
$this->contact->addContactData($data);
}
public function getIp()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
public function resetMailToken()
{
unset($_SESSION['token']);
$_SESSION['token'] = bin2hex(openssl_random_pseudo_bytes(16));
}
}