</tr>

					<tr>
						<th><?php 
_e('E-mail Content', 'cp');
?>
</th>
						<td>
							<p class="description"><?php 
_e('These codes will be replaced with actual data: STUDENT_FIRST_NAME, COURSE_NAME, COURSE_EXCERPT, COURSE_ADDRESS, WEBSITE_ADDRESS', 'cp');
?>
</p>
							<?php 
$editor_name = "option_invitation_content_email";
$editor_id = "option_invitation_content_email";
$editor_content = stripslashes(coursepress_get_invitation_content_email());
$args = array("textarea_name" => $editor_name, "textarea_rows" => 10, 'wpautop' => true);
// Filter $args before showing editor
$args = apply_filters('coursepress_element_editor_args', $args, $editor_name, $editor_id);
wp_editor($editor_content, $editor_id, $args);
?>
						</td>
					</tr>

					</tbody>
				</table>
			</div>
			<!--/inside-->

		</div>
		<!--/postbox-->
function coursepress_send_email($email_args = array())
{
    if ($email_args['email_type'] == 'student_registration') {
        global $course_slug;
        $email_address = $email_args['student_email'];
        $subject = coursepress_get_registration_email_subject();
        $courses_address = trailingslashit(home_url()) . trailingslashit($course_slug);
        $tags = array('STUDENT_FIRST_NAME', 'STUDENT_LAST_NAME', 'STUDENT_USERNAME', 'STUDENT_PASSWORD', 'BLOG_NAME', 'LOGIN_ADDRESS', 'COURSES_ADDRESS', 'WEBSITE_ADDRESS');
        $tags_replaces = array($email_args['student_first_name'], $email_args['student_last_name'], $email_args['student_username'], $email_args['student_password'], get_bloginfo(), cp_student_login_address(), $courses_address, home_url());
        $message = coursepress_get_registration_content_email();
        $message = str_replace($tags, $tags_replaces, $message);
        add_filter('wp_mail_from', 'my_registration_from_function');
        if (!function_exists('my_registration_from_function')) {
            function my_registration_from_function($email)
            {
                return coursepress_get_registration_from_email();
            }
        }
        add_filter('wp_mail_from_name', 'my_registration_from_name_function');
        if (!function_exists('my_registration_from_name_function')) {
            function my_registration_from_name_function($name)
            {
                return coursepress_get_registration_from_name();
            }
        }
    }
    if ($email_args['email_type'] == 'enrollment_confirmation') {
        global $course_slug;
        $email_address = $email_args['student_email'];
        $dashboard_address = $email_args['dashboard_address'];
        $subject = coursepress_get_enrollment_email_subject();
        $courses_address = trailingslashit(home_url()) . trailingslashit($course_slug);
        $course = new Course($email_args['course_id']);
        $tags = array('STUDENT_FIRST_NAME', 'STUDENT_LAST_NAME', 'BLOG_NAME', 'LOGIN_ADDRESS', 'COURSES_ADDRESS', 'WEBSITE_ADDRESS', 'COURSE_ADDRESS', 'COURSE_TITLE', 'STUDENT_DASHBOARD');
        $tags_replaces = array($email_args['student_first_name'], $email_args['student_last_name'], get_bloginfo(), cp_student_login_address(), $courses_address, home_url(), $course->get_permalink(), $course->details->post_title, $email_args['dashboard_address']);
        $message = coursepress_get_enrollment_content_email();
        $message = str_replace($tags, $tags_replaces, $message);
        add_filter('wp_mail_from', 'my_enrollment_from_function');
        if (!function_exists('my_enrollment_from_function')) {
            function my_enrollment_from_function($email)
            {
                return coursepress_get_enrollment_from_email();
            }
        }
        add_filter('wp_mail_from_name', 'my_enrollment_from_name_function');
        if (!function_exists('my_enrollment_from_name_function')) {
            function my_enrollment_from_name_function($name)
            {
                return coursepress_get_enrollment_from_name();
            }
        }
    }
    if ($email_args['email_type'] == 'student_invitation') {
        global $course_slug;
        $email_address = $email_args['student_email'];
        if (isset($email_args['course_id'])) {
            $course = new Course($email_args['course_id']);
        }
        $tags = array('STUDENT_FIRST_NAME', 'STUDENT_LAST_NAME', 'COURSE_NAME', 'COURSE_EXCERPT', 'COURSE_ADDRESS', 'WEBSITE_ADDRESS', 'PASSCODE');
        $tags_replaces = array($email_args['student_first_name'], $email_args['student_last_name'], $course->details->post_title, $course->details->post_excerpt, $course->get_permalink(), home_url(), $course->details->passcode);
        if ($email_args['enroll_type'] == 'passcode') {
            $message = coursepress_get_invitation_content_passcode_email();
            $subject = coursepress_get_invitation_passcode_email_subject();
        } else {
            $message = coursepress_get_invitation_content_email();
            $subject = coursepress_get_invitation_email_subject();
        }
        $message = str_replace($tags, $tags_replaces, $message);
        add_filter('wp_mail_from', 'my_passcode_from_function');
        if (!function_exists('my_passcode_from_function')) {
            function my_passcode_from_function($email)
            {
                return coursepress_get_invitation_passcode_from_email();
            }
        }
        add_filter('wp_mail_from_name', 'my_passcode_from_name_function');
        if (!function_exists('my_passcode_from_name_function')) {
            function my_passcode_from_name_function($name)
            {
                return coursepress_get_invitation_passcode_from_name();
            }
        }
    }
    if ('instructor_invitation' == $email_args['email_type']) {
        global $course_slug;
        $course = '';
        $course_summary = '';
        $course_name = '';
        $courses_address = trailingslashit(home_url()) . trailingslashit($course_slug);
        $bugfix = false;
        if (isset($email_args['course_id'])) {
            $course = new Course($email_args['course_id']);
            $course_name = $course->details->post_title;
            $course_summary = $course->details->post_excerpt;
            // For unpublished courses.
            $permalink = '';
            if (in_array($course->details->post_status, array('draft', 'pending', 'auto-draft'))) {
                $permalink = CoursePress::instance()->get_course_slug(true) . '/' . $course->details->post_name . '/';
            } else {
                $permalink = get_permalink($email_args['course_id']);
            }
            $course_address = $permalink;
        }
        $confirm_link = $course_address . '?action=course_invite&course_id=' . $email_args['course_id'] . '&c=' . $email_args['invite_code'] . '&h=' . $email_args['invite_hash'];
        $email_address = $email_args['instructor_email'];
        $subject = cp_get_instructor_invitation_email_subject();
        $tags = array('INSTRUCTOR_FIRST_NAME', 'INSTRUCTOR_LAST_NAME', 'INSTRUCTOR_EMAIL', 'CONFIRMATION_LINK', 'COURSE_NAME', 'COURSE_EXCERPT', 'COURSE_ADDRESS', 'WEBSITE_ADDRESS', 'WEBSITE_NAME');
        $tags_replaces = array($email_args['first_name'], $email_args['last_name'], $email_address, $confirm_link, $course_name, $course_summary, $course_address, home_url(), get_bloginfo());
        $message = cp_get_instructor_invitation_email();
        $message = str_replace($tags, $tags_replaces, $message);
        add_filter('wp_mail_from', 'my_instructor_invitation_from_function');
        if (!function_exists('my_instructor_invitation_from_function')) {
            function my_instructor_invitation_from_function($email)
            {
                return coursepress_get_instructor_invitation_from_email();
            }
        }
        add_filter('wp_mail_from_name', 'my_instructor_invitation_from_name_function');
        if (!function_exists('my_instructor_invitation_from_name_function')) {
            function my_instructor_invitation_from_name_function($name)
            {
                return coursepress_get_instructor_invitation_from_name();
            }
        }
    }
    add_filter('wp_mail_content_type', 'cp_email_set_content_type');
    if (!function_exists('cp_email_set_content_type')) {
        function cp_email_set_content_type($content_type)
        {
            return 'text/html';
        }
    }
    add_filter('wp_mail_charset', 'cp_set_charset');
    if (!function_exists('cp_set_charset')) {
        function cp_set_charset($charset)
        {
            return get_option('blog_charset');
        }
    }
    return wp_mail($email_address, stripslashes($subject), stripslashes(nl2br($message)));
}