Sign in Registration
ruen

How to develop a Drupal 7 module for sending email, download ready

Writing a module for Drupal is easy. To get started, you can start by solving simple problems, for example, let's develop a simple module for sending messages to email.

drupal-module-delivery

Our module will be called Delivery , in general, you can take any name, the main thing is that there are no conflicts with existing module names. In the folder, create a delivery.info file with the following content:

  name = Delivery
description = Delivery
package = Delivery
core = 7.x
version = "7.x-1.00"
 

Everything is intuitive here, the name, description, package, kernel version and module version are listed. Next, create a file delivery.module , in which you can write the required functionality. For example, the minimal code for a mailing list might look like this:

  function delivery_menu () {
$ items = array ();

$ items ['admin / special / delivery'] = array (
'title' => t ('Newsletter email'),
'page callback' => 'drupal_get_form',
'page arguments' => array ('delivery_get_form'),
'access callback' => TRUE,
);

return $ items;
}

function delivery_get_form () {
$ form ['from'] = array (
'#title' => t ('Sender address'),
'#type' => 'textfield',
'#required' => TRUE,
);

$ form ['to'] = array (
'#title' => t ('Addresses of recipients'),
'#type' => 'file',
);

$ form ['subject'] = array (
'#title' => t ('Message subject'),
'#type' => 'textfield',
'#required' => TRUE,
);

$ form ['data'] = array (
'#title' => t ('Text of the letter'),
'#type' => 'text_format',
'#base_type' => 'textarea',
'#required' => TRUE,
);

$ form ['submit'] = array (
'#type' => 'submit',
'#value' => t ('Start mailing'),
);

return $ form;
}

function delivery_get_form_submit ($ form, & $ form_state) {
$ file = $ form_state ['values'] ['file'];
$ emails = file ($ file-> uri);
file_delete ($ file);

$ subject = $ form_state ['values'] ['subject'];
$ data = $ form_state ['values'] ['data'] ['value'];
$ from = $ form_state ['values'] ['from'];

$ subject = '=? utf-8? B?'. base64_encode ($ subject). '? =';
$ header = 'From:'. $ from. "\ r \ n";
$ header. = 'Reply-To:'. $ from. "\ r \ n";
$ header. = 'MIME-Version: 1.0'. "\ r \ n";
$ header. = 'Content-type: text / html; charset = UTF-8 '. "\ r \ n";
$ header. = 'Content-Transfer-Encoding: 8bit'. "\ r \ n";

foreach ($ emails as $ email) {
mail ($ email, $ subject, $ data, $ header);
}

drupal_set_message (t ('Mailing successfully completed'));
}
 

There are only three functions, but they solve the problem completely. The first delivery_menu function is the implementation of the hook_menu hook, in this hook you can easily list the necessary addresses at which the interface for managing the module's functionality will be available. We indicate that the standard Drupal forms engine will be used and assign the delivery_get_form function, which will be responsible for creating the form.

In the delivery_get_form function, we simply list the required fields and their settings, the Drupal Forms API is used. For the newsletter form, this example uses several fields:

  • field for specifying the sender's address,
  • field for specifying a file with email addresses, each address in such a file must be specified on a new line,
  • field for setting the message subject,
  • field for the main content of the letter,
  • button to send a letter.

Finally, the delivery_get_form_submit function, it triggers itself when the form described in the previous function is submitted, that is, after clicking the submit button. This is where a file with email addresses is opened and read into an array, then the mail function sends letters in a loop. All text should preferably be wrapped in the t function, which does auto-translation depending on the language of the site. I must say that this is the minimum implementation of the module, you can still add functions for validation and other improvements.

So, the development of the Drupal module for sending by email took very little time. Now it can be copied to the modules folder and included in the module settings pages. After that, the module will be available via the path indicated in the menu hook.

Download Delivery Module for Drupal 7

Comments (0)
For commenting sign in or register.
Оставить заявку

Latest articles

Popular sections

Eqsash (Tools)

Android app - VK LAST USER ID, отучитель от зависимости и т.д.:
Available on Google Play

Amessage (Communication)

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to

YouTube

Books

IT notes - In simple language about the most necessary things (HTML, CSS, JavaScript, PHP, databases, Drupal, Bitrix, SEO, domains, security and more), PDF, 500 p.