-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
147 lines (125 loc) · 4.28 KB
/
index.php
File metadata and controls
147 lines (125 loc) · 4.28 KB
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
<?php
/*
Plugin Name: QuicklyPHPMailer
Description: Plugin para enviar e-mails usando PHPMailer sem depender do PHPMailer do WordPress.
Version: 1.5
Author: Matheus Alves
Author URI: https://github.com/matthaiosalves/QuicklyPHPMailer
*/
if (!defined('ABSPATH')) {
exit;
}
function enfileirarEmail($destinatario, $assunto, $mensagem)
{
$existing_email = new WP_Query([
'post_type' => 'email_queue',
'meta_query' => [
'relation' => 'AND',
[
'key' => 'email_destinatario',
'value' => $destinatario,
'compare' => '='
],
[
'key' => 'assunto',
'value' => $assunto,
'compare' => '='
]
]
]);
if ($existing_email->have_posts()) {
return;
}
wp_insert_post([
'post_type' => 'email_queue',
'post_title' => "E-mail para {$destinatario}",
'post_content' => $mensagem,
'post_status' => 'publish',
'meta_input' => [
'email_destinatario' => $destinatario,
'assunto' => $assunto,
'attempts' => 0,
],
]);
}
function processarFilaDeEmails()
{
$query = new WP_Query([
'post_type' => 'email_queue',
'post_status' => 'publish',
'posts_per_page' => -1,
]);
$max_attempts = 3;
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$attempts = (int) get_post_meta($post_id, 'attempts', true);
$destinatario = get_post_meta($post_id, 'email_destinatario', true);
$assunto = get_post_meta($post_id, 'assunto', true);
$mensagem = get_the_content();
if ($attempts >= $max_attempts) {
error_log("E-mail para {$destinatario} falhou após {$max_attempts} tentativas.");
wp_delete_post($post_id, true);
continue;
}
if (sendEmail($destinatario, $assunto, $mensagem)) {
wp_delete_post($post_id, true);
} else {
update_post_meta($post_id, 'attempts', $attempts + 1);
}
}
wp_reset_postdata();
}
function sendEmail($destinatario, $assunto, $mensagem)
{
require_once plugin_dir_path(__FILE__) . 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require_once plugin_dir_path(__FILE__) . 'vendor/phpmailer/phpmailer/src/Exception.php';
require_once plugin_dir_path(__FILE__) . 'vendor/autoload.php';
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'HOST';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'USERNAME';
$mail->Password = 'PASSWORD';
$mail->SMTPSecure = 'tls';
$mail->setFrom('fake-email@email.com', 'Formulário - Fake Formulário');
$mail->addAddress($destinatario);
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = $assunto;
$mail->Body = $mensagem;
return $mail->send();
} catch (PHPMailer\PHPMailer\Exception $e) {
error_log("Erro ao enviar o e-mail: {$mail->ErrorInfo}");
return false;
}
}
function pluginHandleRequest()
{
if (isset($_POST["nome"])) {
$nome = isset($_POST["nome"]) ? htmlspecialchars($_POST["nome"]) : '';
$phone = isset($_POST["phone"]) ? filter_var($_POST["phone"], FILTER_SANITIZE_STRING) : '';
$mensage = isset($_POST['mensage']) ? filter_var($_POST['mensage'], FILTER_SANITIZE_STRING) : '';
if (empty($nome) || empty($phone) || empty($mensage)) {
echo 'Todos os campos são obrigatórios.';
exit;
}
$destinatario = 'fake-email@email.com';
$assunto = 'Formulario';
$mensagem = "
<p><strong>De:</strong> Formulário: Fake Formulário</p>
<p><strong>Nome:</strong> $nome</p>
<p><strong>Telefone:</strong> $phone</p>
<p><strong>Mensagem:</strong></p>
<p>$mensage</p>
<p>Mensagem enviada de: " . home_url() . "</p>
";
enfileirarEmail($destinatario, $assunto, $mensagem);
echo esc_html('E-mail enfileirado com sucesso!');
exit;
}
}
add_action('shutdown', 'processarFilaDeEmails');
add_action('init', 'pluginHandleRequest');