255 lines
9.4 KiB
PHP
255 lines
9.4 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: Send WP Mail
|
|
* Update URI: false
|
|
* Description: Send email from WordPress. Sends email using WordPress core wp_mail function - if enabled.
|
|
* Version: 1.1
|
|
* Author: abuyoyo
|
|
* Domain Path: /languages
|
|
* Text Domain: swpm
|
|
*/
|
|
|
|
|
|
# Exit if accessed directly
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
define( 'SWPM_PLUGIN_DIR_URL', plugin_dir_url( __FILE__ ) );
|
|
define( 'SWPM_PLUGIN_VER', '0.9.3' );
|
|
|
|
/**
|
|
* Add our sub menu in the Tools menu
|
|
*
|
|
* @since 0.9.2
|
|
*/
|
|
function swpm_plugin_add_admin_page() {
|
|
// create swpm submenu page under the Tools menu
|
|
$swpm_page = add_submenu_page( 'tools.php', 'Send WP Mail', 'Send Email', 'manage_options', 'send-wp-mail', 'swpm_plugin_main' );
|
|
// load js and css on swpm page only
|
|
add_action( 'load-' . $swpm_page, 'swpm_plugin_scripts' );
|
|
}
|
|
add_action( 'admin_menu', 'swpm_plugin_add_admin_page' );
|
|
|
|
/**
|
|
* Load our css and js.
|
|
*
|
|
* @since 0.9.2
|
|
*/
|
|
function swpm_plugin_scripts() {
|
|
wp_enqueue_style( 'swpm_admin_css', SWPM_PLUGIN_DIR_URL . 'css/send-wp-mail.css', '', SWPM_PLUGIN_VER );
|
|
wp_enqueue_script( 'swpm_admin_js', SWPM_PLUGIN_DIR_URL . 'js/send-wp-mail.js', array('jquery'), SWPM_PLUGIN_VER);
|
|
}
|
|
|
|
/**
|
|
* Register our text domain.
|
|
*
|
|
* @since 0.9
|
|
*/
|
|
function swpm_plugin_load_textdomain() {
|
|
load_plugin_textdomain( 'swpm', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
|
|
}
|
|
add_action('plugins_loaded', 'swpm_plugin_load_textdomain');
|
|
|
|
/**
|
|
* Our main function to display and process our form
|
|
*
|
|
* @since 0.9
|
|
*/
|
|
function swpm_plugin_main() {
|
|
// get site info to construct 'FROM' for email
|
|
$from_name = wp_specialchars_decode( get_option('blogname'), ENT_QUOTES );
|
|
$from_email = get_bloginfo('admin_email');
|
|
|
|
// initialize
|
|
$send_mail_message = false;
|
|
|
|
if ( !empty( $_POST ) && check_admin_referer( 'send-wp-mail', 'send-wp-mail' ) ) {
|
|
// handle attachment
|
|
$attachment_path = '';
|
|
if ( $_FILES ) {
|
|
if ( !function_exists( 'wp_handle_upload' ) ) {
|
|
require_once( ABSPATH . 'wp-admin/includes/file.php' );
|
|
}
|
|
$uploaded_file = $_FILES['attachment'];
|
|
$upload_overrides = array( 'test_form' => false );
|
|
$attachment = wp_handle_upload( $uploaded_file, $upload_overrides );
|
|
if ( $attachment && !isset( $attachment['error'] ) ) {
|
|
// file was successfully uploaded
|
|
$attachment_path = $attachment['file'];
|
|
} else {
|
|
// echo $attachment['error'];
|
|
}
|
|
}
|
|
|
|
// get the posted form values
|
|
$email_to = isset( $_POST['email_to'] ) ? trim($_POST['email_to']) : '';
|
|
$email_subject = isset( $_POST['email_subject'] ) ? stripslashes(trim($_POST['email_subject'])) : '';
|
|
$email_body = isset( $_POST['email_body'] ) ? stripslashes(nl2br($_POST['email_body'])) : '';
|
|
$group_email = isset( $_POST['group_email'] ) ? trim($_POST['group_email']) : 'no';
|
|
$recipients = explode( ',',$email_to );
|
|
|
|
// initialize some vars
|
|
$errors = array();
|
|
$valid_email = true;
|
|
|
|
// simple form validation
|
|
if ( empty( $email_to ) ) {
|
|
$errors[] = __( "Please enter an email recipient in the To: field.", 'swpm' );
|
|
} else {
|
|
// Loop through each email and validate it
|
|
foreach( $recipients as $recipient ) {
|
|
if ( !filter_var( trim($recipient), FILTER_VALIDATE_EMAIL ) ) {
|
|
$valid_email = false;
|
|
break;
|
|
}
|
|
}
|
|
// create appropriate error msg
|
|
if ( !$valid_email ) {
|
|
$errors[] = _n( "The To: email address appears to be invalid.", "One of the To: email addresses appears to be invalid.", count($recipients), 'swpm' );
|
|
}
|
|
}
|
|
if ( empty($email_subject) ) $errors[] = __( "Please enter a Subject.", 'swpm' );
|
|
if ( empty($email_body) ) $errors[] = __( "Please enter a Message.", 'swpm' );
|
|
|
|
// send the email if no errors were found
|
|
if ( empty($errors) ) {
|
|
$headers[] = "Content-Type: text/html; charset=\"" . get_option('blog_charset') . "\"\n";
|
|
$headers[] = 'From: ' . $from_name . ' <' . $from_email . ">\r\n";
|
|
$attachments = $attachment_path;
|
|
|
|
if ( $group_email === 'yes' ) {
|
|
if ( wp_mail( $email_to, $email_subject, $email_body, $headers, $attachments ) ) {
|
|
$send_mail_message = '<div class="updated">' . __( 'Your email has been successfully sent!', 'swpm' ) . '</div>';
|
|
} else {
|
|
$send_mail_message = '<div class="error">' . __( 'There was an error sending the email.', 'swpm' ) . '</div>';
|
|
}
|
|
} else {
|
|
foreach( $recipients as $recipient ) {
|
|
if ( wp_mail( $recipient, $email_subject, $email_body, $headers, $attachments ) ) {
|
|
$send_mail_message .= '<div class="updated">' . __( 'Your email has been successfully sent to ', 'swpm' ) . esc_html($recipient) . '!</div>';
|
|
} else {
|
|
$send_mail_message .= '<div class="error">' . __( 'There was an error sending the email to ', 'swpm' ) . esc_html($recipient) . '</div>';
|
|
}
|
|
}
|
|
}
|
|
|
|
// delete the uploaded file (attachment) from the server
|
|
if ( $attachment_path ) {
|
|
unlink($attachment_path);
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<div class="wrap" id="swpm-wrapper">
|
|
<h1><?php _e( 'Send WP Mail', 'swpm' ); ?></h1>
|
|
<?php
|
|
if ( !empty($errors) ) {
|
|
echo '<div class="error"><ul>';
|
|
foreach ($errors as $error) {
|
|
echo "<li>$error</li>";
|
|
}
|
|
echo "</ul></div>\n";
|
|
}
|
|
if ( $send_mail_message ) {
|
|
echo $send_mail_message;
|
|
}
|
|
?>
|
|
<div id="poststuff">
|
|
<div id="post-body" class="metabox-holder columns-2">
|
|
<div id="post-body-content">
|
|
<form method="POST" id="swpm-form" enctype="multipart/form-data">
|
|
<?php wp_nonce_field( 'send-wp-mail', 'send-wp-mail' ); ?>
|
|
<table cellpadding="0" border="0" class="form-table">
|
|
<tr>
|
|
<th scope=”row”>From:</th>
|
|
<td><input type="text" disabled value="<?php echo "$from_name <$from_email>"; ?>" required><div class="note"><?php _e( 'These can be changed in Settings->General.', 'swpm' ); ?></div></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope=”row”><label for="swpm-recipient-emails">To:</label></th>
|
|
<td><input type="email" multiple id="swpm-recipient-emails" name="email_to" value="<?php echo esc_attr( swpm_plugin_issetor($email_to) ); ?>" required><div class="note"><?php _e( 'To send to multiple recipients, enter each email address separated by a comma or choose from the user list below.', 'swpm' ); ?></div>
|
|
<select id="swpm-user-list">
|
|
<option value="">-- <?php _e( 'user list', 'swpm' ); ?> --</option>
|
|
<?php
|
|
$users = get_users( 'orderby=user_email' );
|
|
foreach ( $users as $user ) {
|
|
if ( $user->first_name && $user->last_name ) {
|
|
$user_fullname = ' (' . $user->first_name . ' ' . $user->last_name . ')';
|
|
} else {
|
|
$user_fullname = '';
|
|
}
|
|
echo '<option value="' . esc_html( $user->user_email ) . '">' . esc_html( $user->user_email ) . esc_html( $user_fullname) . '</option>';
|
|
};
|
|
?>
|
|
</select>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<th scope=”row”></th>
|
|
<td>
|
|
<div class="swpm-radio-wrap">
|
|
<input type="radio" class="radio" name="group_email" value="no" id="no"<?php if ( isset($group_email) && $group_email === 'no' ) echo ' checked'; ?> required>
|
|
<label for="no"><?php _e( 'Send each recipient an individual email', 'swpm' ); ?></label>
|
|
</div>
|
|
|
|
<div class="swpm-radio-wrap">
|
|
<input type="radio" class="radio" name="group_email" value="yes" id="yes"<?php if ( isset($group_email) && $group_email === 'yes' ) echo ' checked'; ?> required>
|
|
<label for="yes"><?php _e( 'Send a group email to all recipients', 'swpm' ); ?></label>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<th scope=”row”><label for="swpm-subject">Subject:</label></th>
|
|
<td><input type="text" id="swpm-subject" name="email_subject" value="<?php echo esc_attr( swpm_plugin_issetor($email_subject) );?>" required></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope=”row”><label for="email_body">Message:</label></th>
|
|
<td align="left">
|
|
<?php
|
|
$settings = array( "editor_height" => "200" );
|
|
wp_editor( swpm_plugin_issetor($email_body), "email_body", $settings );
|
|
?>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<th scope=”row”><label for="attachment">Attachment:</label></th>
|
|
<td><input type="file" id="attachment" name="attachment"></td>
|
|
</tr>
|
|
<tr>
|
|
<td colspan="2" align="right">
|
|
<input type="submit" value="<?php _e( 'Send WP Mail', 'swpm' ); ?>" name="submit" class="button button-primary">
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</form>
|
|
</div>
|
|
<div id="postbox-container-1" class="postbox-container">
|
|
<div class="postbox">
|
|
<h3><span>Like this plugin?</span></h3>
|
|
<div class="inside">
|
|
<ul>
|
|
<li><a href="https://wordpress.org/support/view/plugin-reviews/send-email-from-admin?filter=5" target="_blank">Rate it on WordPress.org</a></li>
|
|
<li><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=8HHLL6WRX9Z68" target="_blank">Donate to the developer</a></li>
|
|
</ul>
|
|
</div> <!-- .inside -->
|
|
</div>
|
|
</div>
|
|
<div class="clear"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Helper function for form values
|
|
*
|
|
* @since 0.9
|
|
*
|
|
* @param string $var Var name to test isset
|
|
*
|
|
* @return string $var value if isset or ''
|
|
*/
|
|
function swpm_plugin_issetor(&$var) {
|
|
return isset($var) ? $var : '';
|
|
}
|