function _contact_attach_add_attachment in Contact Attach 7
Same name and namespace in other branches
- 5 contact_attach.module \_contact_attach_add_attachment()
- 6 contact_attach.module \_contact_attach_add_attachment()
Returns a fully-encoded attachment ready to be included into a message body.
Parameters
object $file: An attachment to add to the message.
array $mail_system: (optional) An associative array containing the contents of the persistent variable mail_system. Defaults to array().
bool $delete: (optional) A boolean indicating if the file will be deleted after it has been base64 encoded. Tests can set this to FALSE so that they can use the file to compare against after the message has been sent. Defaults to TRUE.
Return value
string The processed attachment, ready for appending to the message.
2 calls to _contact_attach_add_attachment()
- ContactAttachContactFormsTestCase::submitWithAttachments in ./
contact_attach.test - Submits contact forms with attachments after setting the settings.
- _contact_attach_process_attachments in ./
contact_attach.module - Checks for attachments and processes them, if one or more exist.
File
- ./
contact_attach.module, line 404 - Allows attaching files to messages sent using contact forms.
Code
function _contact_attach_add_attachment($file, $mail_system = array(), $delete = TRUE) {
$attachment = 'Content-Type: ' . $file->filemime . '; name="' . basename($file->filename) . "\"\n";
$attachment .= "Content-Transfer-Encoding: base64\n";
// SMTP module pulls the file path from the filename attribute in the header,
// so it can not contain only the file name if the SMTP module is used.
if (!empty($mail_system) && $mail_system['default-system'] === 'SmtpMailSystem') {
$attachment .= 'Content-Disposition: attachment; filename="' . $file->uri . "\"\n\n";
}
else {
$attachment .= 'Content-Disposition: attachment; filename="' . $file->filename . "\"\n\n";
}
if (file_exists($file->uri)) {
$attachment .= chunk_split(base64_encode(file_get_contents($file->uri)));
}
else {
$attachment .= chunk_split(base64_encode(file_get_contents(file_directory_temp() . '/' . $file->filename)));
}
if ($delete) {
// Delete the file after it has been embedded, as it no longer serves a
// purpose. Drupal deletes them after DRUPAL_MAXIMUM_TEMP_FILE_AGE has
// passed on the next cron run, but we can save Drupal the trouble by
// cleaning up after ourselves. This can also be important for privacy.
file_delete($file);
}
return $attachment;
}