Skip to content

Commit d570637

Browse files
committed
Version 1.0.11
0 parents  commit d570637

File tree

9 files changed

+1517
-0
lines changed

9 files changed

+1517
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.svn
2+
.DS_Store
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
<?php
2+
namespace EarthAsylumConsulting\Extensions;
3+
4+
if (! class_exists(__NAMESPACE__.'\Simple_SMTP_extension', false) )
5+
{
6+
/**
7+
* Extension: simple_smtp - Configure phpmailer using SMTP server
8+
*
9+
* @category WordPress Plugin
10+
* @package {eac}Doojigger\Extensions
11+
* @author Kevin Burkholder <KBurkholder@EarthAsylum.com>
12+
* @copyright Copyright (c) 2023 EarthAsylum Consulting <www.EarthAsylum.com>
13+
* @version 1.x
14+
* @link https://eacDoojigger.earthasylum.com/
15+
* @see https://eacDoojigger.earthasylum.com/phpdoc/
16+
*/
17+
18+
class Simple_SMTP_extension extends \EarthAsylumConsulting\abstract_extension
19+
{
20+
/**
21+
* @var string extension version
22+
*/
23+
const VERSION = '23.0608.1';
24+
25+
26+
/**
27+
* constructor method
28+
*
29+
* @param object $plugin main plugin object
30+
* @return void
31+
*/
32+
public function __construct($plugin)
33+
{
34+
parent::__construct($plugin, self::DEFAULT_DISABLED | self::ALLOW_ALL);
35+
36+
if ($this->is_admin())
37+
{
38+
$this->registerExtension( [$this->className,'Simple SMTP'] );
39+
// Register plugin options when needed
40+
$this->add_action( "options_settings_page", array($this, 'admin_options_settings') );
41+
// Add contextual help
42+
$this->add_action( 'options_settings_help', array($this, 'admin_options_help') );
43+
}
44+
}
45+
46+
47+
/**
48+
* register options on options_settings_page
49+
*
50+
* @access public
51+
* @return void
52+
*/
53+
public function admin_options_settings()
54+
{
55+
require 'includes/simple_smtp.options.php';
56+
}
57+
58+
59+
/**
60+
* Add help tab on admin page
61+
*
62+
* @return void
63+
*/
64+
public function admin_options_help()
65+
{
66+
if (!$this->plugin->isSettingsPage('Simple SMTP')) return;
67+
68+
require 'includes/simple_smtp.help.php';
69+
}
70+
71+
72+
/**
73+
* initialize method - called from main plugin
74+
*
75+
* @return void
76+
*/
77+
public function initialize()
78+
{
79+
if ( ! parent::initialize() ) return; // disabled
80+
}
81+
82+
83+
/**
84+
* Add filters and actions - called from main plugin
85+
*
86+
* @return void
87+
*/
88+
public function addActionsAndFilters()
89+
{
90+
parent::addActionsAndFilters();
91+
92+
\add_action( 'phpmailer_init', array( $this, 'phpmailer_init' ) );
93+
\add_filter( 'wp_mail', array( $this, 'wp_mail_headers' ), 999);
94+
95+
if ($this->is_option('smtp_override'))
96+
{
97+
if ($from = $this->get_option('smtp_fromemail'))
98+
{
99+
\add_filter( 'wp_mail_from', function($email) use ($from)
100+
{
101+
return $from;
102+
}, 999);
103+
}
104+
if ($from = $this->get_option('smtp_fromname'))
105+
{
106+
\add_filter( 'wp_mail_from_name', function($name) use ($from)
107+
{
108+
return $from;
109+
}, 999);
110+
}
111+
}
112+
113+
\add_filter( 'simpleSMTP_from_name', function($from=null)
114+
{
115+
return $this->get_option('smtp_fromname') ?: $from;
116+
}
117+
);
118+
\add_filter( 'simpleSMTP_from_email', function($from=null)
119+
{
120+
return $this->get_option('smtp_fromemail') ?: $from;
121+
}
122+
);
123+
124+
}
125+
126+
127+
/**
128+
* set phpmailer options for SMTP server use (called from wp_mail)
129+
*
130+
* @param object $phpmailer - phpmailer object
131+
* @return void
132+
*/
133+
public function phpmailer_init(object $phpmailer)
134+
{
135+
if ($debugLevel = $this->get_option('smtp_debug'))
136+
{
137+
$phpmailer->SMTPDebug = $debugLevel;
138+
\add_action( 'wp_mail_succeeded', array( $this, 'wp_mail_complete' ));
139+
\add_action( 'wp_mail_failed', array( $this, 'wp_mail_complete' ));
140+
ob_start();
141+
}
142+
143+
if ($this->is_option('smtp_server'))
144+
{
145+
$phpmailer->isSMTP();
146+
$phpmailer->Host = $this->get_option('smtp_server');
147+
$phpmailer->Port = $this->get_option('smtp_port');
148+
}
149+
150+
if ( ($username = $this->get_option_decrypt('smtp_username')) && ($password = $this->get_option_decrypt('smtp_password')) )
151+
{
152+
$phpmailer->SMTPAuth = true;
153+
$phpmailer->Username = $username;
154+
$phpmailer->Password = $password;
155+
}
156+
else
157+
{
158+
$phpmailer->SMTPAuth = false;
159+
}
160+
161+
$encryption = $this->get_option('smtp_encryption');
162+
if ($encryption && $encryption != 'none')
163+
{
164+
$phpmailer->SMTPSecure = $encryption;
165+
}
166+
}
167+
168+
169+
/**
170+
* debugging - after when wp_mail sends email
171+
*
172+
* @param array|wp_error $mail_data An array containing the mail recipient, subject, message, headers, and attachments
173+
* @return void
174+
*/
175+
public function wp_mail_complete($mail_data)
176+
{
177+
$debug = ob_get_clean();
178+
$this->plugin->logDebug([$mail_data,$debug],__METHOD__);
179+
}
180+
181+
182+
/**
183+
* wp_mail custom headers
184+
*
185+
* @param string|array $args - wp_mail args
186+
* @param string $headers - smtp_headers
187+
* @return void
188+
*/
189+
public function wp_mail_headers($args)
190+
{
191+
if (! ($headers = $this->get_option('smtp_headers')) )
192+
{
193+
return $args;
194+
}
195+
196+
$headers = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
197+
198+
if (! empty($args['headers']) )
199+
{
200+
if (! is_array($args['headers']) ) {
201+
$args['headers'] = explode( "\n", str_replace( "\r\n", "\n", $args['headers'] ) );
202+
}
203+
$args['headers'] = array_merge($headers,$args['headers']);
204+
}
205+
else
206+
{
207+
$args['headers'] = $headers;
208+
}
209+
return $args;
210+
}
211+
212+
213+
/**
214+
* filter for options_form_post_ _smtp_testemail
215+
*
216+
* @param string $email - the value POSTed
217+
* @param string $fieldName - the name of the field/option
218+
* @param array $metaData - the option metadata
219+
* @param string $priorValue - the prior option value
220+
* @return string $email
221+
*/
222+
public function send_test_email($email, $fieldName=null, $metaData=null, $priorValue=null)
223+
{
224+
if (! ($testEmail = filter_var( $email, FILTER_VALIDATE_EMAIL )) )
225+
{
226+
return $email;
227+
}
228+
229+
$content = require 'includes/simple_smtp.email.php';
230+
231+
$result = wp_mail(
232+
$testEmail,
233+
\get_option('blogname')." SMTP Email Test",
234+
$content,
235+
[
236+
"from: ".$this->get_option('smtp_fromname').' <'.$this->get_option('smtp_fromemail').'>',
237+
"Content-type: text/html"
238+
]
239+
);
240+
}
241+
}
242+
}
243+
/**
244+
* return a new instance of this class
245+
*/
246+
if (isset($this)) return new Simple_SMTP_extension($this);
247+
?>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Extension: simple_smtp - Configure phpmailer using SMTP server
4+
*
5+
* @category WordPress Plugin
6+
* @package {eac}Doojigger\Extensions
7+
* @author Kevin Burkholder <KBurkholder@EarthAsylum.com>
8+
* @copyright Copyright (c) 2022 EarthAsylum Consulting <www.EarthAsylum.com>
9+
* @version 1.x
10+
*
11+
* included for send_test_email() method
12+
* @version 23.1031.1
13+
*/
14+
15+
defined( 'ABSPATH' ) or exit;
16+
17+
return "
18+
<!DOCTYPE html>
19+
<head>
20+
<style type='text/css' media='all'>
21+
* {font-family: Verdana, Helvetica, Tahoma, sans-serif; font-size: 14px;}
22+
#content {
23+
text-align: center;
24+
max-width: 80%;
25+
margin: 5em auto;
26+
position: relative;
27+
padding: 20px 5px;
28+
border-radius: 4px;
29+
border: 1px solid #ccc;
30+
border-left: 10px solid #358ccb;
31+
border-right: 10px solid #358ccb;
32+
}
33+
footer * {font-size: 10px; font-weight: 100;}
34+
</style>
35+
</head>
36+
<body>
37+
<div id='content'>
38+
<h3>Congratulations!</h3>
39+
<h4>Your test email from '".\get_option('blogname')."' was successfully delivered.</h4>
40+
<p>Your message was sent from <em>".$this->get_option('smtp_fromname')." &lt;".$this->get_option('smtp_fromemail')."&gt;</em>
41+
<br/>to <em>".$testEmail."</em></p>
42+
<p> Your mail configuration appears to be working correctly.</p>
43+
</div>
44+
<footer>
45+
<p>".$this->pluginName." - ".$this->className."<br/>
46+
<a href='".$this->plugin->getPluginValue('AuthorURI')."'/>".$this->plugin->getPluginValue('Author')."</a></p>
47+
<footer>
48+
</body>
49+
</html>
50+
";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Extension: simple_smtp - Configure phpmailer using SMTP server
4+
*
5+
* @category WordPress Plugin
6+
* @package {eac}Doojigger\Extensions
7+
* @author Kevin Burkholder <KBurkholder@EarthAsylum.com>
8+
* @copyright Copyright (c) 2022 EarthAsylum Consulting <www.EarthAsylum.com>
9+
* @version 1.x
10+
*
11+
* included for admin_options_help() method
12+
* @version 23.1031.1
13+
*/
14+
15+
defined( 'ABSPATH' ) or exit;
16+
17+
ob_start();
18+
?>
19+
<p>The {eac}SimpleSMTP extension adds SMTP server configuration for WordPress so that all
20+
email sent from your WordPress site will be sent through your SMTP (outgoing) mail server.</p>
21+
22+
<details><summary>Available Filters:</summary>
23+
<ul>
24+
<li><code>simpleSMTP_from_name</code> get the email from name used by SimpleSMTP
25+
<li><code>simpleSMTP_from_email</code> get the email from address used by SimpleSMTP
26+
</li>
27+
</details>
28+
<?php
29+
$content = ob_get_clean();
30+
31+
$this->addPluginHelpTab('Simple SMTP',$content,['Simple SMTP Extension','open']);
32+
33+
$this->addPluginSidebarLink(
34+
"<span class='dashicons dashicons-email'></span>{eac}SimpleSMTP",
35+
"https://eacdoojigger.earthasylum.com/eacsimplesmtp/",
36+
"{eac}SimpleSMTP Extension Plugin"
37+
);

0 commit comments

Comments
 (0)