/** * {@inheritdoc} */ public function mail($module, $key, $to, $langcode, $params = array(), $reply = NULL, $send = TRUE) { $message = $this->mailManager->mail($module, $key, $to, $langcode, $params, $reply, $send); $instance = $this->mailManager->getInstance(['module' => $module, 'key' => $key]); $this->dataCollector->addMessage($message, $instance); return $message; }
/** * Tests sending a mail to two recipients. * * @covers ::execute */ public function testSendMailToTwoRecipients() { $to = ['*****@*****.**', '*****@*****.**']; $this->action->setContextValue('to', $to)->setContextValue('subject', 'subject')->setContextValue('message', 'hello'); $params = ['subject' => 'subject', 'message' => 'hello']; $this->mailManager->mail('rules', 'rules_action_mail_' . $this->action->getPluginId(), implode(', ', $to), LanguageInterface::LANGCODE_SITE_DEFAULT, $params, NULL)->willReturn(['result' => TRUE])->shouldBeCalledTimes(1); $this->logger->notice(Argument::any(), Argument::any())->shouldBeCalledTimes(1); $this->action->execute(); }
/** * Tests sending a mail to two recipients. * * @covers ::execute */ public function testSendMailToTwoRecipients() { $to = ['*****@*****.**', '*****@*****.**']; $this->action->setContextValue('to', $to)->setContextValue('subject', 'subject')->setContextValue('message', 'hello'); $params = ['subject' => 'subject', 'message' => 'hello']; $this->mailManager->mail('rules', 'rules_action_mail_' . $this->action->getPluginId(), implode(', ', $to), LanguageInterface::LANGCODE_SITE_DEFAULT, $params, NULL)->willReturn(['result' => TRUE])->shouldBeCalledTimes(1); $this->logger->log(LogLevel::NOTICE, SafeMarkup::format('Successfully sent email to %to', ['%to' => implode(', ', $to)]))->shouldBeCalledTimes(1); $this->action->execute(); }
/** * ya se definen los tipos en los valores, el form es de tipo array * el form state es de tipo interfaz */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_values = $form_state->getValues(); $to = $form_values['email']; $from = \Drupal::config('system.site')->get('mail'); $params = $form_values; $language_code = \Drupal::languageManager()->getDefaultLanguage()->getId(); $result = $this->mailManager->mail('email_example', 'contact_message', $to, $language_code, $params, $from); if ($result['result'] == TRUE) { drupal_set_message(t('El correo ha sido enviado.')); } else { drupal_set_message(t('Hubo un problema al mandar el correo.'), 'error'); } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_values = $form_state->getValues(); $module = 'customer_contact'; $key = 'contact_message'; // Specify 'to' and 'from' addresses. $to = "*****@*****.**"; $from = \Drupal::config('system.site')->get('mail'); // Output $output = ''; $output .= 'First name: ' . $form_values['first_name'] . '\\n'; $output .= 'Last name: ' . $form_values['last_name'] . '\\n'; $output .= 'Company: ' . $form_values['company'] . '\\n'; $output .= 'Phone number: ' . $form_values['phone_number'] . '\\n'; $output .= 'Website: ' . $form_values['website'] . '\\n'; $output .= 'Know about: ' . $form_values['know_about'] . '\\n'; $output .= 'Project start: ' . $form_values['project_start'] . '\\n'; $output .= 'Price range: ' . $form_values['price_range'] . '\\n'; $output .= 'Description: ' . $form_values['description'] . '\\n'; $params = array('message' => $output); $language_code = \Drupal::languageManager()->getDefaultLanguage()->getId(); $send_now = TRUE; // Send the mail, and check for success. Note that this does not guarantee // message delivery; only that there were no PHP-related issues encountered // while sending. $result = $this->mailManager->mail($module, $key, $to, $language_code, $params, $from, $send_now); if ($result['result'] == TRUE) { drupal_set_message(t('An email has been sent to Create Inside. Thank your for your time.')); } else { drupal_set_message(t('There was a problem sending your message.'), 'error'); } }
/** * {@inheritdoc} */ public function sendMailMessages(MessageInterface $message, AccountInterface $sender) { // Clone the sender, as we make changes to mail and name properties. $sender_cloned = clone $this->userStorage->load($sender->id()); $params = array(); $current_langcode = $this->languageManager->getCurrentLanguage()->getId(); $recipient_langcode = $this->languageManager->getDefaultLanguage()->getId(); $contact_form = $message->getContactForm(); if ($sender_cloned->isAnonymous()) { // At this point, $sender contains an anonymous user, so we need to take // over the submitted form values. $sender_cloned->name = $message->getSenderName(); $sender_cloned->mail = $message->getSenderMail(); // For the email message, clarify that the sender name is not verified; it // could potentially clash with a username on this site. $sender_cloned->name = $this->t('@name (not verified)', array('@name' => $message->getSenderName())); } // Build email parameters. $params['contact_message'] = $message; $params['sender'] = $sender_cloned; if (!$message->isPersonal()) { // Send to the form recipient(s), using the site's default language. $params['contact_form'] = $contact_form; $to = implode(', ', $contact_form->getRecipients()); } elseif ($recipient = $message->getPersonalRecipient()) { // Send to the user in the user's preferred language. $to = $recipient->getEmail(); $recipient_langcode = $recipient->getPreferredLangcode(); $params['recipient'] = $recipient; } else { throw new MailHandlerException('Unable to determine message recipient'); } // Send email to the recipient(s). $key_prefix = $message->isPersonal() ? 'user' : 'page'; $this->mailManager->mail('contact', $key_prefix . '_mail', $to, $recipient_langcode, $params, $sender_cloned->getEmail()); // If requested, send a copy to the user, using the current language. if ($message->copySender()) { $this->mailManager->mail('contact', $key_prefix . '_copy', $sender_cloned->getEmail(), $current_langcode, $params, $sender_cloned->getEmail()); } // If configured, send an auto-reply, using the current language. if (!$message->isPersonal() && $contact_form->getReply()) { // User contact forms do not support an auto-reply message, so this // message always originates from the site. if (!$sender_cloned->getEmail()) { $this->logger->error('Error sending auto-reply, missing sender e-mail address in %contact_form', ['%contact_form' => $contact_form->label()]); } else { $this->mailManager->mail('contact', 'page_autoreply', $sender_cloned->getEmail(), $current_langcode, $params); } } if (!$message->isPersonal()) { $this->logger->notice('%sender-name (@sender-from) sent an email regarding %contact_form.', array('%sender-name' => $sender_cloned->getUsername(), '@sender-from' => $sender_cloned->getEmail(), '%contact_form' => $contact_form->label())); } else { $this->logger->notice('%sender-name (@sender-from) sent %recipient-name an email.', array('%sender-name' => $sender_cloned->getUsername(), '@sender-from' => $sender_cloned->getEmail(), '%recipient-name' => $message->getPersonalRecipient()->getUsername())); } }
/** * Send a system email. * * @param string[] $to * Email addresses of the recipients. * @param string $subject * Subject of the email. * @param string $message * Email message text. * @param string|null $reply * (optional) Reply to email address. * @param \Drupal\Core\Language\LanguageInterface|null $language * (optional) Language code. */ protected function doExecute($to, $subject, $message, $reply = NULL, LanguageInterface $language = NULL) { $langcode = isset($language) ? $language->getId() : LanguageInterface::LANGCODE_SITE_DEFAULT; $params = ['subject' => $subject, 'message' => $message]; // Set a unique key for this mail. $key = 'rules_action_mail_' . $this->getPluginId(); $recipients = implode(', ', $to); $message = $this->mailManager->mail('rules', $key, $recipients, $langcode, $params, $reply); if ($message['result']) { $this->logger->notice('Successfully sent email to %recipient', ['%recipient' => $recipients]); } }
/** * Tests the sendMailMessages method. * * @dataProvider getSendMailMessages * * @covers ::sendMailMessages */ public function testSendMailMessages(MessageInterface $message, AccountInterface $sender, $results) { $this->logger->expects($this->once())->method('notice'); $this->mailManager->expects($this->any())->method('mail')->willReturnCallback(function ($module, $key, $to, $langcode, $params, $from) use(&$results) { $result = array_shift($results); $this->assertEquals($module, $result['module']); $this->assertEquals($key, $result['key']); $this->assertEquals($to, $result['to']); $this->assertEquals($langcode, $result['langcode']); $this->assertArrayEquals($params, $result['params']); $this->assertEquals($from, $result['from']); }); $this->userStorage->expects($this->any())->method('load')->willReturn(clone $sender); $this->contactMailHandler->sendMailMessages($message, $sender); }
/** * {@inheritdoc} */ public function execute() { $to = $this->getContextValue('to'); $reply = $this->getContextValue('reply'); $language = $this->getContextValue('language'); $langcode = isset($language) ? $language->getId() : LanguageInterface::LANGCODE_SITE_DEFAULT; $params = ['subject' => $this->getContextValue('subject'), 'message' => $this->getContextValue('message')]; // Set a unique key for this mail. $key = 'rules_action_mail_' . $this->getPluginId(); $recipients = implode(', ', $to); $message = $this->mailManager->mail('rules', $key, $recipients, $langcode, $params, $reply); if ($message['result']) { $this->logger->log(LogLevel::NOTICE, $this->t('Successfully sent email to %recipient', ['%recipient' => $recipients])); } }
/** * {@inheritdoc} */ public function sendCombinedConfirmation(SubscriberInterface $subscriber) { $params['from'] = $this->getFrom(); $params['context']['simplenews_subscriber'] = $subscriber; // Send multiple if there is more than one change for this subscriber // single otherwise. $use_combined = $this->config->get('subscription.use_combined'); $changes = $subscriber->getChanges(); if (count($changes) > 1 && $use_combined != 'never' || $use_combined == 'always') { $key = 'subscribe_combined'; $this->mailManager->mail('simplenews', $key, $subscriber->getMail(), $subscriber->getLangcode(), $params, $params['from']['address']); } else { foreach ($changes as $newsletter_id => $key) { $params['context']['newsletter'] = simplenews_newsletter_load($newsletter_id); $this->mailManager->mail('simplenews', $key, $subscriber->getMail(), $subscriber->getLangcode(), $params, $params['from']['address']); } } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_values = $form_state->getValues(); // All system mails need to specify the module and template key (mirrored // from hook_mail()) that the message they want to send comes from. $module = 'email_example'; $key = 'contact_message'; // Specify 'to' and 'from' addresses. $to = $form_values['email']; $from = \Drupal::config('system.site')->get('mail'); // "params" loads in additional context for email content completion in // hook_mail(). In this case, we want to pass in the values the user entered // into the form, which include the message body in $form_values['message']. $params = $form_values; // The language of the e-mail. This will one of three values: // - $account->getPreferredLangcode(): Used for sending mail to a particular // website user, so that the mail appears in their preferred language. // - \Drupal::currentUser()->getPreferredLangcode(): Used when sending a // mail back to the user currently viewing the site. This will send it in // the language they're currently using. // - \Drupal::languageManager()->getDefaultLanguage()->getId: Used when // sending mail to a pre-existing, 'neutral' address, such as the system // e-mail address, or when you're unsure of the language preferences of // the intended recipient. // // Since in our case, we are sending a message to a random e-mail address // that is not necessarily tied to a user account, we will use the site's // default language. $language_code = \Drupal::languageManager()->getDefaultLanguage()->getId(); // Whether or not to automatically send the mail when we call mail() on the // mail manager. This defaults to TRUE, and is normally what you want unless // you need to do additional processing before the mail manager sends the // message. $send_now = TRUE; // Send the mail, and check for success. Note that this does not guarantee // message delivery; only that there were no PHP-related issues encountered // while sending. $result = $this->mailManager->mail($module, $key, $to, $language_code, $params, $from, $send_now); if ($result['result'] == TRUE) { drupal_set_message(t('Your message has been sent.')); } else { drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error'); } }
/** * {@inheritdoc} */ public function execute($entity = NULL) { if (empty($this->configuration['node'])) { $this->configuration['node'] = $entity; } $recipient = PlainTextOutput::renderFromHtml($this->token->replace($this->configuration['recipient'], $this->configuration)); // If the recipient is a registered user with a language preference, use // the recipient's preferred language. Otherwise, use the system default // language. $recipient_accounts = $this->storage->loadByProperties(array('mail' => $recipient)); $recipient_account = reset($recipient_accounts); if ($recipient_account) { $langcode = $recipient_account->getPreferredLangcode(); } else { $langcode = $this->languageManager->getDefaultLanguage()->getId(); } $params = array('context' => $this->configuration); if ($this->mailManager->mail('system', 'action_send_email', $recipient, $langcode, $params)) { $this->logger->notice('Sent email to %recipient', array('%recipient' => $recipient)); } else { $this->logger->error('Unable to send email to %recipient', array('%recipient' => $recipient)); } }