You are here

public function ExampleForm::buildForm in Mime Mail 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

modules/mimemail_example/src/Form/ExampleForm.php, line 76

Class

ExampleForm
The example email contact form.

Namespace

Drupal\mimemail_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $dir = NULL, $img = NULL) {
  $form['intro'] = [
    '#markup' => $this
      ->t('Use this form to send a HTML message to an email address. No spamming!'),
  ];
  $form['key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Key'),
    '#description' => $this
      ->t('A key to identify the email sent.'),
    '#default_value' => 'test',
    '#required' => TRUE,
  ];
  $form['to'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('To'),
    '#description' => $this
      ->t('The email address of the recipient. The formatting of this string must comply with RFC 2822.'),
    '#default_value' => $this
      ->currentUser()
      ->getEmail(),
    '#required' => TRUE,
  ];
  $form['from'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Sender name'),
    '#description' => $this
      ->t("The sender's name. Leave empty to use the site-wide configured name."),
  ];
  $form['from_mail'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Sender email address'),
    '#description' => $this
      ->t("The sender's email address. Leave empty to use the site-wide configured address."),
  ];
  $form['params'] = [
    '#tree' => TRUE,
    'headers' => [
      'Cc' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Cc'),
        '#description' => $this
          ->t("The mail's carbon copy address. You may separate multiple addresses with comma."),
      ],
      'Bcc' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Bcc'),
        '#description' => $this
          ->t("The mail's blind carbon copy address. You may separate multiple addresses with comma."),
      ],
      'Reply-to' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Reply to'),
        '#description' => $this
          ->t("The address to reply to. Leave empty to use the sender's address."),
      ],
      'List-unsubscribe' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('List-unsubscribe'),
        '#description' => $this
          ->t('An email address and/or a URL which can be used for unsubscription. Values must be enclosed by angle brackets and separated by a comma.'),
      ],
    ],
    'subject' => [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Subject'),
      '#description' => $this
        ->t("The email's subject."),
    ],
    'body' => [
      '#type' => 'textarea',
      '#title' => $this
        ->t('HTML message'),
      '#description' => $this
        ->t("HTML version of the email body. This will be formatted using the text format selected at 'admin/config/system/mimemail'"),
    ],
    // This form element forces plaintext-only email when there is no HTML
    // content (that is, when the 'body' form element is empty).
    'plain' => [
      '#type' => 'hidden',
      '#states' => [
        'value' => [
          ':input[name="body"]' => [
            'value' => '',
          ],
        ],
      ],
    ],
    'plaintext' => [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Plain text message'),
      '#description' => $this
        ->t('Plain text version of the email body. HTML not allowed.'),
    ],
    'attachments' => [
      '#name' => 'files[attachment]',
      '#type' => 'file',
      '#title' => $this
        ->t('Choose a file to send as an email attachment'),
    ],
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Send message'),
  ];
  return $form;
}