如何使用外部php文件(子文件夹)的drupal邮件功能?

时间:2017-10-09 06:03:19

标签: php email drupal drupal-7

我在drupal网站的子文件夹中使用核心php开发了一个单独的功能(假设像mysite.com/myfolder/myfunc.php)。

现在我想发送电子邮件,就像drupal网站发送它一样。

由于这不是自定义模块,因此我无法使用hook_mail。或者有可能实现这个目标吗?

如何从核心php(网站的子文件夹)使用drupal邮件功能?

3 个答案:

答案 0 :(得分:1)

最好的方法是创建一个模块,但如果需要,你可以使用

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
/**
Write your code here
use PHP core, drupal core and contrib functions
**/

答案 1 :(得分:0)

同意@AZinkey。一种方法是包括drupal的bootstrap并且可以使用drupal的所有功能,就像他解释的那样。但更好的方法是从Drupal定义您的页面。查看drupal的hook_menu函数:

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7.x

在那里解释:

function mymodule_menu() {
  $items['abc/def'] = array(
    'page callback' => 'mymodule_abc_view',
  );
  return $items;
}
function mymodule_abc_view($ghi = 0, $jkl = '') {

  // ...
}

..您可以轻松定义自定义页面。你需要的只是页面路径(即“abc / def”)和提供页面内容的功能(“mymodule_abc_view”)。

答案 2 :(得分:0)

作为参考,我已将代码放在此处。它可能是完整的代码,但这可能对某人有所帮助。

//These lines are to use drupal functions
define('DRUPAL_ROOT', 'Your/drupal/path');
require_once '../../../includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

//Get the mail content
$email_content = get_mail_content();
$params = array('body' => $email_content);
$key = 'test_email'; //this is the key
$to = 'siddiqxxxxx@gmail.com';
$from = 'support@xxxxx.com';

//use the hook_mail name here. in my case it is 'test'.
$mail = drupal_mail('test', $key, $to, language_default(), $params, $from);
echo "Mail sent";

//using hook_mail. we can use whatever the name we want. Parameters are just fine.
function test_mail($key, &$message, $params) {
  $language = $message['language'];
  switch ($key) {
//switching on $key lets you create variations of the email based on the $key parameter
    case 'test_email': //this is the key
      $message['subject'] = t('Test Email');
//the email body is here, inside the $message array
      $message['body'][] = $params['body'];
      break;
  }
}


function get_mail_content() {

  $email_to = 'siddiqxxxxx@gmail.com';
  $pos = strpos($email_to, '@');
  $user_name = substr($email_to, 0, $pos);
  $body = '';
  $body .= 'Hi ' . $user_name . '<br>';
  $body .= 'Please find my test email. <br>';
  $body .= 'This is the email body' . '<br>';
  $body .= 'Thanks<br>';
  $body .= 'TestTeam';
  return $body;
}
相关问题