0

I've successfully managed to send email by using drupal_mail and hook_mail, but the next step is to send attachment within the email. I've read that this is not possible without adding extra modules, like MIME and Mail System.

OK, here's my code that sends email without attachments:

<?php
//The form page callback
function mymodule_news_page($form, &$form_state) {
    $form['title'] = array(
        '#type' => 'markup',
        '#markup' => '<div class="add-news-title">Add news</div>',
    );
    $form['upload'] = array(
        '#type' => 'managed_file', 
        '#title' => t('Upload image'),

    );
    $form['description'] = array(
        '#type' => 'textarea',
        '#title' => t('Description'),
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
        '#submit' => array('mymodule_news_submit'),
    );

    return $form;
}

function mymodule_news_submit($form, &$form_state) {
    $upload = $form_state['values']['upload'];
    $description = $form_state['values']['description'];

    $params = array('upload' => $upload, 'description' => $description ); 
    drupal_mail('mymodule', 'mymodule_news_mail', 'myemail', language_default(), $params);
    drupal_set_message(t('Message sent!')); 
    //dpm($form_state); 
}

/*
** Implements hook_mail().
*/
function mymodule_mail($key, &$message, $params) {
    switch($key) {    
        case 'mymodule_news_mail':
            $message['subject'] = t('Hello');
            $message['body'][] = t('This is a test for adding news');
            $message['body'][] = t('Image upload: @upload', array('@upload' => $params['upload']));
            $message['body'][] = t('Description: @description', array('@description' => $params['description']));   
        break;    
    }
}

As you can see I have a form that has three fields: upload, description and submit button. I'm trying to upload a image, but on submitting I receive an email with filled description and the uploaded image is not in the email, but it shows some numbers like 110...

Now I've enabled the MIME and Mail System module, and from their README module I can see this line: "'attachments': Array of arrays with the path or content, name and MIME type of the file (optional)."

I guess this is for uploading files, but there's no other documentation, so it's not clear how can I use MIME mail with hook_mail and drupal_mail().

What do I have to add in my code, so the uploaded file will be sent into the email?

3

No, the part in the documentation is not referring to file uploads, but indeed to mail sending. The expected array structure can look like this:

'attachments' => array(
  array(
    'filename' => 'text.csv',
    'filepath' => $filepath,
    'filemime' => 'application/csv',
  ),
),

or

'attachments' => array(
  array(
    'filename' => 'text.csv',
    'filecontent' => 'test content',
    'filemime' => 'application/csv',
  ),
),

Also have a look at this for another example: https://drupal.org/comment/5617230#comment-5617230

Edit: The attributes that you can use

  • filename: This is the name of the file as it will appear for the mails recipient. So you can freely choose this name.
  • filepath: If you want to attach a file that exists on your server, then this is the attribute that you want to use. Specifiy the full path to the file (e.g. image).
  • filecontent: If the attachement doesn't exist on your server but you want to send some content as an attachment, then you can use this attribute to pass on the content.
  • filemime: Here you can specifiy the mimetype of your attachment. This information gives the users mail application some basic information what to do with the attachment. An attachment of type application/pdf will probably open in the local pdf application, if the mimetype is image/jpeg it will probably open in an image viewer application.

filepath and filecontent are somewhat mutual exclusive. I'm not sure what will be the result, when you specify both.

  • Didn't know you could attach a file without saving it to disk, that's really handy to know – Clive Feb 19 '14 at 16:36
  • @berliner, can you be more specific? From where do I declare $filepath, is text.csv just some example, what is appilication/csv? I only want to attach an image. I'm pretty confused how to put this in my code. – user2519032 Feb 19 '14 at 21:44
  • @user2519032 See my updated answer. – berliner Feb 19 '14 at 23:42

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.