欢迎各位兄弟 发布技术文章
这里的技术是共享的
function shicustom_node_insert($node){ //shicustom_node_insert 这是一个插入后的 hook
$language = language_default();
$test_address = '958187@qq.com';
$result = db_query("SELECT count(nid) as c FROM {node} where created>='".strtotime(date('Y-m-01'))."'");
$num = $result->fetch()->c;
$params['subject'] = '于'.date('Y-m-d H:i:s').'新增订单 '.$node->title.', 共计 '.$num.' 个订单';
$params['body'][] = '于'.date('Y-m-d H:i:s').'新增订单 '.$node->title.', 共计 '.$num.' 个订单';
drupal_mail('shicustom', 'notice', $test_address, $language, $params);
}
function shicustom_mail($key, &$message, $params) {
if ($key == 'notice') {
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
}
}
使用 drupal_mail 这个函数时,可能会使用 phpmailer 里面的phpmailer来发送邮件,
,而不是使用phpmailer 里面的smtp来发送邮件,
此时可能会出错,出错出在 sites\all\libraries\phpmailer\class.smtp.php 约 299 行,
$this->smtp_conn = stream_socket_client(
$host . ":" . $port,
$errno,
$errstr,
$timeout,
STREAM_CLIENT_CONNECT,
$socket_context
);
为了不执行到这里的代码,需要使用 phpmailer 里面的smtp来发送邮件,
参考了sites\all\modules\phpmailer\phpmailer.admin.inc 里面的代码,, 我修改我的drupal_mail函数如下
下面是例子1
funcction _generate_all_dhcp_clients
{
$language = language_default();
$params['subject'] = '于'.date('Y-m-d H:i:s').'新增订单title ';
$params['body'][] = '于'.date('Y-m-d H:i:s').'新增订单 body';
$to = 'cccc.Shi@aaaa-ict.com';
$message = drupal_mail('custom_twenty_two_jses', 'notice', $to, NULL, $params, NULL, FALSE);
module_load_include('inc', 'phpmailer', 'includes/phpmailer.drupal');
$result = phpmailer_send($message);
exit;
}
钩子函数不变 如下
function custom_twenty_two_jses_mail($key, &$message, $params)
{
if ($key == 'notice') {
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
//下面几行的目的是为了发送邮件是,使用 html 来发送,特别 Content-Type 这一行的目的就是为了使用 html 来发送
$message['headers'] = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
}
}
下面是例子2
//得到发件人
function _my_get_mail_from()
{
return 'NNNN@eee.com';
}
//得到收件人
function _my_get_mail_to()
{
// return 'Pingzhong.Shi@bbbb-admincom';
return 'aaaa.Shi@aaa.com,bbbb.Wan@Shi@aaa.com,ccc.Gao@aaa.com';
}
//查看dhcp 里面 昨天的数据 和今天的数据
function _my_chakan_chayi_dhcp($clients_all)
{
//取出昨天的数据
$old_clients_all = unserialize(file_get_contents(dirname(__FILE__) . '/clients_all.txt'));
$old_clients_all_no_expires= array();// $old_clients_all_no_expires,除了是没有LeaseExpires9999-12-31 是没有LeaseExpires
foreach($old_clients_all as $key=>$row){
if($row['LeaseExpires'] !='9999-12-31'){//因为租约每天(甚至每几小时,几分钟)都在变,所以移除它
unset($row['LeaseExpires']);
}
$old_clients_all_no_expires[$key] = $row;
}
$clients_all_no_expires= array();// // $clients_all_no_expires,除了是没有LeaseExpires9999-12-31 是没有LeaseExpires
foreach($clients_all as $key=>$row){
if($row['LeaseExpires'] !='9999-12-31'){//因为租约每天(甚至每几小时,几分钟)都在变,所以移除它
unset($row['LeaseExpires']);
}
$clients_all_no_expires[$key] = $row;
}
$in_olds_arr_no_expires =_my_get_diff_array_by_filter($old_clients_all_no_expires,$clients_all_no_expires);
$in_olds_arr = array(); // $in_olds_arr 是有LeaseExpires
foreach($in_olds_arr_no_expires as $key=>$value){
$in_olds_arr[$key] = $old_clients_all[$key];
}
$in_news_arr_no_expires =_my_get_diff_array_by_filter($clients_all_no_expires,$old_clients_all_no_expires);
$in_news_arr = array(); // $in_news_arr 是有LeaseExpires
foreach($in_news_arr_no_expires as $key=>$value){
$in_news_arr[$key] = $clients_all[$key];
}
$str = '<html><body><h3>昨天消失(及已更改)的</h3>';
$str .= _my_get_table_str_by_dhcp_value($in_olds_arr);
$str .= '<h3>今天新增(及已更改)的</h3>';
$str .= _my_get_table_str_by_dhcp_value($in_news_arr);
$str .= '</body></html>';
//把今天现在的数据生成
file_put_contents(dirname(__FILE__) . '/clients_all.txt', serialize($clients_all));//序列化数组保存一下
$language = language_default();
$params['subject'] = 'dhcp昨天与今天的差异数据 ';
$params['body'][] = htmlspecialchars($str);
$to = _my_get_mail_to();
$from = _my_get_mail_from();
$message = drupal_mail('custom_twenty_two_jses', 'notice', $to, NULL, $params, $from, FALSE);
module_load_include('inc', 'phpmailer', 'includes/phpmailer.drupal');
$result = phpmailer_send($message);
}
function custom_twenty_two_jses_mail($key, &$message, $params) {
if ($key == 'notice') {
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
$message['headers'] = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
}
}
在drupal的模块开发中实现发邮件的功能非常简单,只用到一个钩子函数 hook_mail 和一个接口函数drupal_mail.
使用方法如下:
<?php
function example_mail($key, &$message, $params) {
$language = $message['language'];
$variables = user_mail_tokens($params['account'], $language);
switch($key) {
case 'notice':
$message['subject'] = t('Notification from !site', $variables, $language->language);
$message['body'][] = t("Dear !username\n\nThere is new content available on the site.", $variables, $language->language);
break;
}
}
function example_notify($accounts) {
foreach ($accounts as $account) {
$params['account'] = $account;
// example_mail() will be called based on the first drupal_mail() parameter.
drupal_mail('example', 'notice', $account->mail, user_preferred_language($account), $params);
}
}
?>
<?php
example_mail($key, &$message, $params)
?>
$key 发邮件时用到的唯一标识.
$message 需要传过来的一个数组信息,包括:
'id': 验证将要发送的email ID,参照 drupal_mail() 得到更多信息。
'to': 接收邮件的email地址。
'subject': 邮件标题,不要包括换行符等特殊字符。
properly. drupal_mail() sets this to an empty string when the hook is invoked.
'body': 邮件正文。
'from': 邮件发送方
'headers: 包括邮件头部信息,例如 From, Sender, MIME-Version, Content-Type, 等等。
$params 在被drupal_mail调用时设置的一些参数.
-----------------------------------------------------------------------------------------------------------------------------------------
Drupal的函数原型为:
<?php
drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE)
?>
$params:一个数组,包括主要信息,比如邮件标题,邮件内容等<br />
<?php
$message = array(
'to' => 'example@mailinator.com',
'subject' => t('Example subject'),
'body' => t('Example body'),
'headers' => array('From' => 'example@mailinator.com'),
);
drupal_mail_send($message);
?>