0

(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 & cover letter to it.

Here are my submit and hook_mail functions.

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

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

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

        global $base_url;

        $attachment = array(
          'filecontent' => file_get_contents('http://localhost/public/counciltax.txt'),
          'filename' => 'counciltax.txt',
          'filemime' => 'text/plain',
          'list' => 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' => $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.

enter image description here

Here's the successful email.enter image description here]![enter image description here

And here's the unsuccessful email.enter image description here]![enter image description here

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:

enter image description here

This is the information I'm entering:

'uri' => 'public://hashtags.txt',
'filename' => 'README.txt',
'filemime' => 'text/plain',

The results of sending 12 or so in a row. It mostly works now, but still randomly failsenter image description here

I'm changing nothing inbetween the tries, it just doesn't work sometimes. Any other ideas or info about how I could debug this?

0

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, &$form_state) { ...
    $attachment = array(
      'uri' => $filepath,
      'filename' => 'counciltax.txt',
      'filemime' => 'text/plain',
    );
    // Add attachments      
    $params = array(
      'attachments' => $attachment,
      'file' => file_create_url($attachment['uri']),
    );

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

function tellafriend_mail($key, &$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' => $base_url));
  $message['params']['attachments'][] = $params['attachments'];  
}
  • Unfortunately no luck. I edited the original post to add more information about trying it your way. (Cheers for the help anyway.) It works most of time but still randomly breaks like before. Any ideas how I'd debug this? It's not throwing any errors I can use. – Dominic Woodman Aug 4 '13 at 23:24
0

Solved! Not a clever solution, but changing to the dev version of Mimemail cleared this up.

That's uninstalling the alpha and then installing "7.x-1.x-dev".

Never found out why it was happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.