Mailgun for WordPress - Version 0.1

Version Description

Initial Release

=

Download this release

Release Info

Developer sivel
Plugin Icon 128x128 Mailgun for WordPress
Version 0.1
Comparing to
See all releases

Version 0.1

includes/admin.php ADDED
@@ -0,0 +1,295 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class MailgunAdmin extends Mailgun {
4
+ /**
5
+ * Setup backend functionality in WordPress
6
+ *
7
+ * @return none
8
+ * @since 0.1
9
+ */
10
+ function __construct() {
11
+ Mailgun::__construct();
12
+
13
+ // Load localizations if available
14
+ load_plugin_textdomain( 'mailgun' , false , 'mailgun/languages' );
15
+
16
+ // Activation hook
17
+ register_activation_hook( $this->plugin_file, array( &$this, 'init' ) );
18
+
19
+ if ( ! defined( 'MAILGUN_USEAPI' ) || ! MAILGUN_USEAPI ) {
20
+ // Hook into admin_init and register settings and potentially register an admin_notice
21
+ add_action( 'admin_init', array( &$this, 'admin_init' ) );
22
+
23
+ // Activate the options page
24
+ add_action( 'admin_menu', array( &$this , 'admin_menu' ) );
25
+ }
26
+
27
+ // Register an AJAX action for testing mail sending capabilities
28
+ add_action( 'wp_ajax_mailgun-test', array( &$this, 'ajax_send_test' ) );
29
+ }
30
+
31
+ /**
32
+ * Initialize the default options during plugin activation
33
+ *
34
+ * @return none
35
+ * @since 0.1
36
+ */
37
+ function init() {
38
+ $defaults = array(
39
+ 'useAPI' => '1',
40
+ 'apiKey' => '',
41
+ 'domain' => '',
42
+ 'username' => '',
43
+ 'password' => '',
44
+ 'secure' => '1',
45
+ );
46
+ if ( ! $this->options ) {
47
+ $this->options = $defaults;
48
+ add_option( 'mailgun', $this->options );
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Add the options page
54
+ *
55
+ * @return none
56
+ * @since 0.1
57
+ */
58
+ function admin_menu() {
59
+ if ( current_user_can( 'manage_options' ) ) {
60
+ $this->hook_suffix = add_options_page( __( 'Mailgun', 'mailgun' ), __( 'Mailgun', 'mailgun' ), 'manage_options', 'mailgun', array( &$this , 'options_page' ) );
61
+ add_action( "admin_print_scripts-{$this->hook_suffix}" , array( &$this , 'admin_js' ) );
62
+ add_filter( "plugin_action_links_{$this->plugin_basename}" , array( &$this , 'filter_plugin_actions' ) );
63
+ add_action( "admin_footer-{$this->hook_suffix}" , array( &$this , 'admin_footer_js' ) );
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Enqueue javascript required for the admin settings page
69
+ *
70
+ * @return none
71
+ * @since 0.1
72
+ */
73
+ function admin_js() {
74
+ wp_enqueue_script( 'jquery' );
75
+ }
76
+
77
+ /**
78
+ * Output JS to footer for enhanced admin page functionality
79
+ *
80
+ * @since 0.1
81
+ */
82
+ function admin_footer_js() {
83
+ ?>
84
+ <script type="text/javascript">
85
+ /* <![CDATA[ */
86
+ var mailgunApiOrNot = function() {
87
+ if (jQuery("#mailgun-api").val() == 1) {
88
+ jQuery(".mailgun-smtp").hide();
89
+ jQuery(".mailgun-api").show();
90
+ } else {
91
+ jQuery(".mailgun-api").hide();
92
+ jQuery(".mailgun-smtp").show();
93
+ }
94
+
95
+ }
96
+ var formModified = false;
97
+ jQuery().ready(function() {
98
+ mailgunApiOrNot();
99
+ jQuery('#mailgun-api').change(function() {
100
+ mailgunApiOrNot();
101
+ });
102
+ jQuery('#mailgun-test').click(function(e) {
103
+ e.preventDefault();
104
+ if ( formModified ) {
105
+ var doTest = confirm('<?php _e( 'The Mailgun plugin configuration has changed since you last saved. Do you wish to test anyway?\n\nClick "Cancel" and then "Save Changes" if you wish to save your changes.', 'mailgun'); ?>');
106
+ if ( ! doTest ) {
107
+ return false;
108
+ }
109
+ }
110
+ jQuery(this).val('<?php _e( 'Testing...', 'mailgun' ); ?>');
111
+ jQuery("#mailgun-test-result").text('');
112
+ jQuery.get(
113
+ ajaxurl,
114
+ {
115
+ action: 'mailgun-test',
116
+ _wpnonce: '<?php echo wp_create_nonce(); ?>'
117
+ }
118
+ )
119
+ .complete(function() {
120
+ jQuery("#mailgun-test").val('<?php _e( 'Test Configuration', 'mailgun' ); ?>');
121
+ })
122
+ .success(function(data) {
123
+ alert('Mailgun ' + data.method + ' Test ' + data.message);
124
+ })
125
+ .error(function() {
126
+ alert('Mailgun Test <?php _e( 'Failure', 'mailgun' ); ?>');
127
+ });
128
+ });
129
+ jQuery("#mailgun-form").change(function() {
130
+ formModified = true;
131
+ });
132
+ });
133
+ /* ]]> */
134
+ </script>
135
+ <?php
136
+ }
137
+
138
+ /**
139
+ * Output the options page
140
+ *
141
+ * @return none
142
+ * @since 0.1
143
+ */
144
+ function options_page() {
145
+ if ( ! @include( 'options-page.php' ) ) {
146
+ printf( __( '<div id="message" class="updated fade"><p>The options page for the <strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</strong> is missing. Please reinstall the plugin.</p></div>', 'mailgun'), dirname( __FILE__ ) . '/options-page.php' );
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Wrapper function hooked into admin_init to register settings
152
+ * and potentially register an admin notice if the plugin hasn't
153
+ * been configured yet
154
+ *
155
+ * @return none
156
+ * @since 0.1
157
+ */
158
+ function admin_init() {
159
+ $this->register_settings();
160
+ $apiKey = $this->get_option( 'apiKey' );
161
+ $useAPI = $this->get_option( 'useAPI' );
162
+ $password = $this->get_option( 'password' );
163
+ if ( ( empty( $apiKey ) && $useAPI == '1' ) || ( empty( $password ) && $useAPI == '0' ) ) {
164
+ add_action( 'admin_notices', array( &$this, 'admin_notices' ) );
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Whitelist the mailgun options
170
+ *
171
+ * @since 0.1
172
+ * @return none
173
+ */
174
+ function register_settings() {
175
+ register_setting( 'mailgun', 'mailgun', array( &$this, 'validation' ) );
176
+ }
177
+
178
+ /**
179
+ * Data validation callback function for options
180
+ *
181
+ * @param array $options An array of options posted from the options page
182
+ * @return array
183
+ * @since 0.1
184
+ */
185
+ function validation( $options ) {
186
+ $apiKey = trim( $options['apiKey'] );
187
+ $username = trim( $options['username'] );
188
+ if ( ! empty( $apiKey ) ) {
189
+ $pos = strpos( $apiKey, 'key-' );
190
+ if ( $pos === false || $pos > 4 )
191
+ $apiKey = "key-{$apiKey}";
192
+
193
+ $pos = strpos( $apiKey, 'api:' );
194
+ if ( $pos !== false && $pos == 0 )
195
+ $apiKey = substr( $apiKey, 4 );
196
+ $options['apiKey'] = $apiKey;
197
+ }
198
+
199
+ if ( ! empty( $username ) ) {
200
+ $username = preg_replace( '/@.+$/', '', $username );
201
+ $options['username'] = $username;
202
+ }
203
+
204
+ foreach ( $options as $key => $value )
205
+ $options[$key] = trim( $value );
206
+
207
+ $this->options = $options;
208
+ return $options;
209
+ }
210
+
211
+ /**
212
+ * Function to output an admin notice when the plugin has not
213
+ * been configured yet
214
+ *
215
+ * @return none
216
+ * @since 0.1
217
+ */
218
+ function admin_notices() {
219
+ $screen = get_current_screen();
220
+ if ( $screen->id == $this->hook_suffix )
221
+ return;
222
+ ?>
223
+ <div id='mailgun-warning' class='updated fade'><p><strong><?php _e( 'Mailgun is almost ready. ', 'mailgun' ); ?></strong><?php printf( __( 'You must <a href="%1$s">configure Mailgun</a> for it to work.', 'mailgun' ), menu_page_url( 'mailgun' , false ) ); ?></p></div>
224
+ <?php
225
+ }
226
+
227
+ /**
228
+ * Add a settings link to the plugin actions
229
+ *
230
+ * @param array $links Array of the plugin action links
231
+ * @return array
232
+ * @since 0.1
233
+ */
234
+ function filter_plugin_actions( $links ) {
235
+ $settings_link = '<a href="' . menu_page_url( 'mailgun', false ) . '">' . __( 'Settings', 'mailgun' ) . '</a>';
236
+ array_unshift( $links, $settings_link );
237
+ return $links;
238
+ }
239
+
240
+ /**
241
+ * AJAX callback function to test mail sending functionality
242
+ *
243
+ * @return string
244
+ * @since 0.1
245
+ */
246
+ function ajax_send_test() {
247
+ nocache_headers();
248
+ header( 'Content-Type: application/json' );
249
+
250
+ if ( ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( $_GET[ '_wpnonce' ] ) ) {
251
+ die(
252
+ json_encode(
253
+ array(
254
+ 'message' => __( 'Unauthorized', 'mailgun' ),
255
+ 'method' => null
256
+ )
257
+ )
258
+ );
259
+ }
260
+
261
+ $useAPI = ( defined( 'MAILGUN_USEAPI' ) && MAILGUN_USEAPI ) ? MAILGUN_USEAPI : $this->get_option( 'useAPI' );
262
+ $secure = ( defined( 'MAILGUN_SECURE' ) && MAILGUN_SECURE ) ? MAILGUN_SECURE : $this->get_option( 'secure' );
263
+ if ( (bool) $useAPI )
264
+ $method = __( 'HTTP API', 'mailgun' );
265
+ else
266
+ $method = ( (bool) $secure ) ? __( 'Secure SMTP', 'mailgun' ) : __( 'SMTP', 'mailgun' );
267
+
268
+ $admin_email = get_option( 'admin_email' );
269
+ $result = wp_mail(
270
+ $admin_email,
271
+ __( 'Mailgun WordPress Plugin Test', 'mailgun' ),
272
+ sprintf( __( "This is a test email generated by the Mailgun WordPress plugin.\n\nIf you have received this message, the requested test has succeeded.\n\nThe method used to send this email was: %s.", 'mailgun' ), $method )
273
+ );
274
+
275
+ if ( $result ) {
276
+ die(
277
+ json_encode(
278
+ array(
279
+ 'message' => __( 'Success', 'mailgun' ),
280
+ 'method' => $method
281
+ )
282
+ )
283
+ );
284
+ } else {
285
+ die(
286
+ json_encode(
287
+ array(
288
+ 'message' => __( 'Failure', 'mailgun' ),
289
+ 'method' => $method
290
+ )
291
+ )
292
+ );
293
+ }
294
+ }
295
+ }
includes/options-page.php ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div class="wrap">
2
+ <div id="icon-options-general" class="icon32"><br /></div>
3
+ <span class="alignright"><a target="_blank" href="http://www.mailgun.com/"><img src="https://mailgun.net/static/img/logo.png" alt="Mailgun" /></a></span>
4
+ <h2><?php _e( 'Mailgun' , 'mailgun' ); ?></h2>
5
+ <p>A <a target="_blank" href="http://www.mailgun.com/">Mailgun</a> account is required to use this plugin and the Mailgun service.</p>
6
+ <p>If you need to register for an account, you can do so at <a target="_blank" href="http://www.mailgun.com/">http://www.mailgun.com/</a>.</p>
7
+ <form id="mailgun-form" action="options.php" method="post">
8
+ <?php settings_fields( 'mailgun' ); ?>
9
+ <h3><?php _e( 'Configuration' , 'mailgun' ); ?></h3>
10
+ <table class="form-table">
11
+ <tr valign="top">
12
+ <th scope="row">
13
+ <?php _e( 'Use HTTP API' , 'mailgun' ); ?>
14
+ </th>
15
+ <td>
16
+ <select id="mailgun-api" name="mailgun[useAPI]">
17
+ <option value="1"<?php selected( '1' , $this->get_option( 'useAPI' ) ); ?>><?php _e( 'Yes' , 'mailgun' ); ?></option>
18
+ <option value="0"<?php selected( '0' , $this->get_option( 'useAPI' ) ); ?>><?php _e( 'No' , 'mailgun' ); ?></option>
19
+ </select>
20
+ <p class="description"><?php _e( 'Set this to "No" if your server cannot make outbound HTTP connections or if emails are not being delivered. "No" will cause this plugin to use SMTP. Default "Yes".', 'mailgun' ); ?></p>
21
+ </td>
22
+ </tr>
23
+ <tr valign="top">
24
+ <th scope="row">
25
+ <?php _e( 'Mailgun Domain Name' , 'mailgun' ); ?>
26
+ </th>
27
+ <td>
28
+ <input type="text" class="regular-text" name="mailgun[domain]" value="<?php esc_attr_e( $this->get_option( 'domain' ) ); ?>" placeholder="samples.mailgun.org" />
29
+ <p class="description"><?php _e( 'Your Mailgun Domain Name.', 'mailgun' ); ?></p>
30
+ </td>
31
+ </tr>
32
+ <tr valign="top" class="mailgun-api">
33
+ <th scope="row">
34
+ <?php _e( 'API Key' , 'mailgun' ); ?>
35
+ </th>
36
+ <td>
37
+ <input type="text" class="regular-text" name="mailgun[apiKey]" value="<?php esc_attr_e( $this->get_option( 'apiKey' ) ); ?>" placeholder="key-3ax6xnjp29jd6fds4gc373sgvjxteol0" />
38
+ <p class="description"><?php _e( 'Your Mailgun API key, that starts with and includes "key-". Only valid for use with the API.', 'mailgun' ); ?></p>
39
+ </td>
40
+ </tr>
41
+ <tr valign="top" class="mailgun-smtp">
42
+ <th scope="row">
43
+ <?php _e( 'Username' , 'mailgun' ); ?>
44
+ </th>
45
+ <td>
46
+ <input type="text" class="regular-text" name="mailgun[username]" value="<?php esc_attr_e( $this->get_option( 'username' ) ); ?>" placeholder="postmaster" />
47
+ <p class="description"><?php _e( 'Your Mailgun SMTP username. Only valid for use with SMTP.', 'mailgun' ); ?></p>
48
+ </td>
49
+ </tr>
50
+ <tr valign="top" class="mailgun-smtp">
51
+ <th scope="row">
52
+ <?php _e( 'Password' , 'mailgun' ); ?>
53
+ </th>
54
+ <td>
55
+ <input type="text" class="regular-text" name="mailgun[password]" value="<?php esc_attr_e( $this->get_option( 'password' ) ); ?>" placeholder="my-password" />
56
+ <p class="description"><?php _e( 'Your Mailgun SMTP password that goes with the above username. Only valid for use with SMTP.', 'mailgun' ); ?></p>
57
+ </td>
58
+ </tr>
59
+ <tr valign="top" class="mailgun-smtp">
60
+ <th scope="row">
61
+ <?php _e( 'Use Secure SMTP' , 'mailgun' ); ?>
62
+ </th>
63
+ <td>
64
+ <select name="mailgun[secure]">
65
+ <option value="1"<?php selected( '1' , $this->get_option( 'secure' ) ); ?>><?php _e( 'Yes' , 'mailgun' ); ?></option>
66
+ <option value="0"<?php selected( '0' , $this->get_option( 'secure' ) ); ?>><?php _e( 'No' , 'mailgun' ); ?></option>
67
+ </select>
68
+ <p class="description"><?php _e( 'Set this to "No" if your server cannot establish SSL SMTP connections or if emails are not being delivered. If you set this to "No" your password will be sent in plain text. Only valid for use with SMTP. Default "Yes".', 'mailgun' ); ?></p>
69
+ </td>
70
+ </tr>
71
+ </table>
72
+ <p><?php _e( 'Before attempting to test the configuration, please click "Save Changes".', 'mailgun' ); ?></p>
73
+ <p class="submit">
74
+ <input type="submit" class="button-primary" value="<?php _e( 'Save Changes' , 'mailgun' ); ?>" />
75
+ <input type="button" id="mailgun-test" class="button-secondary" value="<?php _e( 'Test Configuration', 'mailgun' ); ?>" />
76
+ </p>
77
+ </form>
78
+ </div>
includes/wp-mail.php ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * wp_mail function to be loaded in to override the core wp_mail function
4
+ * from wp-includes/pluggable.php
5
+ *
6
+ * Based off of the core wp_mail function, but with modifications required to
7
+ * send email using the Mailgun HTTP API
8
+ *
9
+ * @param string|array $to Array or comma-separated list of email addresses to send message.
10
+ * @param string $subject Email subject
11
+ * @param string $message Message contents
12
+ * @param string|array $headers Optional. Additional headers.
13
+ * @param string|array $attachments Optional. Files to attach.
14
+ * @return bool Whether the email contents were sent successfully.
15
+ * @since 0.1
16
+ */
17
+ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
18
+ // Compact the input, apply the filters, and extract them back out
19
+ extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
20
+
21
+ $mailgun = get_option( 'mailgun' );
22
+ $apiKey = ( defined( 'MAILGUN_APIKEY' ) && MAILGUN_APIKEY ) ? MAILGUN_APIKEY : $mailgun['apiKey'];
23
+ $domain = ( defined( 'MAILGUN_DOMAIN' ) && MAILGUN_DOMAIN ) ? MAILGUN_DOMAIN : $mailgun['domain'];
24
+
25
+ if ( empty( $apiKey ) || empty( $domain ) )
26
+ return false;
27
+
28
+ if ( !is_array($attachments) )
29
+ $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
30
+
31
+ // Headers
32
+ if ( empty( $headers ) ) {
33
+ $headers = array();
34
+ } else {
35
+ if ( !is_array( $headers ) ) {
36
+ // Explode the headers out, so this function can take both
37
+ // string headers and an array of headers.
38
+ $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
39
+ } else {
40
+ $tempheaders = $headers;
41
+ }
42
+ $headers = array();
43
+ $cc = array();
44
+ $bcc = array();
45
+
46
+ // If it's actually got contents
47
+ if ( !empty( $tempheaders ) ) {
48
+ // Iterate through the raw headers
49
+ foreach ( (array) $tempheaders as $header ) {
50
+ if ( strpos($header, ':') === false ) {
51
+ if ( false !== stripos( $header, 'boundary=' ) ) {
52
+ $parts = preg_split('/boundary=/i', trim( $header ) );
53
+ $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
54
+ }
55
+ continue;
56
+ }
57
+ // Explode them out
58
+ list( $name, $content ) = explode( ':', trim( $header ), 2 );
59
+
60
+ // Cleanup crew
61
+ $name = trim( $name );
62
+ $content = trim( $content );
63
+
64
+ switch ( strtolower( $name ) ) {
65
+ // Mainly for legacy -- process a From: header if it's there
66
+ case 'from':
67
+ if ( strpos($content, '<' ) !== false ) {
68
+ // So... making my life hard again?
69
+ $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
70
+ $from_name = str_replace( '"', '', $from_name );
71
+ $from_name = trim( $from_name );
72
+
73
+ $from_email = substr( $content, strpos( $content, '<' ) + 1 );
74
+ $from_email = str_replace( '>', '', $from_email );
75
+ $from_email = trim( $from_email );
76
+ } else {
77
+ $from_email = trim( $content );
78
+ }
79
+ break;
80
+ case 'content-type':
81
+ if ( strpos( $content, ';' ) !== false ) {
82
+ list( $type, $charset ) = explode( ';', $content );
83
+ $content_type = trim( $type );
84
+ if ( false !== stripos( $charset, 'charset=' ) ) {
85
+ $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
86
+ } elseif ( false !== stripos( $charset, 'boundary=' ) ) {
87
+ $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
88
+ $charset = '';
89
+ }
90
+ } else {
91
+ $content_type = trim( $content );
92
+ }
93
+ break;
94
+ case 'cc':
95
+ $cc = array_merge( (array) $cc, explode( ',', $content ) );
96
+ break;
97
+ case 'bcc':
98
+ $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
99
+ break;
100
+ default:
101
+ // Add it to our grand headers array
102
+ $headers[trim( $name )] = trim( $content );
103
+ break;
104
+ }
105
+ }
106
+ }
107
+ }
108
+
109
+ // From email and name
110
+ // If we don't have a name from the input headers
111
+ if ( !isset( $from_name ) )
112
+ $from_name = 'WordPress';
113
+
114
+ /* If we don't have an email from the input headers default to wordpress@$sitename
115
+ * Some hosts will block outgoing mail from this address if it doesn't exist but
116
+ * there's no easy alternative. Defaulting to admin_email might appear to be another
117
+ * option but some hosts may refuse to relay mail from an unknown domain. See
118
+ * http://trac.wordpress.org/ticket/5007.
119
+ */
120
+
121
+ if ( !isset( $from_email ) ) {
122
+ // Get the site domain and get rid of www.
123
+ $sitename = strtolower( $_SERVER['SERVER_NAME'] );
124
+ if ( substr( $sitename, 0, 4 ) == 'www.' ) {
125
+ $sitename = substr( $sitename, 4 );
126
+ }
127
+
128
+ $from_email = 'wordpress@' . $sitename;
129
+ }
130
+
131
+ // Plugin authors can override the potentially troublesome default
132
+ $from_email = apply_filters( 'wp_mail_from' , $from_email );
133
+ $from_name = apply_filters( 'wp_mail_from_name', $from_name );
134
+
135
+ $body = array(
136
+ 'from' => "{$from_name} <{$from_email}>",
137
+ 'to' => $to,
138
+ 'subject' => $subject,
139
+ 'text' => $message,
140
+ 'o:tag' => array( 'WordPress', $sitename )
141
+ );
142
+
143
+ if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'mailgun-test' )
144
+ $body['o:tag'][] = 'test';
145
+
146
+ if ( ! empty( $cc ) && is_array( $cc ) )
147
+ $body['cc'] = implode( ', ', $cc );
148
+
149
+ if ( ! empty( $bcc ) && is_array( $bcc ) )
150
+ $body['bcc'] = implode( ', ', $bcc );
151
+
152
+ // Set Content-Type and charset
153
+ // If we don't have a content-type from the input headers
154
+ if ( !isset( $content_type ) )
155
+ $content_type = 'text/plain';
156
+
157
+ $content_type = apply_filters( 'wp_mail_content_type', $content_type );
158
+ if ( $content_type == 'text/html' ) {
159
+ $body['html'] = $message;
160
+ $body['text'] = strip_tags( $message );
161
+ }
162
+
163
+ // If we don't have a charset from the input headers
164
+ if ( !isset( $charset ) )
165
+ $charset = get_bloginfo( 'charset' );
166
+
167
+ // Set the content-type and charset
168
+ $charset = apply_filters( 'wp_mail_charset', $charset );
169
+ if ( isset( $headers['Content-Type'] ) ) {
170
+ if ( ! strstr( $headers['Content-Type'], 'charset' ) )
171
+ $headers['Content-Type'] = rtrim( $headers['Content-Type'], '; ' ) . "; charset={$charset}";
172
+ }
173
+
174
+ // Set custom headers
175
+ if ( !empty( $headers ) ) {
176
+ foreach( (array) $headers as $name => $content ) {
177
+ $body["h:{$name}"] = $content;
178
+ }
179
+
180
+ // TODO: Can we handle this?
181
+ //if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
182
+ // $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
183
+ }
184
+
185
+ if ( !empty( $attachments ) ) {
186
+ foreach ( $attachments as $attachment ) {
187
+ $i = 1;
188
+ foreach ( $attachments as $attachment ) {
189
+ if ( file_exists( $attachment ) ) {
190
+ $body["attachment[{$i}]"] = "@{$attachment}";
191
+ $i++;
192
+ }
193
+ }
194
+ }
195
+ }
196
+
197
+ $data = array(
198
+ 'body' => $body,
199
+ 'headers' => array(
200
+ 'Authorization' => 'Basic ' . base64_encode( "api:{$apiKey}" )
201
+ )
202
+ );
203
+
204
+ $url = "https://api.mailgun.net/v2/{$domain}/messages";
205
+
206
+ // TODO: Mailgun only supports 1000 recipients per request, since we are
207
+ // overriding this function, let's add looping here to handle that
208
+ $response = wp_remote_post( $url, $data );
209
+ if ( is_wp_error( $response ) )
210
+ return false;
211
+
212
+ $response_code = wp_remote_retrieve_response_code( $response );
213
+ if ( (int) $response_code != 200 )
214
+ return false;
215
+
216
+ // Not sure there is any additional checking that needs to be done here, but why not?
217
+ $response_body = json_decode( wp_remote_retrieve_body( $response ) );
218
+ if ( ! isset( $response_body->message ) || $response_body->message != 'Queued. Thank you.' )
219
+ return false;
220
+
221
+ return true;
222
+ }
languages/mailgun-template.po ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ msgid ""
2
+ msgstr ""
3
+ "Project-Id-Version: Mailgun\n"
4
+ "POT-Creation-Date: 2012-11-20 13:54-0600\n"
5
+ "PO-Revision-Date: 2012-11-20 13:54-0600\n"
6
+ "Last-Translator: Matt Martz <matt@sivel.net>\n"
7
+ "Language-Team: Mailgun <matt@sivel.net>\n"
8
+ "Language: en_US\n"
9
+ "MIME-Version: 1.0\n"
10
+ "Content-Type: text/plain; charset=UTF-8\n"
11
+ "Content-Transfer-Encoding: 8bit\n"
12
+
13
+ #: mailgun.php:89
14
+ #, php-format
15
+ msgid ""
16
+ "Mailgun has been automatically deactivated because the file <strong>%s</"
17
+ "strong> is missing. Please reinstall the plugin and reactivate."
18
+ msgstr ""
19
+
20
+ #: includes/admin.php:60 includes/options-page.php:4
21
+ msgid "Mailgun"
22
+ msgstr ""
23
+
24
+ #: includes/admin.php:105
25
+ msgid ""
26
+ "The Mailgun plugin configuration has changed since you last saved. Do you "
27
+ "wish to test anyway?\\n\\nClick \"Cancel\" and then \"Save Changes\" if you "
28
+ "wish to save your changes."
29
+ msgstr ""
30
+
31
+ #: includes/admin.php:110
32
+ msgid "Testing..."
33
+ msgstr ""
34
+
35
+ #: includes/admin.php:120 includes/options-page.php:75
36
+ msgid "Test Configuration"
37
+ msgstr ""
38
+
39
+ #: includes/admin.php:126 includes/admin.php:288
40
+ msgid "Failure"
41
+ msgstr ""
42
+
43
+ #: includes/admin.php:146
44
+ #, php-format
45
+ msgid ""
46
+ "<div id=\"message\" class=\"updated fade\"><p>The options page for the "
47
+ "<strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</"
48
+ "strong> is missing. Please reinstall the plugin.</p></div>"
49
+ msgstr ""
50
+
51
+ #: includes/admin.php:223
52
+ msgid "Mailgun is almost ready. "
53
+ msgstr ""
54
+
55
+ #: includes/admin.php:223
56
+ #, php-format
57
+ msgid "You must <a href=\"%1$s\">configure Mailgun</a> for it to work."
58
+ msgstr ""
59
+
60
+ #: includes/admin.php:235
61
+ msgid "Settings"
62
+ msgstr ""
63
+
64
+ #: includes/admin.php:254
65
+ msgid "Unauthorized"
66
+ msgstr ""
67
+
68
+ #: includes/admin.php:264
69
+ msgid "HTTP API"
70
+ msgstr ""
71
+
72
+ #: includes/admin.php:266
73
+ msgid "Secure SMTP"
74
+ msgstr ""
75
+
76
+ #: includes/admin.php:266
77
+ msgid "SMTP"
78
+ msgstr ""
79
+
80
+ #: includes/admin.php:271
81
+ msgid "Mailgun WordPress Plugin Test"
82
+ msgstr ""
83
+
84
+ #: includes/admin.php:272
85
+ #, php-format
86
+ msgid ""
87
+ "This is a test email generated by the Mailgun WordPress plugin.\n"
88
+ "\n"
89
+ "If you have received this message, the requested test has succeeded.\n"
90
+ "\n"
91
+ "The method used to send this email was: %s."
92
+ msgstr ""
93
+
94
+ #: includes/admin.php:279
95
+ msgid "Success"
96
+ msgstr ""
97
+
98
+ #: includes/options-page.php:9
99
+ msgid "Configuration"
100
+ msgstr ""
101
+
102
+ #: includes/options-page.php:13
103
+ msgid "Use HTTP API"
104
+ msgstr ""
105
+
106
+ #: includes/options-page.php:17 includes/options-page.php:65
107
+ msgid "Yes"
108
+ msgstr ""
109
+
110
+ #: includes/options-page.php:18 includes/options-page.php:66
111
+ msgid "No"
112
+ msgstr ""
113
+
114
+ #: includes/options-page.php:20
115
+ msgid ""
116
+ "Set this to \"No\" if your server cannot make outbound HTTP connections or "
117
+ "if emails are not being delivered. \"No\" will cause this plugin to use "
118
+ "SMTP. Default \"Yes\"."
119
+ msgstr ""
120
+
121
+ #: includes/options-page.php:25
122
+ msgid "Mailgun Domain Name"
123
+ msgstr ""
124
+
125
+ #: includes/options-page.php:29
126
+ msgid "Your Mailgun Domain Name."
127
+ msgstr ""
128
+
129
+ #: includes/options-page.php:34
130
+ msgid "API Key"
131
+ msgstr ""
132
+
133
+ #: includes/options-page.php:38
134
+ msgid ""
135
+ "Your Mailgun API key, that starts with and includes \"key-\". Only valid for "
136
+ "use with the API."
137
+ msgstr ""
138
+
139
+ #: includes/options-page.php:43
140
+ msgid "Username"
141
+ msgstr ""
142
+
143
+ #: includes/options-page.php:47
144
+ msgid "Your Mailgun SMTP username. Only valid for use with SMTP."
145
+ msgstr ""
146
+
147
+ #: includes/options-page.php:52
148
+ msgid "Password"
149
+ msgstr ""
150
+
151
+ #: includes/options-page.php:56
152
+ msgid ""
153
+ "Your Mailgun SMTP password that goes with the above username. Only valid for "
154
+ "use with SMTP."
155
+ msgstr ""
156
+
157
+ #: includes/options-page.php:61
158
+ msgid "Use Secure SMTP"
159
+ msgstr ""
160
+
161
+ #: includes/options-page.php:68
162
+ msgid ""
163
+ "Set this to \"No\" if your server cannot establish SSL SMTP connections or "
164
+ "if emails are not being delivered. If you set this to \"No\" your password "
165
+ "will be sent in plain text. Only valid for use with SMTP. Default \"Yes\"."
166
+ msgstr ""
167
+
168
+ #: includes/options-page.php:72
169
+ msgid ""
170
+ "Before attempting to test the configuration, please click \"Save Changes\"."
171
+ msgstr ""
172
+
173
+ #: includes/options-page.php:74
174
+ msgid "Save Changes"
175
+ msgstr ""
mailgun.php ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Plugin Name: Mailgun
4
+ * Plugin URI: http://wordpress.org/extend/plugins/mailgun/
5
+ * Description: Mailgun integration for WordPress
6
+ * Version: 0.1
7
+ * Author: Matt Martz
8
+ * Author URI: http://www.mailgun.com/
9
+ * License: GPLv2 or later
10
+ * Text Domain: mailgun
11
+ * Domain Path: /languages/
12
+ */
13
+
14
+ class Mailgun {
15
+
16
+ /**
17
+ * Setup shared functionality for Admin and Front End
18
+ *
19
+ * @return none
20
+ * @since 0.1
21
+ */
22
+ function __construct() {
23
+ $this->options = get_option( 'mailgun' );
24
+ $this->plugin_file = __FILE__;
25
+ $this->plugin_basename = plugin_basename( $this->plugin_file );
26
+
27
+ // Either override the wp_mail function or configure PHPMailer to use the
28
+ // Mailgun SMTP servers
29
+ if ( $this->get_option( 'useAPI' ) || ( defined( 'MAILGUN_USEAPI' ) && MAILGUN_USEAPI ) ) {
30
+ if ( ! function_exists( 'wp_mail' ) ) {
31
+ if ( ! @include( dirname( __FILE__ ) . '/includes/wp-mail.php' ) )
32
+ Mailgun::deactivate_and_die( dirname( __FILE__ ) . '/includes/wp-mail.php' );
33
+ }
34
+ } else {
35
+ add_action( 'phpmailer_init', array( &$this, 'phpmailer_init' ) );
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Get specific option from the options table
41
+ *
42
+ * @param string $option Name of option to be used as array key for retrieving the specific value
43
+ * @return mixed
44
+ * @since 0.1
45
+ */
46
+ function get_option( $option, $options = null ) {
47
+ if ( is_null( $options ) )
48
+ $options = &$this->options;
49
+ if ( isset( $options[$option] ) )
50
+ return $options[$option];
51
+ else
52
+ return false;
53
+ }
54
+
55
+ /**
56
+ * Hook into phpmailer to override SMTP based configurations
57
+ * to use the Mailgun SMTP server
58
+ *
59
+ * @param object $phpmailer The PHPMailer object to modify by reference
60
+ * @return none
61
+ * @since 0.1
62
+ */
63
+ function phpmailer_init( &$phpmailer ) {
64
+ $username = ( defined( 'MAILGUN_USERNAME' ) && MAILGUN_USERNAME ) ? MAILGUN_USERNAME : $this->get_option( 'username' );
65
+ $domain = ( defined( 'MAILGUN_DOMAIN' ) && MAILGUN_DOMAIN ) ? MAILGUN_DOMAIN : $this->get_option( 'domain' );
66
+ $username = preg_replace( '/@.+$/', '', $username ) . "@{$domain}";
67
+ $secure = ( defined( 'MAILGUN_SECURE' ) && MAILGUN_SECURE ) ? MAILGUN_SECURE : $this->get_option('secure');
68
+ $password = ( defined( 'MAILGUN_PASSWORD' ) && MAILGUN_PASSWORD ) ? MAILGUN_PASSWORD : $this->get_option('password');
69
+
70
+ $phpmailer->Mailer = 'smtp';
71
+ $phpmailer->SMTPSecure = (bool) $secure ? 'ssl' : 'none';
72
+ $phpmailer->Host = 'smtp.mailgun.net';
73
+ $phpmailer->Port = (bool) $secure ? 465 : 587;
74
+ $phpmailer->SMTPAuth = true;
75
+ $phpmailer->Username = $username;
76
+ $phpmailer->Password = $password;
77
+ }
78
+
79
+ /**
80
+ * Deactivate this plugin and die
81
+ *
82
+ * Used to deactivate the plugin when files critical to it's operation can not be loaded
83
+ *
84
+ * @since 0.1
85
+ * @return none
86
+ */
87
+ function deactivate_and_die( $file ) {
88
+ load_plugin_textdomain( 'mailgun', false, 'mailgun/languages' );
89
+ $message = sprintf( __( "Mailgun has been automatically deactivated because the file <strong>%s</strong> is missing. Please reinstall the plugin and reactivate." ), $file );
90
+ if ( ! function_exists( 'deactivate_plugins' ) )
91
+ include( ABSPATH . 'wp-admin/includes/plugin.php' );
92
+ deactivate_plugins( __FILE__ );
93
+ wp_die( $message );
94
+ }
95
+
96
+ }
97
+
98
+ if ( is_admin() ) {
99
+ if ( @include( dirname( __FILE__ ) . '/includes/admin.php' ) ) {
100
+ $mailgunAdmin = new MailgunAdmin();
101
+ } else {
102
+ Mailgun::deactivate_and_die( dirname( __FILE__ ) . '/includes/admin.php' );
103
+ }
104
+ } else {
105
+ $mailgun = new Mailgun();
106
+ }
readme.txt ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Mailgun for WordPress ===
2
+ Contributors: sivel, Mailgun
3
+ Tags: mailgun, smtp, http, api, mail, email
4
+ Requires at least: 3.3
5
+ Tested up to: 3.5
6
+ Stable tag: 0.1
7
+ License: GPLv2 or later
8
+
9
+ Easily send email from your WordPress site through Mailgun using using the HTTP API or SMTP.
10
+
11
+ == Description ==
12
+
13
+ [Mailgun](http://www.mailgun.com/) is the email automation engine trusted by over 10,000 website and application developers for sending, receiving and tracking emails. By taking advantage of Mailgun's powerful email APIs, developers can spend more time building awesome websites and less time fighting with email servers. Mailgun supports all of the most popular languages including PHP, Ruby, Python, C# and Java.
14
+
15
+ One particularly useful feature of this plugin is that it provides you with a way to send email when the server you are on does not support SMTP or where outbound SMTP is restricted since the plug-in uses the Mailgun HTTP API for sending email by default. All you need to use the plugin in a [Mailgun account](http://www.mailgun.com/). Mailgun has a free account that lets you send up to 200 emails per day, which is great for testing. Paid subscriptions are available for increased limits.
16
+
17
+ The current version of this plugin only handles sending emails. Future releases will include additional functionality around processing incoming emails, tracking and analytics. Until then, you can configure how to handle incoming emails and set up analytics in the Mailgun control panel or using the Mailgun API.
18
+
19
+ == Installation ==
20
+
21
+ 1. Upload the `mailgun` folder to the `/wp-content/plugins/` directory or install directly through the plugin installer
22
+ 1. Activate the plugin through the 'Plugins' menu in WordPress or by using the link provided by the plugin installer
23
+ 1. Visit the settings page in the Admin at `Settings -> Mailgun` and configure the plugin with your account details
24
+
25
+ == Frequently Asked Questions ==
26
+
27
+ = Testing the configuration fails when using the HTTP API =
28
+
29
+ Your web server may not allow outbound HTTP connections. Set `Use HTTP API` to "No", and fill out the configuration options to SMTP and test again.
30
+
31
+ = Testing the configuration fails when using SMTP =
32
+
33
+ Your web server may not allow outbound SMTP connections on port 465 for secure connections or 587 for unsecured connections. Try changing `Use Secure SMTP` to "No" or "Yes" depending on your current configuration and testing again. If both fail, try setting `Use HTTP API` to "Yes" and testing again.
34
+
35
+ = Can this be configured globally for WordPress Multisite? =
36
+
37
+ Yes, using the following constants that can be placed in wp-config.php:
38
+
39
+ `
40
+ MAILGUN_USEAPI Type: boolean
41
+ MAILGUN_APIKEY Type: string
42
+ MAILGUN_DOMAIN Type: string
43
+ MAILGUN_USERNAME Type: string
44
+ MAILGUN_PASSWORD Type: string
45
+ MAILGUN_SECURE Type: boolean
46
+ `
47
+
48
+ == Screenshots ==
49
+
50
+ 1. Configuration options for using the Mailgun HTTP API
51
+ 2. Configuration options for using the Mailgun SMTP servers
52
+
53
+ == Upgrade Notice ==
54
+
55
+ = 0.1 =
56
+
57
+ Initial Release
58
+
59
+ == ChangeLog ==
60
+
61
+ = 0.1 (2012-11-21): =
62
+ * Initial Release