Search code examples
phpemaildrupaldrupal-7

Drupal email with attachments


I have seen the question posed on here many times but none of the solutions seems to work for me so I'm going to post it again.

I am trying to attach a document to an email in drupal. I have a custom form that includes a file field. I have the file filed uploading working fine and I have the path to the file confirmed. I installed MIME and Mail System modules in Drupal. I added a _mail function the custom module that I inherited for the project. This is how I have it based on what I have seen in examples:

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

    $attachment = array(
        'filecontent' => DRUPAL_ROOT .'/sites/our.website.com/files/'.$params['upload'],
        'filename' => $params['upload'],
        'filemime' => $params['filemime']
    );

    $langcode = $message['language']->language;
    $message['subject'] = $params['subject'];
    $message['body'][] = $params['body'];
    $message['params']['attachments'][] = $attachment;

}

In my form submission I have this for the function which should compose the email and attach the file. Everything seems to work but no attachment.

function champs_admin_email_form_submit($form, &$form_state)
{

    set_time_limit(1000);

    //Database stuff here

   if($form_state['values']['upload']!=''){

        $attachment = array(
            'filecontent' => DRUPAL_ROOT .'/sites/our.website.com/files/'.$form_state['values']['upload'],
            'filename' => $form_state['values']['upload'],
            'filemime' => $form_state['values']['filemime']
        );
    }else{
        $attachment = array();
    };


    foreach($result as $row)
    {
        $to = $form_state['values']['template'] == "supervisor" ? $row->supervisor_email : $row->mail;

        $params = array(
            '!champname' => $row->fname . " " . $row->lname,
            '!champfirstname' => $row->fname,
            '!champlastname' => $row->lname,
            '!supervisor' => $row->supervisor_name,
            'subject' => $form_state['values']['subject'],
            'body' => is_array($form_state['values']['body']) ? check_markup($form_state['values']['body']['value'], $form_state['values']['body']['format']) : $form_state['values']['body'],
            'attachment' => $attachment,
        );

        drupal_mail('champs', 'custom', $to, language_default(), $params,$form_state['values']['from']);

    }

    drupal_set_message("Email sent");
    $form_state['redirect'] = 'admin/config/system/champs';
}

Any ideas on what I am missing in this process or if MIME or Mail System needs additional configuring. Thanks and let me know if anyone seeing anything out of the ordinary.


Solution

  • I ended up rewriting the mailing function using straight HTML and headers and just use the mail() function in PHP.