View source
<?php
namespace Drupal\mandrill\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\mandrill\Plugin\Mail\MandrillMail;
use Drupal\mandrill\Plugin\Mail\MandrillTestMail;
class MandrillAdminTestForm extends ConfirmFormBase {
function getFormID() {
return 'mandrill_test_email';
}
public function getQuestion() {
return $this
->t('Send Test Email');
}
public function getDescription() {
return $this
->t('This action will send a test email through Mandrill.');
}
public function getCancelUrl() {
return new Url('mandrill.test');
}
public function getConfirmText() {
return $this
->t('Send test email');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$click_tracking_url = Url::fromUri('http://www.drupal.org/project/mandrill');
$mandrill_test_mail = \Drupal::config('mailsystem.settings')
->get('defaults')['sender'] == 'mandrill_test_mail';
$form['mandrill_test_address'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Email address to send a test email to'),
'#default_value' => \Drupal::config('system.site')
->get('mail'),
'#description' => $this
->t('Type in an address to have a test email sent there.'),
'#required' => TRUE,
);
if (!$mandrill_test_mail) {
$form['mandrill_test_bcc_address'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Email address to BCC on this test email'),
'#description' => $this
->t('Type in an address to have a test email sent there.'),
);
}
$form['mandrill_test_body'] = array(
'#type' => 'textarea',
'#title' => $this
->t('Test body contents'),
'#default_value' => $this
->t('If you receive this message it means your site is capable of using Mandrill to send email. This url is here to test click tracking: %link', array(
'%link' => Link::fromTextAndUrl($this
->t('link'), $click_tracking_url)
->toString(),
)),
);
if (!$mandrill_test_mail) {
$form['include_attachment'] = array(
'#title' => $this
->t('Include attachment'),
'#type' => 'checkbox',
'#description' => $this
->t('If checked, the Drupal icon will be included as an attachment with the test email.'),
'#default_value' => TRUE,
);
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$message = array(
'id' => 'mandrill_test_email',
'module' => 'mandrill',
'to' => $form_state
->getValue('mandrill_test_address'),
'body' => $form_state
->getValue('mandrill_test_body'),
'subject' => $this
->t('Drupal Mandrill test email'),
);
$bcc_email = $form_state
->getValue('mandrill_test_bcc_address');
if (!empty($bcc_email)) {
$message['bcc_email'] = $bcc_email;
}
if ($form_state
->getValue('include_attachment')) {
$message['attachments'][] = \Drupal::service('file_system')
->realpath('core/themes/bartik/logo.svg');
$message['body'] .= ' ' . $this
->t('The Drupal icon is included as an attachment to test the attachment functionality.');
}
$sender = \Drupal::config('mailsystem.settings')
->get('defaults')['sender'];
if ($sender == 'mandrill_mail') {
$mailer = new MandrillMail();
}
elseif ($sender == 'mandrill_test_mail') {
$mailer = new MandrillTestMail();
}
if (isset($mailer) && $mailer
->mail($message)) {
$this
->messenger()
->addStatus($this
->t('Test email has been sent.'));
}
}
}