简体   繁体   English

无法通过Drupal 8发送电子邮件

[英]Unable to send email through Drupal 8

I am developing a custom module in Drupal 8. I am trying to send an email on form submission. 我正在Drupal 8中开发一个自定义模块。我正在尝试发送有关表单提交的电子邮件。 I installed mail client on my server and able to send email from command line in the server but when I am trying to send email from devel to test my site its not working.Its returning nothing. 我在服务器上安装了邮件客户端程序,并且能够从服务器中的命令行发送电子邮件,但是当我尝试从devel发送电子邮件以测试我的网站不起作用时,它什么也没有返回。 I tried with \\Drupal\\Core\\Mail\\Plugin\\Mail\\PhpMail(); 我尝试使用\\ Drupal \\ Core \\ Mail \\ Plugin \\ Mail \\ PhpMail(); and \\Drupal::service('plugin.manager.mail'); 和\\ Drupal :: service('plugin.manager.mail');

    $mailManager = \Drupal::service('plugin.manager.mail');
    $langcode = \Drupal::currentUser()->getPreferredLangcode();
    $params['context']['subject'] = 'Subject';
    $params['context']['message'] = 'body';
    $to = 'myorgemail@company.test';
    $result['result'] = $mailManager->mail('system', 'mail', $to, $langcode, $params);
    dpm($result['result']);   

The other way which I tried is 我尝试的另一种方法是

    $send_mail = new \Drupal\Core\Mail\Plugin\Mail\PhpMail(); 
    $from = 'from_email_given';
    $message['headers'] = array(
    'content-type' => 'text/html',
    'MIME-Version' => '1.0',
    'reply-to' => $from,
    'from' => 'sender name <'.$from.'>');
    $message['to'] = 'to_email_given';
    $message['subject'] = 'Subject Goes here !!!!!';
    $message['body'] = 'Hello'; 
    $send_mail->mail($message); 

both the ways I am not receiving the email. 两种方式我都没有收到电子邮件。 I am not sure how to debug and solve this. 我不确定如何调试和解决这个问题。 Please ask me more information if needed. 如果需要,请询问我更多信息。

It was a server setting issue. 这是服务器设置问题。 We had to turn on the "httpd_can_sendmail" configuration on server. 我们必须打开服务器上的“ httpd_can_sendmail”配置。

setsebool -P httpd_can_sendmail 1

Use below code to sending mails in Drupal 8 使用以下代码在Drupal 8中发送邮件

$params = [];
$params['message'] = 'Mail Body';
$params['subject'] = 'Sample Subject';
$to = 'example@gmail.com';

//Calling drupal Mail service
$mailManager = \Drupal::service('plugin.manager.mail');
//Module Name
$module = 'custom_mail_sending';
//Static Mail Key
$key = 'custom_mail_sending_key';
//Email Language
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = true;

$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
if ($result['result'] != true) {     
  $message = t('There was a problem sending your email notification to @email.', array('@email' => $to));
  drupal_set_message($message, 'error');
  \Drupal::logger('custom_mail_sending_log')->notice($message);
} else {
  $message = t('An email notification has been sent to @email ', array('@email' => $to));
  drupal_set_message($message,'status');
  \Drupal::logger('custom_mail_sending_log')->error($message);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM