haraka / Haraka

A fast, highly extensible, and event driven SMTP server
https://haraka.github.io
MIT License
5.02k stars 662 forks source link

Is there any reason why outbound.send_email does not use the queue/smtp_forward? #1289

Closed ricardopolo closed 8 years ago

ricardopolo commented 8 years ago

I have a plugin that send an email. I also configure the plugin queue/smtp_forward, and it works.

But when the email is send throught

outbound.send_email(mail_from, transaction.rcpt_to,"message")

Haraka send the emails by itself and dont use the forward smtp.

Any idea?

Thank you!

ricardopolo commented 8 years ago

@msimerson @smfreegard any idea please?

In case this method runs after the QUEUE and mandatory sends by itself, do you know if there is other method that puts a new message in the queue instead of sending it diirectly?

Thanks!

smfreegard commented 8 years ago

That is because queue/smtp_forward is only used when a message is injected via SMTP, outbound.send_email uses Haraka's internal queuing as you've already found.

If you want to send a message, then you'll need to use something that speaks SMTP like nodemailer.

ricardopolo commented 8 years ago

@smfreegard this is my use case:

I use haraka to get some headers from the original message (as example the subject) and send a brand new email with a new connect but keeping the subject.

So in the data_post event I read the headers I need, and create a new message using mailcomposer. Then I send the new message using outbound.send_email.

The code looks like this:

      var headers = generate_array_of_new_headers(transaction);
      var body = genereate_new_bodytext(transaction.body.bodytext);
      mailcomposer({ headers: headers , html: body }).build(function(err, message){
          outbound.send_email(mail_from, transaction.rcpt_to, message.toString());
          //Discard the original message using the Discard plugin
          connection.notes.discard = 1;
          return next();
      })

It works as expected. I also needs that the new email is no being send directly but uses an Smtp Forwards, because if not, Haraka would be detected as a spam server.

So... any recommendation where can I construct this new message and still use the smtp forward feature? In this case us do you still recommend mailnoder? (Like running mailnoder insinde haraka?)

Dexus commented 8 years ago

@ricardopolo as @smfreegard you write, the forwarding plugin is only listen on new smtp connections not on "server-side-code" sendings. There is no other way for now.

smfreegard commented 8 years ago

Ok - actually I can see an easy way to achieve this, and you won't need the smtp_forward plugin at all. Use the following in place of queue/smtp_forward and modify it with the necessary host/ip in mx.exchange:

exports.hook_queue = function (next, connection) {
    var transaction = connection.transaction;

    outbound.send_email(connection.transaction, function(retval, msg) {
        switch(retval) {
            case constants.ok:
                return next(OK, msg);
                break;
            case constants.deny:
                return next(DENY, msg);
                break;
            default:
                return next(DENYSOFT, msg);
        }
    });
}

exports.hook_get_mx = function (next, hmail, domain) {
    var mx = {
        priority: 0,
        exchange: 'ip.ip.ip.ip',
        //auth_user: 'user',
        //auth_pass: 'pass',
        port: 25,
    }
    return next(OK, mx);
}

This will force all traffic via the outbound queuing mechanism and force the routing to the host that you specify e.g. a smart host configuration.

ricardopolo commented 8 years ago

@smfreegard it works! Thank you so much :+1:

smfreegard commented 8 years ago

No problem - you're welcome.