📜 ⬆️ ⬇️

Sending a letter with an attached file from Drupal

Last week, one of the clients wanted to be able to send a letter from the admin to his subscribers with the ability to attach a file. Of course, it was possible to download a file from the form to the site and send a link to download the file in a letter, but, in addition to certain advantages of this solution, the obvious drawback, namely the accumulation of unnecessary files on the server, made me not even offer the client such a crutch solution.

So, fortunately, as a rule, it is extremely rare to reinvent the wheel in Drupal, the main thing is to be able to find a ready-made solution at drupal.org. I admit that I had to spend some time to find it and that’s why I’m writing this message to save you time if you face such a task.

I chose the Mime Mail module (http://drupal.org/project/mimemail), I did not find another alternative.

For ease of understanding, I will dilute my story with examples (let me choose Drupal 6, which is due to the fact that the customer’s site was in the six). To begin with, let's make a form for entering a letter and attaching a file:
')
function custom_mail_form() { $form['#attributes'] = array('enctype' => "multipart/form-data"); //       $form['mail_to'] = array( '#type' => 'textfield', '#title' => t('E-mail'), '#required' => TRUE, ); $form['mail_subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), '#required' => TRUE, ); $form['mail_body'] = array( '#type' => 'textarea', '#title' => t('Body'), '#required' => TRUE, ); $form['mail_attachment'] = array( '#type' => 'file', '#title' => t('Attachment'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Send'), ); return $form; } 


Depending on the task, you can not create a new form, but modify the existing one by adding the file field and the handler function using the form_alter hook.

I will not stop at validating the form data, I note that at least the 'mail_to' field must be validated. I will write immediately the handler of this form:

 function custom_mail_form_submit($form, &$form_state) { $folder = file_directory_path()."/mail/"; //  ,       ,         $attachment = array(); //       if ($file = file_save_upload('mail_attachment', array(), $folder)) { //   $attachment[] = $file; //         } //      , ,  ,     if (module_exists('mimemail')) { // ,    $message_result = mimemail( //   mimemail,        variable_get('site_mail', ini_get('sendmail_from')), //   $form_state['values']['mail_to'], //   $form_state['values']['mail_subject'], //   $form_state['values']['mail_body'], //     HTML,    ,    TRUE TRUE, //   ,        plaintext array(), //            check_plain($form_state['values']['mail_body']), //          plaintext,   check_plain()         html  $attachment, //        files,         '' //        ,    ); //   TRUE,     if($message_result) //     drupal_set_message("Mail was sent to ".$form_state['values']['mail_to']); else drupal_set_message("Mail was not sent to ".$form_state['values']['mail_to'], "error"); if(count($attachment)) file_delete($attachment[0]->filepath); //       ,      } else{ drupal_set_message("Please install <a href="http://drupal.org/project/mimemail">Mime Mail</a> module ".$mail, "error"); //        } } 


I hope there are enough comments in the code to understand how this works. The recipient will receive an email with one attachment ...

You can read more about using the mimemail () function in the Mime Mail module's README.txt.

PS: Thank you for the discussion of the material and comments. In addition to comments, I received several letters with valuable comments. I think it is worth sharing what rrromka wrote to me:

“It is possible to send letters to all registered users (with a filter by roles, for example), regardless of whether the user has signed up for the newsletter explicitly or not. You need to put the module drupal.org/project/simplenews_roles, then here: admin / content / simplenews / types in the settings of the desired distribution (distribution is a term from the dictionary that automatically creates simplenews) to specify the users from which role to attach to this distribution. And then, as usual: you create a “mailing” type material, choose a newsletter and send it. ”

This remark once again shows that almost all typical tasks have already been solved on drupal.org, you just need to search, ask questions in specialized forums and practice more :-)

Source: https://habr.com/ru/post/111541/


All Articles