-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailpit.php
More file actions
189 lines (168 loc) · 6.19 KB
/
mailpit.php
File metadata and controls
189 lines (168 loc) · 6.19 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* MailPit SMTP
*
* @package MailPit
* @author Bishwajit Adhikary
* @copyright 2024 Bishwajit Adhikary
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: MailPit SMTP
* Plugin URI: https://wordpress.org/plugins/mailpit-smtp
* Description: A simple plugin to test SMTP settings and override the default WordPress mail system.
* Version: 1.0.1
* Requires at least: 5.3
* Requires PHP: 7.4
* Author: Bishwajit Adhikary
* Author URI: https://github.com/bishwajitcadhikary
* Text Domain: mailpit-smtp
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// Set default values for SMTP host and port
function mailpit_smtp_default_options() {
add_option('mailpit_smtp_host', '127.0.0.1');
add_option('mailpit_smtp_port', '1025');
}
function mailpit_smtp_settings_menu() {
// Define the URL of the custom icon
$icon_url = plugin_dir_url(__FILE__) . 'mailpit.svg';
add_menu_page(
'MailPit SMTP Settings',
'MailPit SMTP',
'manage_options',
'mailpit-smtp-settings',
'mailpit_smtp_settings_page',
$icon_url // Specify the custom icon URL here
);
}
// Display the SMTP settings page
function mailpit_smtp_settings_page() {
?>
<div class="wrap">
<h2>MailPit SMTP Settings</h2>
<form method="post" action="<?php echo esc_url(admin_url('options.php')); ?>">
<?php
settings_fields('mailpit-smtp-settings-group');
do_settings_sections('mailpit-smtp-settings');
submit_button();
?>
</form>
<h2>Test SMTP Email</h2>
<form method="post" action="">
<?php
wp_nonce_field('mailpit_smtp_test_email', 'mailpit_smtp_nonce');
?>
<table class="form-table" role="presentation">
<tbody>
<tr>
<th scope="row">
<label for="subject">Subject:</label>
</th>
<td>
<input type="text" id="subject" name="subject" value="Test Email" required>
</td>
</tr>
<tr>
<th scope="row">
<label for="test_email">Test Email Address:</label>
</th>
<td>
<input type="email" id="test_email" name="test_email" value="<?php echo esc_attr(get_option('admin_email')); ?>" required>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="submit" name="send_test_email" class="button-primary" value="Send Test Email">
</p>
</form>
<?php
if (isset($_POST['send_test_email'])) {
// Verify nonce
if (!isset($_POST['mailpit_smtp_nonce']) || !wp_verify_nonce($_POST['mailpit_smtp_nonce'], 'mailpit_smtp_test_email')) {
wp_die('Security check');
}
$test_email = sanitize_email($_POST['test_email']);
$subject = sanitize_text_field($_POST['subject']);
$message = 'This is a test email sent from the MailPit SMTP Plugin for WordPress.';
$result = wp_mail($test_email, $subject, $message);
if ($result) { ?>
<div class="updated"><p>Test email sent successfully to <?php echo esc_attr($test_email) ?></p></div>
<?php } else { ?>
<div class="error"><p>Error sending test email to <?php echo esc_attr($test_email) ?></p></div>
<?php }
}
?>
</div>
<?php
}
// Initialize and register SMTP settings
function mailpit_smtp_settings_init() {
register_setting(
'mailpit-smtp-settings-group',
'mailpit_smtp_host'
);
register_setting(
'mailpit-smtp-settings-group',
'mailpit_smtp_port'
);
add_settings_section(
'mailpit-smtp-settings-section',
'SMTP Configuration',
'mailpit_smtp_settings_section_callback',
'mailpit-smtp-settings'
);
add_settings_field(
'mailpit-smtp-host',
'SMTP Host',
'mailpit_smtp_host_callback',
'mailpit-smtp-settings',
'mailpit-smtp-settings-section'
);
add_settings_field(
'mailpit-smtp-port',
'SMTP Port',
'mailpit_smtp_port_callback',
'mailpit-smtp-settings',
'mailpit-smtp-settings-section'
);
}
// Callback to display section description
function mailpit_smtp_settings_section_callback() {
echo 'Configure your SMTP settings below:';
}
// Callback to display SMTP Host field
function mailpit_smtp_host_callback() {
$host = esc_attr(get_option('mailpit_smtp_host'));
echo "<input type='text' name='mailpit_smtp_host' value='" . esc_attr($host) ."' />";
}
// Callback to display SMTP Port field
function mailpit_smtp_port_callback() {
$port = esc_attr(get_option('mailpit_smtp_port'));
echo "<input type='text' name='mailpit_smtp_port' value='". esc_attr($port) ."' />";
}
// Hook to add menu, initialize settings, and set default options
add_action('admin_menu', 'mailpit_smtp_settings_menu');
add_action('admin_init', 'mailpit_smtp_settings_init');
add_action('admin_init', 'mailpit_smtp_default_options');
// Override the WordPress mail system with SMTP settings
function mailpit_smtp_phpmailer_init($phpmailer) {
$smtp_host = get_option('mailpit_smtp_host');
$smtp_port = get_option('mailpit_smtp_port');
$phpmailer->isSMTP();
$phpmailer->Host = $smtp_host;
$phpmailer->Port = $smtp_port;
$phpmailer->SMTPAuth = false;
// You can add more SMTP configuration options here, such as authentication settings if needed.
}
add_action('phpmailer_init', 'mailpit_smtp_phpmailer_init');
// Define the uninstaller function
function mailpit_smtp_uninstall() {
// Remove options from the database
delete_option('mailpit_smtp_host');
delete_option('mailpit_smtp_port');
}
// Hook the uninstaller function to the 'uninstall' action
register_uninstall_hook(__FILE__, 'mailpit_smtp_uninstall');