Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created May 6, 2009 08:38
Show Gist options
  • Save miyagawa/107437 to your computer and use it in GitHub Desktop.
Save miyagawa/107437 to your computer and use it in GitHub Desktop.
smtp2web plugin for qpsmtpd
#!perl
=head1 NAME
queue/http-post
=head1 DESCRIPTION
This plugin posts email body to an URI as HTTP POST.
=head1 CONFIG
It takes one required parameter, the location of the URI to post messages to.
=head1 HTTP POST SPEC
This plugin clones smtp2web specification, so it sends the entire message (including headers) in a POST request, with Content-Type: message/rfc822, along with the query parameters I<from> and I<to> set to the envelope sender and recipient.
See L<http://www.smtp2web.com/about> for details. Note that this plugin uses I<message/rfc822> as a Content-Type instead of I<multipart/rfc-822> that smtp2web is using, which I assume is not correct.
=cut
# NOTE: rewrite with Danga::Socket for async server
use LWP::UserAgent;
use HTTP::Request;
use URI;
sub register {
my ($self, $qp, @args) = @_;
if (@args > 0) {
($self->{_post_uri}) = ($args[0] =~ m!(\S+)!);
}
unless ($self->{_post_uri}) {
$self->log(LOGWARN, "WARNING: URI to post is required");
return 0;
}
}
sub hook_queue {
my ($self, $transaction) = @_;
my $recip = ($transaction->recipients)[0];
$recip = $recip ? $recip->address : "";
my $uri = URI->new($self->{_post_uri});
$uri->query_form(from => ($transaction->sender->address || ""), to => $recip);
my $body = $transaction->header->as_string . $transaction->body_as_string;
my $agent = LWP::UserAgent->new(agent => "qpsmtpd/$Qpsmtpd::VERSION", timeout => 15);
my $req = HTTP::Request->new(POST => $uri);
$req->content_type("message/rfc822");
$req->content($body);
$req->content_length(length $body);
my $res = $agent->request($req);
if ($res->is_success) {
return OK, "message POSTed successfully";
} else {
return DECLINED, "POSTing message failed: " . $res->status_line;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment