drupal8发送mail方法
注册SMTP server
|
|
配置SMTP server
- 安装SMTP Authentication Support模块
- 配置SMTP
admin/config/system/smtp
drupal实现
EmailExampleController.php12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849public 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 = $this->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 = $this->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');}}
email_example.module
修改drupal8 bug
参考资料
MailManager::mail
Managing Mail Handling for Development or Testing
How to Configure SMTP in Drupal 8