How to attach files using mimemail (Getting 0kb file) (This isn't the same as the other issue I raised. I thought I'd got the attachment working but I haven't)

Initial Problem

I'm trying to attach a file to an email. It's part of job application so a person goes to a node, presses apply and it sends an email to the creator of the node. I want to attach a CV &amp; cover letter to it.

Here are my submit and hook_mail functions.

function tellafriend_form_submit($form, &amp;$form_state) { ...

        $message = drupal_mail('tellafriend', 'notify', $email, language_default()); ...
}

function tellafriend_mail($key, &amp;$message, $params) {

        global $base_url;

        $attachment = array(
          'filecontent' =&gt; file_get_contents('http://localhost/public/counciltax.txt'),
          'filename' =&gt; 'counciltax.txt',
          'filemime' =&gt; 'text/plain',
          'list' =&gt; TRUE,
          );
        $message['subject'] = t('Your friend thought you might like this site');
        $message['body'][] = t("Hello,\nYour friend thought you might like this site.  Please click the link below to visit.\n\n!link", array('!link' =&gt; $base_url));
        $message['params']['attachments'][] = $attachment;
}


Sometimes this will send an attachment. Othertimes it won't. It sent once, I repeated the same process and it didn't work. Mostly it doesn't work.

These two tests were run 10 seconds apart and I didn't change any code.



Here's the successful email.

And here's the unsuccessful email.

Am I doing this wrong, or is this just broken? How would I debug this?

(This was the example I used as reference http://rapiddg.com/blog-post/drupal-7-html-e-mail-pdf-attachments)

EDIT  ----- 05/08/2013

I tried using frazras's technique (cheers!), of putting the attachment in the submit function however it still doesn't work every time. (Code not shown because it's identical to the section he posted.)

My public folder looks like this:



This is the information I'm entering:

'uri' =&gt; 'public://hashtags.txt',
'filename' =&gt; 'README.txt',
'filemime' =&gt; 'text/plain',


The results of sending 12 or so in a row. It mostly works now, but still randomly fails

I'm changing nothing inbetween the tries, it just doesn't work sometimes. Any other ideas or info about how I could debug this?
I usually do mine I little different from you, I add the attachment in hook_submit then pass it as a param to hook_mail, but even then some of our variables differ. here is an example from a working solution I have implemented, but changing to your text. I also added in the headers, maybe they arent necessary because I use it when dealing with jpeg file types.

function tellafriend_form_submit($form, &amp;$form_state) { ...
    $attachment = array(
      'uri' =&gt; $filepath,
      'filename' =&gt; 'counciltax.txt',
      'filemime' =&gt; 'text/plain',
    );
    // Add attachments      
    $params = array(
      'attachments' =&gt; $attachment,
      'file' =&gt; file_create_url($attachment['uri']),
    );

    drupal_mail('tellafriend', 'notify', $email, language_default()), $params);
}

function tellafriend_mail($key, &amp;$message, $params) {
  $message['headers']['MIME-Version'] = '1.0';
  $message['headers']['Content-Type'] = 'multipart/mixed;';
  $message['subject'] = t('Your friend thought you might like this site'); 
  $message['body'][] =t("Hello,\nYour friend thought you might like this site.  Please click the link below to visit.\n\n!link", array('!link' =&gt; $base_url));
  $message['params']['attachments'][] = $params['attachments'];  
}