- <?php
- * @file
- * Hook implementations for the Email Example module.
- */
-
- * @defgroup email_example Example: Email
- * @ingroup examples
- * @{
- * This example demonstrates how to use the mail API.
- *
- * This module provides two different examples of the Backdrop email API:
- * - Defines a simple contact form and shows how to use backdrop_mail() to
- * send an e-mail (defined in hook_mail()) when the form is submitted.
- * - Shows how modules can alter emails defined by other modules, using
- * hook_mail_alter(), to attach a custom signature before they are sent.
- */
-
- * Implements hook_mail().
- *
- * This hook defines a list of possible e-mail templates that this module can
- * send. Each e-mail is given a unique identifier, or 'key'.
- *
- * $message comes in with some standard properties already set: 'to' address,
- * 'from' address, and a set of default 'headers' from backdrop_mail(). The goal
- * of hook_mail() is to set the message's 'subject' and 'body' properties, as
- * well as make any adjustments to the headers that are necessary.
- *
- * The $params argument is an array which can hold any additional data required
- * to build the mail subject and body; for example, user-entered form data, or
- * some context information as to where the mail request came from.
- *
- * Note that hook_mail() is not actually a hook. It is only called for a single
- * module, the module named in the first argument of backdrop_mail(). So it's
- * a callback of a type, but not a hook.
- */
- function email_example_mail($key, &$message, $params) {
- global $user;
-
-
-
-
-
- $options = array(
- 'langcode' => $message['language']->langcode,
- );
-
- switch ($key) {
-
- case 'contact_message':
- $message['subject'] = t('E-mail sent from @site-name', array('@site-name' => config_get('system.core', 'site_name')), $options);
-
- $message['body'][] = t('@name sent you the following message:', array('@name' => $user->name), $options);
-
-
-
-
-
- $message['body'][] = check_plain($params['message']);
- break;
- }
- }
-
- * Sends an e-mail.
- *
- * @param array $form_values
- * An array of values from the contact form fields that were submitted.
- * There are just two relevant items: $form_values['email'] and
- * $form_values['message'].
- */
- function email_example_mail_send($form_values) {
-
-
- $module = 'email_example';
- $key = 'contact_message';
-
-
- $to = $form_values['email'];
- $from = config_get('system.core', 'site_mail');
-
-
-
-
- $params = $form_values;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $language = language_default();
-
-
-
-
- $send = TRUE;
-
-
-
- $result = backdrop_mail($module, $key, $to, $language, $params, $from, $send);
- if ($result['result'] == TRUE) {
- backdrop_set_message(t('Your message has been sent.'));
- }
- else {
- backdrop_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
- }
-
- }
-
- * Implements hook_mail_alter().
- *
- * This function is not required to send an email using Backdrop's mail system.
- *
- * Hook_mail_alter() provides an interface to alter any aspect of email sent by
- * Backdrop. You can use this hook to add a common site footer to all outgoing
- * email, add extra header fields, and/or modify the email in any way. HTML-izing
- * the outgoing email is one possibility.
- */
- function email_example_mail_alter(&$message) {
-
-
-
- $options = array(
- 'langcode' => $message['language']->langcode,
- );
-
- $signature = t("\n--\nMail altered by email_example module.", array(), $options);
- if (is_array($message['body'])) {
- $message['body'][] = $signature;
- }
- else {
-
- $message['body'] .= $signature;
- }
- }
-
- * Supporting functions.
- */
-
- * Implements hook_menu().
- *
- * Set up a page with an e-mail contact form on it.
- */
- function email_example_menu() {
- $items['example/email_example'] = array(
- 'title' => 'E-mail Example: contact form',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array('email_example_form'),
- 'access arguments' => array('access content'),
- );
-
- return $items;
- }
-
- * The contact form.
- */
- function email_example_form() {
- $form['intro'] = array(
- '#markup' => t('Use this form to send a message to an e-mail address. No spamming!'),
- );
- $form['email'] = array(
- '#type' => 'textfield',
- '#title' => t('E-mail address'),
- '#required' => TRUE,
- );
- $form['message'] = array(
- '#type' => 'textarea',
- '#title' => t('Message'),
- '#required' => TRUE,
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Submit'),
- );
-
- return $form;
- }
-
- * Form validation logic for the contact form.
- */
- function email_example_form_validate($form, &$form_state) {
- if (!valid_email_address($form_state['values']['email'])) {
- form_set_error('email', t('That e-mail address is not valid.'));
- }
- }
-
- * Form submission logic for the contact form.
- */
- function email_example_form_submit($form, &$form_state) {
- email_example_mail_send($form_state['values']);
- }
- * @} End of "defgroup email_example".
- */