$mailform = new mail_edit_form('sendmail.php', array('oldmail' => get_record('email_mail', 'id', $mailid), 'action' => $action), 'post', '', array('name' => 'sendmail'));
if ($mailform->is_cancelled()) {
    // Only redirect
    redirect($CFG->wwwroot . '/blocks/email_list/email/index.php?id=' . $courseid, '', '0');
} else {
    if ($form = $mailform->get_data()) {
        if (empty($form->to) and empty($form->cc) and empty($form->bcc)) {
            notify(get_string('nosenders', 'block_email_list'));
            $mailform->set_data($form);
            $mailform->display();
        } else {
            if (!empty($form->send) or !empty($form->draft)) {
                // Create new eMail
                $email = new eMail($USER->id, $courseid);
                // User send mail
                $email->set_writer($USER->id);
                $email->set_course($courseid);
                // Generic URL for send mails errors
                $baseurl = $CFG->wwwroot . '/blocks/email_list/email/index.php?id=' . $courseid . '&mailid=' . $form->id . '&subject=\'' . $form->subject . '\'&body=\'' . $form->body . '\'';
                // Add subject
                $email->set_subject($form->subject);
                // Get upload file's
                $email->set_attachments(isset($_FILES) ? $_FILES : NULL);
                // If forward or draft mail, can do old attachments
                $i = 0;
                $oldattach = 'oldattachment' . $i;
                if (isset($form->{$oldattach})) {
                    $attachments = array();
                    while (true) {
                        $oldattachck = $oldattach . 'ck';
                        // Only add if it is checked