Skip to content

Commit

Permalink
Merge branch 'MDL-44257-26' of git://github.com/lameze/moodle into MO…
Browse files Browse the repository at this point in the history
…ODLE_26_STABLE
  • Loading branch information
Damyon Wiese committed Sep 16, 2014
2 parents bc5014b + be0d33a commit b068dc7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/moodlelib.php
Expand Up @@ -5854,7 +5854,16 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '',
} else {
require_once($CFG->libdir.'/filelib.php');
$mimetype = mimeinfo('type', $attachname);
$mail->addAttachment($CFG->dataroot .'/'. $attachment, $attachname, 'base64', $mimetype);

$attachmentpath = $attachment;

// If the attachment is a full path to a file in the tempdir, use it as is,
// otherwise assume it is a relative path from the dataroot (for backwards compatibility reasons).
if (strpos($attachmentpath, $CFG->tempdir) !== 0) {
$attachmentpath = $CFG->dataroot . '/' . $attachmentpath;
}

$mail->addAttachment($attachmentpath, $attachname, 'base64', $mimetype);
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/tests/messagelib_test.php
Expand Up @@ -188,6 +188,11 @@ public function test_message_attachment_send() {
$email = reset($emails);
$this->assertTrue(strpos($email->body, 'Content-Disposition: attachment;') !== false);
$this->assertTrue(strpos($email->body, 'emailtest.txt') !== false);

// Check if the stored file still exists after remove the temporary attachment.
$storedfileexists = $fs->file_exists($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'],
$filerecord['itemid'], $filerecord['filepath'], $filerecord['filename']);
$this->assertTrue($storedfileexists);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions message/output/email/message_output_email.php
Expand Up @@ -77,8 +77,7 @@ function send_message($eventdata) {
} else {
// Copy attachment file to a temporary directory and get the file path.
$attachment = $eventdata->attachment->copy_content_to_temp();
// Function email_to_user() adds $CFG->dataroot to file path, so removing it here.
$attachment = str_replace($CFG->dataroot, '', $attachment);

// Get attachment file name.
$attachname = clean_filename($eventdata->attachname);
}
Expand All @@ -88,7 +87,9 @@ function send_message($eventdata) {
$eventdata->fullmessagehtml, $attachment, $attachname);

// Remove an attachment file if any.
@unlink($attachment);
if (!empty($attachment) && file_exists($attachment)) {
unlink($attachment);
}

return $result;
}
Expand Down

0 comments on commit b068dc7

Please sign in to comment.