如何使用Haraka图书馆

时间:2010-07-28 11:40:20

标签: orm

有人可以使用haraka librairy帮我一些例子吗?

1 个答案:

答案 0 :(得分:0)

我们使用Haraka作为我们的应用程序邮件服务器。我们编写了一个简单的插件,它接收任何传入的邮件并将其发布到我们的应用程序Web服务器。

这是插件脚本。只需将其保存到文件中,进行必要的名称更改,并添加Harakas config / plugins文件的路径。

var
    fs = require('fs'),
    query_string = require('querystring'),
    logger = require('./logger'),
    DROP_DIRECTORY_PATH = '/path/haraka/drop/',
    RETRY_DIRECTORY_PATH = '/path/haraka/retry/',
    HOST_NAME = 'this_haraka_servers_name';

exports.hook_queue = function (next, connection, params) {
    'use strict';

    function haraka_log(function_name_in, section_in, text_in) {
        var log_text = '[HttpMail]';
        log_text += ' [' + function_name_in + ']';
        log_text += ' [' + section_in + ']';
        log_text += ' ' + text_in;
        logger.lognotice(log_text);
    }//function haraka_log

    function move_file(filename_in) {
        fs.rename(DROP_DIRECTORY_PATH + filename_in, RETRY_DIRECTORY_PATH + filename_in, function (err) {
            if (err) {
                //throw err;
                haraka_log('move_file', 'fs.rename ... failed', filename_in + '\n' + JSON.stringify(err));
            } else {
                haraka_log('move_file', 'fs.rename ... success', filename_in);
            }
        });
    }//function move_file

    function delete_file(filename_in) {
        fs.unlink(DROP_DIRECTORY_PATH + filename_in, function (err) {
            if (err) {
                //throw err;
                haraka_log('delete_file', 'fs.unlink ... failed', filename_in + '\n' + JSON.stringify(err));
            } else {
                haraka_log('delete_file', 'fs.unlink ... success', filename_in);
            }
        });
    }//function delete_file

    function http_post_file(filename_in) {
        var
            http = require('http'),
            post_options = {
                host: 'my.server.com',
                port: 80,
                path: '/http_mail/' + HOST_NAME + '?' + query_string.stringify({FileName: filename_in}),
                method: 'POST',
                headers: {'Content-Type': 'text/plain'}
            },
            post_request,
            read_stream;

        haraka_log('http_post_file', 'Before http.request', filename_in);

        post_request = http.request(post_options, function (post_response) {
            haraka_log('http_post_file', 'post_response', ' post_response.statusCode = ' + post_response.statusCode + ' : ' + filename_in);
            if (post_response.statusCode === 200) {
                delete_file(filename_in);
            } else {
                move_file(filename_in);//Posted later by retry script;
            }

            post_response.resume();
        });//post_request = http.request(post_options, function(post_response) {

        post_request.on('error', function (err) {
            haraka_log('http_post_file post_request.on(\'error\' ...)', err.message, filename_in);
            move_file(filename_in);
        });//post_request.on('error', function(err) {

        read_stream = fs.createReadStream(DROP_DIRECTORY_PATH + filename_in);
        read_stream.pipe(post_request);

    }//function http_post_file

    var
        x_sender = connection.transaction.mail_from,
        x_receiver_list = connection.transaction.rcpt_to,
        filename = x_sender + '_' + new Date().toISOString() + '_' + connection.uuid,
        writeStream;

    filename = filename.replace(/\//g, '');
    connection.transaction.add_header('x-sender', x_sender.toString());

    x_receiver_list.forEach(function (value) {
        connection.transaction.add_header('x-receiver', value.toString());
    });


    haraka_log('main', 'filename', filename);

    writeStream = fs.createWriteStream(DROP_DIRECTORY_PATH + filename);
    //connection.transaction.message_stream.pipe(writeStream, {dot_stuffing: true, ending_dot: true});
    connection.transaction.message_stream.pipe(writeStream, {dot_stuffing: true});

    writeStream.on("close", function () {
        haraka_log('main writeStream.on("close"...', 'File Saved!', filename);

        http_post_file(filename);
        next(OK);
    });//writeStream.on("close", function()

};//exports.hook_queue = function(next, connection, params) {