【问题标题】:Unable to send email through Drupal 8无法通过 Drupal 8 发送电子邮件
【发布时间】:2018-08-01 22:12:35
【问题描述】:

我正在 Drupal 8 中开发一个自定义模块。我正在尝试发送有关表单提交的电子邮件。我在我的服务器上安装了邮件客户端,并且能够从服务器中的命令行发送电子邮件,但是当我尝试从 devel 发送电子邮件以测试我的网站时,它无法正常工作。它什么也没返回。我尝试使用 \Drupal\Core\Mail\Plugin\Mail\PhpMail();和 \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']);   

我尝试的另一种方法是

    $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); 

我没有收到电子邮件的两种方式。我不确定如何调试和解决这个问题。如果需要,请向我询问更多信息。

【问题讨论】:

标签: php drupal-8


【解决方案1】:

这是服务器设置问题。我们必须打开服务器上的“httpd_can_sendmail”配置。

setsebool -P httpd_can_sendmail 1

【讨论】:

    【解决方案2】:

    使用下面的代码在 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 2012-01-26
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多