Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Drupal\reroute_email\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Implements a form to test Reroute Email.
*/
class TestEmailForm extends FormBase {
/**
* The mail manager.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* The language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'reroute_email_test_email_form';
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.mail'),
$container->get('language_manager')
);
}
/**
* Constructs a new object.
*
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* Mail manager service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager) {
$this->mailManager = $mail_manager;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
return [
'addresses' => [
'#type' => 'fieldset',
'#description' => $this->t('A list of addresses separated by a comma could be submitted.<br/>Email addresses are not validated: any valid or invalid email address format could be submitted.'),
'to' => [
'#type' => 'textfield',
'#title' => $this->t('To'),
'#required' => TRUE,
],
'cc' => [
'#type' => 'textfield',
'#title' => $this->t('Cc'),
],
'bcc' => [
'#type' => 'textfield',
'#title' => $this->t('Bcc'),
],
],
'subject' => [
'#type' => 'textfield',
'#title' => $this->t('Subject'),
'#default_value' => $this->t('Reroute Email Test'),
],
'body' => [
'#type' => 'textarea',
'#title' => $this->t('Body'),
'#default_value' => $this->t('Reroute Email Body'),
],
'submit' => [
'#type' => 'submit',
'#value' => $this->t('Send email'),
],
];
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$to = $form_state->getValue(['to']);
$param_keys = ['cc', 'bcc', 'subject', 'body'];
$params = array_intersect_key($form_state->getValues(), array_flip($param_keys));
$langcode = $this->languageManager->getDefaultLanguage();
// Send email with drupal_mail.
$message = $this->mailManager->mail('reroute_email', 'test_email_form', $to, $langcode, $params);
if (!empty($message['result'])) {
drupal_set_message($this->t('Test email submitted for delivery from test form.'));
}
}
}