我已經在我的drupal站點的子文件夾中開發了一個使用核心php的獨立功能(假設類似mysite.com/myfolder/myfunc.php
)。如何從外部php文件(子文件夾)使用drupal郵件功能?
現在我想發送電子郵件,就像drupal網站發送郵件一樣。
由於這不是自定義模塊,我不能使用hook_mail
。或者有沒有可能實現這一目標?
如何從核心php(網站的子文件夾)使用drupal郵件功能?
我已經在我的drupal站點的子文件夾中開發了一個使用核心php的獨立功能(假設類似mysite.com/myfolder/myfunc.php
)。如何從外部php文件(子文件夾)使用drupal郵件功能?
現在我想發送電子郵件,就像drupal網站發送郵件一樣。
由於這不是自定義模塊,我不能使用hook_mail
。或者有沒有可能實現這一目標?
如何從核心php(網站的子文件夾)使用drupal郵件功能?
最好的方法是創建一個模塊,但如果需要的話,你可以使用
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
/**
Write your code here
use PHP core, drupal core and contrib functions
**/
與@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」)。
對於我已經把代碼放在這裏的參考。它可能是完整的代碼,但這可能有助於某人。
//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 = '[email protected]';
$from = '[email protected]';
//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 = '[email protected]';
$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;
}
好的。發送電子郵件我需要使用'hook_mail'然後'drupal_mail'函數。但我的不是一個模塊。這是全部用核心php編寫的。在這種情況下我能做些什麼? – siddiq
對不起。我們可以使用任何名稱作爲模塊名稱。我用作'測試'。我提到http://dropbucket.org/node/308,它工作正常,除非它必須作爲html發送。我可以稍後再檢查。但使用drupal_mail只是工作正常。 :) – siddiq