2

Was doing something like this

        global $base_url;
        $uri = $base_url . '/sites/default/files/test.pdf';
        if(!empty($mail_content)) {
            /*send mail */
            $to =$mail_content['to'];       //gets the current user's mail address
            $from = variable_get('site_mail', '');    //admin's mail address
            $body = $mail_content['body'];

            $filemime = $mail_content['mime'];
            $filename = $mail_content['filename'];
            //$filepath = 
            //$filealtname  = drupal_basename($filearray['uri'], $suffix = NULL);

                $attachment = array(
                'filecontent' => 'http://localhost/cando/sites/default/files/test.pdf',
                'filename' => $filename,
                'filemime' => $filemime,
            ); 

            $mail_content['params']['attachments'][] = $attachment;
            $params = array(
                'subject' => $mail_content['subject'],
                'body' => $body,
                'attachment'=>$attachment,
            );

             // print_r($attachment);print_r($params);exit;
        //  print_r(file_get_contents(DRUPAL_ROOT));die;
            $message = drupal_mail('cando_custom', $mail_content['mkey'], $to, language_default(),$params,$from); 

            return 'Mail Sent ... ';

        }
    }

2 Answers 2

0

You forgot to add headers to the params array.

I did something like :

$attachment = array(
      'filecontent' => $filepathname,
      'filename' => $namefile,
      'filemime' => 'application/pdf'
      );
//where $filepathname should contain the path to the file and $filename should contain the name of the file.

$params = array(
  'headers' => array('Content-Type' => 'text/html'),
  'key' => 'test',
  'to' => '[email protected]',
  'from' => '[email protected]',
  'subject' => 'Test email',
  'body' => 'test',
  'attachment' => $attachment
);

drupal_mail($module, $key, $to, $language, $params, $from, $send);

which worked for me.

In your case:

  1. Check the value of $filemime variable.
  2. Add headers to the code.
0

Here is an example of how you can attach multiple attachments to an e-mail:

/**
 * Implements hook_mail().
 */
function mymodule_mail($key, &$message, $params) {
  switch($key) {     
    case 'test1':
      $message['subject'] = 'Hello World';
      $message['body'][] = $params['body'];
      $message['params']['attachments'][] = $params['attachment'];
      $message['params']['attachments'][] = $params['attachment2'];          


      break;

    case 'test2':
      $message['subject'] = 'Hello Kitty';
      $message['body'][] = $params['body'];
      $message['params']['attachments'][] = $params['attachment'];
      $message['params']['attachments'][] = $params['attachment2'];          


      break;      
  }
}

/**
 * Implements hook_node_presave().
 */
function mymodule_node_presave($node) {

        $attachment = array(
            'filecontent' => file_get_contents($node->field_name[LANGUAGE_NONE][0]['uri']),
            'filename' => $node->field_name[LANGUAGE_NONE][0]['filename'],
            'filemime' => $node->field_name[LANGUAGE_NONE][0]['filemime'],
        ); 

        $attachment2 = array(
            'filecontent' => file_get_contents($node->field_name[LANGUAGE_NONE][1]['uri']),
            'filename' => $node->field_name[LANGUAGE_NONE][1]['filename'],
            'filemime' => $node->field_name[LANGUAGE_NONE][1]['filemime'],
        ); 


        $params = array(
            'body' => 'cool',
            'attachment'=> $attachment,
            'attachment2'=> $attachment2,
        );

       $to = $node->field_enter_email[LANGUAGE_NONE][0]['value'];

       if($something){

           drupal_mail('mymodule', $key = 'test1', $to, language_default(), $params, $from = '[email protected]', $send = TRUE); 

       }
       else {

           drupal_mail('mymodule', $key = 'test2', $to, language_default(), $params, $from = '[email protected]', $send = TRUE); 

       } 

}
1
  • Only posting code puts your answer at risk to be deleted via moderation. Maybe add a few comments to briefly explain the "clue" of it (for those who don't get it right away)? Commented Dec 19, 2016 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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