/**
  * Constructor
  */
 public function __construct()
 {
     $this->id = 'new_user_account';
     $this->title = __('New user account', 'welcome-buddy');
     $this->description = __('New user account emails are sent to the user when they sign up via the BuddyPress registration page.', 'welcome-buddy');
     $this->template_html = 'new-user-account.php';
     $this->template_plain = 'plain/new-user-account.php';
     $this->subject = __('Welcome to {site_title}', 'welcome-buddy');
     $this->heading = __('Welcome to {site_title}', 'welcome-buddy');
     // Call parent constuctor
     parent::__construct();
 }
 public function bp_email(BP_Email $email)
 {
     $recipients = $email->get_to();
     $to = array();
     foreach ($recipients as $recipient) {
         $to[] = $recipient->get_address();
     }
     $subject = $email->get_subject('replace-tokens');
     $message = normalize_whitespace($email->get_content_plaintext('replace-tokens'));
     $filter_set = false;
     if ('plaintext' != Sendgrid_Tools::get_content_type()) {
         add_filter('wp_mail_content_type', array($this, 'set_html_content_type'), 100);
         $filter_set = true;
         $message = $email->get_template('add-content');
     }
     $result = wp_mail($to, $subject, $message);
     if ($filter_set) {
         remove_filter('wp_mail_content_type', array($this, 'set_html_content_type'));
     }
     return $result;
 }
Beispiel #3
0
/**
 * Add support for pre-2.5 email actions.
 *
 * @since 2.5.0
 *
 * @param bool|WP_Error $delivery_status Bool if the email was sent or not.
 *                                       If a WP_Error, there was a failure.
 * @param BP_Email $email Email object reference.
 * @return mixed
 */
function bp_core_deprecated_email_actions($delivery_status, $email)
{
    $pre_2_5_emails = array('activity-comment', 'activity-comment-author', 'core-user-registration', 'core-user-registration-with-blog', 'friends-request', 'friends-request-accepted', 'groups-details-updated', 'groups-invitation', 'groups-member-promoted', 'groups-membership-request', 'groups-membership-request-accepted', 'groups-membership-request-rejected', 'messages-unread', 'settings-verify-email-change');
    remove_action('bp_send_email_success', 'bp_core_deprecated_email_actions', 20);
    $email_content = $email->get('content');
    $email_subject = $email->get('subject');
    $email_type = $email->get('type');
    $tokens = $email->get('tokens');
    add_action('bp_send_email_success', 'bp_core_deprecated_email_actions', 20, 2);
    // Backpat for pre-2.5 emails only.
    if (!in_array($email_type, $pre_2_5_emails, true)) {
        return;
    }
    if ($email_type === 'activity-comment') {
        /**
         * Fires after the sending of a reply to an update email notification.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email. $params argument unset and deprecated.
         *
         * @param int    $user_id       ID of the original activity item author.
         * @param string $email_subject Email notification subject text.
         * @param string $email_content Email notification message text.
         * @param int    $comment_id    ID for the newly received comment.
         * @param int    $commenter_id  ID of the user who made the comment.
         * @param array  $deprecated    Removed in 2.5; now an empty array.
         */
        do_action('bp_activity_sent_reply_to_update_email', $tokens['original_activity.user_id'], $email_subject, $email_content, $tokens['comment.id'], $tokens['commenter.id'], array());
    } elseif ($email_type === 'activity-comment-author') {
        /**
         * Fires after the sending of a reply to a reply email notification.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email. $params argument unset and deprecated.
         *
         * @param int    $user_id       ID of the parent activity item author.
         * @param string $email_subject Email notification subject text.
         * @param string $email_content Email notification message text.
         * @param int    $comment_id    ID for the newly received comment.
         * @param int    $commenter_id  ID of the user who made the comment.
         * @param array  $deprecated    Removed in 2.5; now an empty array.
         */
        do_action('bp_activity_sent_reply_to_reply_email', $tokens['parent-comment-user.id'], $email_subject, $email_content, $tokens['comment.id'], $tokens['commenter.id'], array());
    } elseif ($email_type === 'core-user-registration') {
        if (!empty($tokens['user.id'])) {
            /**
             * Fires after the sending of activation email to a newly registered user.
             *
             * @since 1.5.0
             *
             * @param string $email_subject   Subject for the sent email.
             * @param string $email_content   Message for the sent email.
             * @param int    $user_id         ID of the new user.
             * @param string $recipient_email Email address of the new user.
             * @param string $key             Activation key.
             */
            do_action('bp_core_sent_user_validation_email', $email_subject, $email_content, $tokens['user.id'], $tokens['recipient.email'], $tokens['key']);
        } else {
            /**
             * Fires after the sending of the notification to new users for successful registration without blog.
             *
             * @since 1.5.0
             * @deprecated 2.5.0 Use the filters in BP_Email. $meta argument unset and deprecated.
             *
             * @param string $admin_email        Admin Email address for the site.
             * @param string $email_subject      Subject used in the notification email.
             * @param string $email_content      Message used in the notification email.
             * @param string $recipient_username The user's login name.
             * @param string $recipient_email    The user's email address.
             * @param string $key                The activation key created in wpmu_signup_blog().
             * @param array  $meta               Removed in 2.5; now an empty array.
             */
            do_action('bp_core_sent_user_signup_email', bp_get_option('admin_email'), $email_subject, $email_content, $tokens['recipient.username'], $tokens['recipient.email'], $tokens['key'], array());
        }
    } elseif ($email_type === 'core-user-registration-with-blog') {
        /**
         * Fires after the sending of the notification to new users for successful registration with blog.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email. $meta argument unset and deprecated.
         *
         * @param string $admin_email        Admin Email address for the site.
         * @param string $email_subject      Subject used in the notification email.
         * @param string $email_content      Message used in the notification email.
         * @param string $domain             The new blog domain.
         * @param string $path               The new blog path.
         * @param string $title              The site title.
         * @param string $recipient_username The user's login name.
         * @param string $recipient_email    The user's email address.
         * @param string $key                The activation key created in wpmu_signup_blog().
         * @param array  $meta               Removed in 2.5; now an empty array.
         */
        do_action('bp_core_sent_blog_signup_email', bp_get_option('admin_email'), $email_subject, $email_content, $tokens['domain'], $tokens['path'], $tokens['title'], $tokens['recipient.username'], $tokens['recipient.email'], $tokens['key_blog'], array());
    } elseif ($email_type === 'friends-request') {
        /**
         * Fires after the new friend request email is sent.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int    $friend_id     ID of the request recipient.
         * @param string $email_subject Text for the friend request subject field.
         * @param string $email_content Text for the friend request message field.
         * @param int    $friendship_id ID of the friendship object.
         * @param int    $initiator_id  ID of the friendship requester.
         */
        do_action('bp_friends_sent_request_email', $tokens['friend.id'], $email_subject, $email_content, $tokens['friendship.id'], $tokens['initiator.id']);
    } elseif ($email_type === 'friends-request-accepted') {
        /**
         * Fires after the friend request accepted email is sent.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int    $initiator_id  ID of the friendship requester.
         * @param string $email_subject Text for the friend request subject field.
         * @param string $email_content Text for the friend request message field.
         * @param int    $friendship_id ID of the friendship object.
         * @param int    $friend_id     ID of the request recipient.
         */
        do_action('bp_friends_sent_accepted_email', $tokens['initiator.id'], $email_subject, $email_content, $tokens['friendship.id'], $tokens['friend.id']);
    } elseif ($email_type === 'groups-invitation') {
        /**
         * Fires after the notification is sent that a member has been invited to a group.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int             $invited_user_id  ID of the user who was invited.
         * @param string          $email_subject    Email notification subject text.
         * @param string          $email_content    Email notification message text.
         * @param BP_Groups_Group $group            Group object.
         */
        do_action('bp_groups_sent_invited_email', $tokens['inviter.id'], $email_subject, $email_content, $tokens['group']);
    } elseif ($email_type === 'groups-member-promoted') {
        /**
         * Fires after the notification is sent that a member has been promoted.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int    $user_id       ID of the user who was promoted.
         * @param string $email_subject Email notification subject text.
         * @param string $email_content Email notification message text.
         * @param int    $group_id      ID of the group that the user is a member of.
         */
        do_action('bp_groups_sent_promoted_email', $tokens['user.id'], $email_subject, $email_content, $tokens['group.id']);
    } elseif ($email_type === 'groups-membership-request') {
        /**
         * Fires after the notification is sent that a member has requested group membership.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int    $admin_id           ID of the group administrator.
         * @param string $email_subject      Email notification subject text.
         * @param string $email_content      Email notification message text.
         * @param int    $requesting_user_id ID of the user requesting membership.
         * @param int    $group_id           ID of the group receiving membership request.
         * @param int    $membership_id      ID of the group membership object.
         */
        do_action('bp_groups_sent_membership_request_email', $tokens['admin.id'], $email_subject, $email_content, $tokens['requesting-user.id'], $tokens['group.id'], $tokens['membership.id']);
    } elseif ($email_type === 'groups-membership-request-accepted' || $email_type === 'groups-membership-request-rejected') {
        /**
         * Fires after the notification is sent that a membership has been approved.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int    $requesting_user_id ID of the user whose membership was approved.
         * @param string $email_subject      Email notification subject text.
         * @param string $email_content      Email notification message text.
         * @param int    $group_id           ID of the group that was joined.
         */
        do_action('bp_groups_sent_membership_approved_email', $tokens['requesting-user.id'], $email_subject, $email_content, $tokens['group.id']);
    }
}
 /**
  * Send the email.
  *
  * @since  1.0.0
  * @access public
  * @param  mixed  $to
  * @param  mixed  $subject
  * @param  mixed  $message
  * @param  string $headers (default: "Content-Type: text/html\r\n")
  * @param  string $attachments (default: "")
  * @return bool
  */
 public function send($to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "")
 {
     // Send
     $email = new BP_Email();
     return $email->send($to, $subject, $message, $headers, $attachments);
 }
 /**
  * Send email(s).
  *
  * @since 2.5.0
  *
  * @param BP_Email $email Email to send.
  * @return bool|WP_Error Returns true if email send, else a descriptive WP_Error.
  */
 public function bp_email(BP_Email $email)
 {
     static $phpmailer = null;
     if ($phpmailer === null) {
         if (!class_exists('PHPMailer')) {
             require_once ABSPATH . WPINC . '/class-phpmailer.php';
             require_once ABSPATH . WPINC . '/class-smtp.php';
         }
         $phpmailer = new PHPMailer(true);
     }
     /*
      * Resets.
      */
     $phpmailer->clearAllRecipients();
     $phpmailer->clearAttachments();
     $phpmailer->clearCustomHeaders();
     $phpmailer->clearReplyTos();
     $phpmailer->Sender = '';
     /*
      * Set up.
      */
     $phpmailer->IsMail();
     $phpmailer->CharSet = bp_get_option('blog_charset');
     $phpmailer->Hostname = self::get_hostname();
     /*
      * Content.
      */
     $phpmailer->Subject = $email->get_subject('replace-tokens');
     $content_plaintext = PHPMailer::normalizeBreaks($email->get_content_plaintext('replace-tokens'));
     if ($email->get('content_type') === 'html') {
         $phpmailer->msgHTML($email->get_template('add-content'), '', 'wp_strip_all_tags');
         $phpmailer->AltBody = $content_plaintext;
     } else {
         $phpmailer->IsHTML(false);
         $phpmailer->Body = $content_plaintext;
     }
     $recipient = $email->get_from();
     try {
         $phpmailer->SetFrom($recipient->get_address(), $recipient->get_name(), false);
     } catch (phpmailerException $e) {
     }
     $recipient = $email->get_reply_to();
     try {
         $phpmailer->addReplyTo($recipient->get_address(), $recipient->get_name());
     } catch (phpmailerException $e) {
     }
     $recipients = $email->get_to();
     foreach ($recipients as $recipient) {
         try {
             $phpmailer->AddAddress($recipient->get_address(), $recipient->get_name());
         } catch (phpmailerException $e) {
         }
     }
     $recipients = $email->get_cc();
     foreach ($recipients as $recipient) {
         try {
             $phpmailer->AddCc($recipient->get_address(), $recipient->get_name());
         } catch (phpmailerException $e) {
         }
     }
     $recipients = $email->get_bcc();
     foreach ($recipients as $recipient) {
         try {
             $phpmailer->AddBcc($recipient->get_address(), $recipient->get_name());
         } catch (phpmailerException $e) {
         }
     }
     $headers = $email->get_headers();
     foreach ($headers as $name => $content) {
         $phpmailer->AddCustomHeader($name, $content);
     }
     /**
      * Fires after PHPMailer is initialised.
      *
      * @since 2.5.0
      *
      * @param PHPMailer $phpmailer The PHPMailer instance.
      */
     do_action('bp_phpmailer_init', $phpmailer);
     /** This filter is documented in wp-includes/pluggable.php */
     do_action_ref_array('phpmailer_init', array(&$phpmailer));
     try {
         return $phpmailer->Send();
     } catch (phpmailerException $e) {
         return new WP_Error($e->getCode(), $e->getMessage(), $email);
     }
 }
/**
 * Add default email tokens.
 *
 * @since 2.5.0
 *
 * @param array $tokens Email tokens.
 * @param string $property_name Unused.
 * @param string $transform Unused.
 * @param BP_Email $email Email being sent.
 * @return array
 */
function bp_email_set_default_tokens($tokens, $property_name, $transform, $email)
{
    $tokens['site.admin-email'] = bp_get_option('admin_email');
    $tokens['site.url'] = home_url();
    // These options are escaped with esc_html on the way into the database in sanitize_option().
    $tokens['site.description'] = wp_specialchars_decode(bp_get_option('blogdescription'), ENT_QUOTES);
    $tokens['site.name'] = wp_specialchars_decode(bp_get_option('blogname'), ENT_QUOTES);
    // Default values for tokens set conditionally below.
    $tokens['email.preheader'] = '';
    $tokens['recipient.email'] = '';
    $tokens['recipient.name'] = '';
    $tokens['recipient.username'] = '';
    $tokens['unsubscribe'] = site_url('wp-login.php');
    // Who is the email going to?
    $recipient = $email->get('to');
    if ($recipient) {
        $recipient = array_shift($recipient);
        $user_obj = $recipient->get_user('search-email');
        $tokens['recipient.email'] = $recipient->get_address();
        $tokens['recipient.name'] = $recipient->get_name();
        if (!$user_obj && $tokens['recipient.email']) {
            $user_obj = get_user_by('email', $tokens['recipient.email']);
        }
        if ($user_obj) {
            // Unsubscribe link.
            $tokens['unsubscribe'] = esc_url(sprintf('%s%s/notifications/', bp_core_get_user_domain($user_obj->ID), function_exists('bp_get_settings_slug') ? bp_get_settings_slug() : 'settings'));
            $tokens['recipient.username'] = $user_obj->user_login;
        }
    }
    // Email preheader.
    $post = $email->get_post_object();
    if ($post) {
        $tokens['email.preheader'] = sanitize_text_field(get_post_meta($post->ID, 'bp_email_preheader', true));
    }
    return $tokens;
}
/**
 * Get an BP_Email object for the specified email type.
 *
 * This function pre-populates the object with the subject, content, and template from the appropriate
 * email post type item. It does not replace placeholder tokens in the content with real values.
 *
 * @since 2.5.0
 *
 * @param string $email_type Unique identifier for a particular type of email.
 * @return BP_Email|WP_Error BP_Email object, or WP_Error if there was a problem.
 */
function bp_get_email($email_type)
{
    $switched = false;
    // Switch to the root blog, where the email posts live.
    if (!bp_is_root_blog()) {
        switch_to_blog(bp_get_root_blog_id());
        $switched = true;
    }
    $args = array('no_found_rows' => true, 'numberposts' => 1, 'post_status' => 'publish', 'post_type' => bp_get_email_post_type(), 'suppress_filters' => false, 'tax_query' => array(array('field' => 'slug', 'taxonomy' => bp_get_email_tax_type(), 'terms' => $email_type)));
    /**
     * Filters arguments used to find an email post type object.
     *
     * @since 2.5.0
     *
     * @param array  $args       Arguments for get_posts() used to fetch a post object.
     * @param string $email_type Unique identifier for a particular type of email.
     */
    $args = apply_filters('bp_get_email_args', $args, $email_type);
    $post = get_posts($args);
    if (!$post) {
        if ($switched) {
            restore_current_blog();
        }
        return new WP_Error('missing_email', __FUNCTION__, array($email_type, $args));
    }
    /**
     * Filters arguments used to create the BP_Email object.
     *
     * @since 2.5.0
     *
     * @param WP_Post $post       Post object containing the contents of the email.
     * @param string  $email_type Unique identifier for a particular type of email.
     * @param array   $args       Arguments used with get_posts() to fetch a post object.
     * @param WP_Post $post       All posts retrieved by get_posts( $args ). May only contain $post.
     */
    $post = apply_filters('bp_get_email_post', $post[0], $email_type, $args, $post);
    $email = new BP_Email($email_type);
    /*
     * Set some email properties for convenience.
     */
    // Post object (sets subject, content, template).
    $email->set_post_object($post);
    /**
     * Filters the BP_Email object returned by bp_get_email().
     *
     * @since 2.5.0
     *
     * @param BP_Email $email      An object representing a single email, ready for mailing.
     * @param string   $email_type Unique identifier for a particular type of email.
     * @param array    $args       Arguments used with get_posts() to fetch a post object.
     * @param WP_Post  $post       All posts retrieved by get_posts( $args ). May only contain $post.
     */
    $retval = apply_filters('bp_get_email', $email, $email_type, $args, $post);
    if ($switched) {
        restore_current_blog();
    }
    return $retval;
}
 public function test_html_entities_are_decoded_in_email_recipient_names()
 {
     // Raw display name.
     $name = "Test o'Toole";
     // Emulate rendered {poster.name} token.
     $token = apply_filters('bp_core_get_user_displayname', $name);
     $email = new BP_Email('activity-at-message');
     $email->set_subject('{{poster.name}}')->set_tokens(array('poster.name' => $token));
     // Subject always has to have tokens replaced before sending.
     $this->assertSame($name, $email->get_subject('replace-tokens'));
 }
 public function test_appending_new_recipients_to_existing_recipients()
 {
     $email = new BP_Email('activity-at-message');
     $original_recipient = '*****@*****.**';
     $new_recipient = '*****@*****.**';
     $email->set_to($original_recipient);
     $addresses = $email->get_to();
     $this->assertSame($original_recipient, $addresses[0]->get_address());
     $email->set_to($new_recipient, '', 'add');
     $addresses = $email->get_to();
     $this->assertSame($original_recipient, $addresses[0]->get_address());
     $this->assertSame($new_recipient, $addresses[1]->get_address());
 }
/**
 * Sends a test email
 *
 * @since 1.0.0
 */
function bp_send_test_email()
{
    if (isset($_GET['send_bp_test_email'])) {
        if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'send-test-email')) {
            wp_die('Security check');
        }
        // Get current user
        $current_user = wp_get_current_user();
        // Send test email to current user
        $send_to = $current_user->user_email;
        // Load the mailer class
        $mailer = Welcome_Buddy::mailer();
        $email_heading = __('Email Preview', 'welcome-buddy');
        ob_start();
        include_once 'views/html-email-template-preview.php';
        $message = ob_get_clean();
        $email = new BP_Email();
        // Wrap the content with the email template and then add styles
        $message = $email->style_inline($mailer->wrap_message($email_heading, $message));
        $subject = wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES) . ' ' . __('Test Email', 'welcome-buddy');
        if ($email->send($send_to, $subject, $message)) {
            _e('Test email was sent', 'welcome-buddy');
        } else {
            _e('Test email was not sent', 'welcome-buddy');
        }
        exit;
    }
}
Beispiel #11
0
 /**
  * Set various email headers for BP 2.5 emails.
  *
  * This includes the 'Reply-To' and custom 'From' headers.
  *
  * @since 1.0-RC4
  *
  * @param bool|WP_Error $retval Returns true if validation is successful, else a descriptive WP_Error.
  * @param BP_Email      $email  Current instance of the email type class.
  */
 public function set_bp_email_headers($retval, $email)
 {
     if (!$email->get_to()) {
         return $retval;
     }
     if (empty($this->listener->item_id)) {
         return $retval;
     }
     $to = $email->get_to();
     // Backpat headers to be used for checks in 'bp_rbe_querystring' filter.
     $headers = array();
     $headers['to'] = array_shift($to)->get_address();
     $reply_to = $this->get_reply_to_address($headers);
     // If no reply to, bail.
     if (empty($reply_to)) {
         return $retval;
     }
     /**
      * Should we use the poster's display name as the 'From' name?
      *
      * @since 1.0-RC4.
      *
      * @param bool $retval
      */
     $use_custom_from_header = apply_filters('bp_rbe_use_custom_from_header', true);
     // Set custom 'From' header.
     if (!empty($this->listener->user_id) && true === $use_custom_from_header) {
         // Fetch current 'From' email address.
         $from = $email->get_from()->get_address();
         // Grab the host.
         $host = substr($from, strpos($from, '@') + 1);
         // Set the custom From email address and name.
         $email->set_from("noreply@{$host}", bp_core_get_user_displayname($this->listener->user_id));
     }
     /**
      * Set our custom 'Reply-To' email header.
      *
      * Have to workaround a mailbox character limit PHPMailer bug by wiping out
      * the Reply-To header and then setting it as a custom header.
      *
      * @link https://github.com/PHPMailer/PHPMailer/issues/706
      */
     $email->set_reply_to('');
     $email->set_headers(array('Reply-To' => $reply_to));
     return $retval;
 }