Send attachments with drupal_mail I'm trying to send attachments with my email from Drupal. In my custom module i have added:

class SponsorprogramMailSystem implements MailSystemInterface {
  /**
   * Concatenate and wrap the e-mail body for plain-text mails.
   *
   * @param $message
   *   A message array, as described in hook_mail_alter().
   *
   * @return
   *   The formatted $message.
   */
  public function format(array $message) {
    $message['body'] = implode("\n\n", $message['body']);
    return $message;
  }
  /**
   * Send an e-mail message, using Drupal variables and default settings.
   *
   * @see http://php.net/manual/en/function.mail.php
   * @see drupal_mail()
   *
   * @param $message
   *   A message array, as described in hook_mail_alter().
   * @return
   *   TRUE if the mail was successfully accepted, otherwise FALSE.
   */
  public function mail(array $message) {
    $mimeheaders = array();
    foreach ($message['headers'] as $name =&gt; $value) {
      $mimeheaders[] = $name . ': ' . mime_header_encode($value);
    }
    $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
    return mail(
      $message['to'],
      mime_header_encode($message['subject']),
      // Note: e-mail uses CRLF for line-endings. PHP's API requires LF
      // on Unix and CRLF on Windows. Drupal automatically guesses the
      // line-ending format appropriate for your system. If you need to
      // override this, adjust $conf['mail_line_endings'] in settings.php.
      preg_replace('@\r?\n@', $line_endings, $message['body']),
      // For headers, PHP's API suggests that we use CRLF normally,
      // but some MTAs incorrectly replace LF with CRLF. See #234403.
      join("\n", $mimeheaders)
    );
  }
}


and i can send mails with html, that part is working.

But when i try to attach a file it does not arrive in my inbox. I attach my test file like this:

$attachment = array(
        'filecontent' =&gt; file_get_contents(DRUPAL_ROOT . '/README.txt'),
        'filename' =&gt; 'test.txt',
        'filemime' =&gt; 'text/plain',
      );


But nothing arrives.

Anyone know how i can fix it?
There might be other ways, but I have found that mailsystem and mimemail modules have to be installed to send email with attachment. So install these two modules first.

Then implement hook_mail to pass attachment to $message

/**
 * Implements hook_mail().
 */
function mymodule_mail($key, &amp;$message, $params) {
  $message['subject'] = $params['subject'];
  $message['body'][] = $params['body'];

  // Add attachment when available.
  if (isset($params['attachment'])) {
    $message['params']['attachments'][] = $params['attachment'];
  }
}


There are two ways to add attachment, you can either pass filecontent or filepath when adding an unmanaged file as attachment (not recorded in DB) or pass file object when adding an managed file.

When adding unmanaged file:

$attachment = array(
  'filepath' =&gt; $filepath, // or $uri
);


or

$attachment = array(
  'filecontent' =&gt; file_get_contents($uri),
  'filename' =&gt; $filename,
  'filemime' =&gt; 'application/pdf'
);


By using filecontent way, you probably will get two php errors by Jan 08, 2015 including


Warning: is_file() expects a valid path
Warning: realpath() expects parameter 1 to be a valid path


When adding managed file:

$attachment = file_load($fid);


Then send email by:

$params = array(
  'key' =&gt; 'my_email_template',
  'to' =&gt; 'test@example.com',
  'from' =&gt; 'test@example.com',
  'subject' =&gt; 'Test email',
  'body' =&gt; 'test',
  'attachment' =&gt; $attachment
);

drupal_mail('mymodule', $key, $to, $language, $params, $from);