If you need to send an e-mail programmatically, this is an example of how to do it.

Put this wherever you want.

      $message = 'Something went wrong. Please contact site administrator.';
      // Send the email.
      $params = array(
        'subject' => t('[Status] - Server is Down'),
        'body' => check_markup(
          t($message),
          'plain_text'
        ),
      );
      drupal_mail('module_name', 'any_key', $email, language_default(), $params);

Then you'll need to use the hook_mail() function.
This is how it should be created:

/**
 * Implements hook_mail for module_name.
 */
function module_name_mail($key, &$message, $params) {
  switch ($key) {
    case 'any_key':
      $message['subject'] = $params['subject'];
      $message['body'][] = $params['body'];
      break;
  }
}

Hope this helps.
Regards,
Sharif.

Comments

DiDebru’s picture

And how did he know where to send the mail ?

jo_as_neo’s picture

The third parameter is $to : The e-mail address or addresses where the message will be sent to. So in this example, $email.

https://api.drupal.org/api/drupal/includes%21mail.inc/function/drupal_ma...

johnhanley’s picture

Clean and concise example. Thank you.

ahaomar’s picture

please guide me full example for drupal 8 please above examples not working