Exemplo n.º 1
0
/**
 * $template_id - mail_templates.id
 * $user - User id or assoc array
 */
function send_user_mail($template, $user, $to = NULL)
{
    $CI =& get_instance();
    $CI->load->library('email');
    $CI->load->library('admin');
    $volunteer_coordinator = $CI->admin->get_volunteer_coordinator();
    $mail_sender_name = $CI->config->item('mail_sender_name');
    $mail_sender_email = $CI->config->item('mail_sender_email');
    if (!is_array($user)) {
        $user = get_user_assoc($user);
    }
    if ($template->recipient == MAILRECIPIENT_ADMIN || $to) {
        $user['application_url'] = site_url('admin/volunteers/show/' . $user['id']);
    }
    $user['homepage_url'] = base_url();
    $user['admin_email'] = $volunteer_coordinator->email;
    $user['admin_name'] = $volunteer_coordinator->name;
    $mail = render_mail($template, $user);
    $CI->email->initialize(array('mailtype' => 'html'));
    $CI->email->clear(TRUE);
    $CI->email->from($mail_sender_email, $mail_sender_name);
    if ($volunteer_coordinator) {
        $CI->email->reply_to($volunteer_coordinator->email, $volunteer_coordinator->name);
    }
    if ($to) {
        $CI->email->to($to);
    } else {
        if ($template->recipient == MAILRECIPIENT_APPLICANT) {
            $CI->email->to($user['email']);
        } else {
            if ($template->recipient == MAILRECIPIENT_ADMIN) {
                $CI->load->library('admin');
                // get all admin e-mails
                $emails = $CI->admin->get_admin_emails();
                $CI->email->to(implode(', ', $emails));
            }
        }
    }
    $CI->email->subject($mail->subject);
    $CI->email->message($mail->html);
    $CI->email->set_alt_message($mail->plaintext);
    /* The filenames that the attachments use on disk are not
       human-readable--they have IDs, not the original filenames.
       To restore the original filenames we must create a temp
       directory structure and then copy the files to the temp
       dir, attach from there, send the file, then clean up the
       temp dir. */
    $tmp = tempnam(sys_get_temp_dir(), 'vteer_mail_');
    if (!unlink($tmp) || !mkdir($tmp)) {
        throw new RuntimeException("Failed to create temp dir");
    }
    while ($attachment = $template->attachments->next()) {
        $filedata = make_attachment_path($attachment->id);
        $subtmp = $tmp . DIRECTORY_SEPARATOR . $attachment->id;
        $filetmp = $subtmp . DIRECTORY_SEPARATOR . $attachment->filename;
        if (!mkdir($subtmp) || !copy($filedata, $filetmp)) {
            throw new RuntimeException("Failed to attach file");
        }
        $CI->email->attach($filetmp);
    }
    $CI->email->send();
    if ($template->recurrence) {
        schedule_mail($user['id'], $template->templateid, new DateTime($template->recurrence));
    }
    $db = new DbConn();
    $db->exec('insert into mails_sent (userid, templateverid, sent) values (?, ?, ?)', $user['id'], $template->id, date_create());
    deltree($tmp);
}
Exemplo n.º 2
0
 function preview_attachment($attachId)
 {
     $db = new DbConn();
     $results = $db->query('select * from mail_attachments where id = ?', (int) $attachId);
     if ($results->length != 1) {
         show_error("File not found", 404);
     }
     $file = $results->next();
     $filename = $file->filename;
     $fileType = $file->type;
     if (!download_file(make_attachment_path($attachId), $filename, $fileType)) {
         show_error("File not found", 404);
     }
 }