Spaces:
Sleeping
Sleeping
File size: 5,818 Bytes
343aa99 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
<?php
namespace SoftEdge;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
/**
* Email Service for SoftEdge Corporation
* Handles contact form submissions and email sending
*/
class EmailService
{
private PHPMailer $mailer;
private array $config;
public function __construct()
{
$this->config = $this->loadConfig();
$this->setupMailer();
}
/**
* Load email configuration from environment
*/
private function loadConfig(): array
{
return [
'host' => $_ENV['SMTP_HOST'] ?? 'smtp.gmail.com',
'port' => (int)($_ENV['SMTP_PORT'] ?? 587),
'username' => $_ENV['SMTP_USERNAME'] ?? '',
'password' => $_ENV['SMTP_PASSWORD'] ?? '',
'encryption' => $_ENV['SMTP_ENCRYPTION'] ?? 'tls',
'from_email' => $_ENV['SMTP_FROM_EMAIL'] ?? 'softedgecorporation@gmail.com',
'from_name' => $_ENV['SMTP_FROM_NAME'] ?? 'SoftEdge Corporation'
];
}
/**
* Setup PHPMailer instance
*/
private function setupMailer(): void
{
$this->mailer = new PHPMailer(true);
// Server settings
$this->mailer->isSMTP();
$this->mailer->Host = $this->config['host'];
$this->mailer->SMTPAuth = true;
$this->mailer->Username = $this->config['username'];
$this->mailer->Password = $this->config['password'];
$this->mailer->SMTPSecure = $this->config['encryption'];
$this->mailer->Port = $this->config['port'];
// Sender
$this->mailer->setFrom($this->config['from_email'], $this->config['from_name']);
// Encoding and charset
$this->mailer->CharSet = 'UTF-8';
$this->mailer->Encoding = 'base64';
}
/**
* Send contact email
*/
public function sendContactEmail(array $data): bool
{
try {
// Validate required fields
$this->validateContactData($data);
// Recipients
$this->mailer->addAddress('softedgecorporation@gmail.com', 'SoftEdge Corporation');
// Content
$this->mailer->isHTML(false);
$this->mailer->Subject = 'π Novo Contato do Site - ' . $data['nome'];
$this->mailer->Body = $this->buildContactEmailBody($data);
// Send email
$result = $this->mailer->send();
// Log success
error_log("Email sent successfully to: softedgecorporation@gmail.com from: {$data['email']}");
return $result;
} catch (Exception $e) {
error_log("Email sending failed: " . $this->mailer->ErrorInfo);
throw new \RuntimeException('Erro ao enviar email: ' . $e->getMessage());
}
}
/**
* Validate contact form data
*/
private function validateContactData(array $data): void
{
$required = ['nome', 'email', 'mensagem'];
$missing = [];
foreach ($required as $field) {
if (empty(trim($data[$field] ?? ''))) {
$missing[] = $field;
}
}
if (!empty($missing)) {
throw new \InvalidArgumentException('Campos obrigatΓ³rios nΓ£o preenchidos: ' . implode(', ', $missing));
}
if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('E-mail invΓ‘lido');
}
// Additional validation
if (strlen($data['nome']) < 2) {
throw new \InvalidArgumentException('Nome deve ter pelo menos 2 caracteres');
}
if (strlen($data['mensagem']) < 10) {
throw new \InvalidArgumentException('Mensagem deve ter pelo menos 10 caracteres');
}
}
/**
* Build contact email body
*/
private function buildContactEmailBody(array $data): string
{
$empresa = $data['empresa'] ?? '(nΓ£o informado)';
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
$timestamp = date('d/m/Y H:i:s');
return "βββββββββββββββββββββββββββββββββββββββ\n" .
" NOVO CONTATO DO SITE SOFTEDGE\n" .
"βββββββββββββββββββββββββββββββββββββββ\n\n" .
"π€ NOME:\n {$data['nome']}\n\n" .
"π§ E-MAIL:\n {$data['email']}\n\n" .
"π’ EMPRESA/PROJETO:\n {$empresa}\n\n" .
"π¬ MENSAGEM:\n " . str_replace("\n", "\n ", $data['mensagem']) . "\n\n" .
"βββββββββββββββββββββββββββββββββββββββ\n" .
"π
Data: {$timestamp}\n" .
"π IP: {$ip}\n" .
"βββββββββββββββββββββββββββββββββββββββ\n";
}
/**
* Send notification email (for internal use)
*/
public function sendNotification(string $subject, string $message): bool
{
try {
$this->mailer->clearAddresses();
$this->mailer->addAddress('softedgecorporation@gmail.com', 'SoftEdge Corporation');
$this->mailer->isHTML(false);
$this->mailer->Subject = 'π ' . $subject;
$this->mailer->Body = $message;
return $this->mailer->send();
} catch (Exception $e) {
error_log("Notification email failed: " . $this->mailer->ErrorInfo);
return false;
}
}
}
|