Like the title says I want to make managed_file value to be a link on form submissions in the email.

Webform has this option. When a user uploads file, on the email form submission there's a link with the path of the uploaded file. I want to achieve this with custom module. This is my code:


function mymodule_page($form, &$form_state) {
    $form['description'] = array(
        '#type' => 'markup',
        '#markup' => t('Add description'),
    $form['file'] = array(
        '#type' => 'managed_file', 
        '#title' => t('Upload image'),
        '#upload_location' => 'public://files',

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
        '#submit' => array('mymodule_page_submit'),

    return $form;

function mymodule_page_submit($form, &$form_state) {
    $file = file_load($form_state['values']['file']);
    $file->status = FILE_STATUS_PERMANENT;


    $description = $form_state['values']['description'];

    $params = array('file' => $file->uri, 'description' => $description ); 
    drupal_mail('mymodule', 'mymodule_news_mail', 'myemail', language_default(), $params);
    drupal_set_message(t('Message sent!')); 

** 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');
            $message['body'][] = t('Image upload: @file', array('@file' => $params['file']));
            $message['body'][] = t('Description: @description', array('@description' => $params['description']));   

As you can see it from my code I'm saving the file properly into the server. After that with dpm($file->uri) I can see the string public://files/file_name.

How can I display the link from the path of the uploaded file on the email, so will be able to click on the absolute path?

1 Answer 1


Thanks to Jaypan from drupal.org, I've got the answer:


Simply with adding this code and make another variable that will use the code, I've got the right result.

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.