[jdev] jabberSMTP

Bart Smit Bart.Smit at nexusmgmt.com
Thu Dec 15 05:22:38 CST 2005

Assuming that on the jabber server your MTA is sendmail you can do this:

Edit your sendmail.cf to listen for external connections:
dnl #  DAEMON_OPTIONS(`Port=smtp,Addr=, Name=MTA')dnl

Set a catch all for the server or domain in /etc/mail/virtusertable:
@your.jabber.org		mailerscript

Add an alias for the script in /etc/aliases (don't forget to run
newaliases as root):
mailerscript	|smtp2jabber.pl

Manually register a bot user on your jabber server and set the user/pass
to mailer/password (if you use other credentials, edit the script
Save the attached script as smtp2jabber.pl and place it in /etc/smrsh or
link it from there. Remember to chmod it to be world executable.

Use cpan or your favourite package manager to install the perl modules
at the top of the script. Restart sendmail and test.



use strict;
use Mail::Message;
use Mail::Message::Field;
use Mail::Message::Construct::Read;
use Mail::Message::Attachment::Stripper;
use Mail::Message::Body;
use Net::Jabber;

use constant SERVER   => "your.jabber.org";
use constant PORT     => 5222;
use constant USERNAME => "mailer";
use constant PASSWORD => "password";
use constant RESOURCE => "Perl Script";

# The email message come in on STDIN
my $msg = Mail::Message->read(\*STDIN);

# Pick out the recipient, subject and sender
my $rcpt    = $msg->head->get('to');
my $subject = $msg->head->get('subject');
my $sender  = $msg->head->get('from');

# Remove the angle brackets
$rcpt =~ s/^<//;
$rcpt =~ s/>$//;
$sender =~ s/^<//;
$sender =~ s/>$//;

# Remove the attachment(s) from the message
my $stripper = Mail::Message::Attachment::Stripper->new($msg);
my Mail::Message $textonly  = $stripper->message;
my $body = $textonly->body;

# Connect to jabber
my $jabber = &setup_Jabber(SERVER, PORT, USERNAME, PASSWORD, RESOURCE,
"normal/forwarding email");

# Compose the message
my $jabmsg->SetMessage(
              "to"      => "$rcpt",
              "subject" => "$sender emailed: $subject",
              "body"    => "$body");

# And send it

# Close the connection and exit

sub setup_Jabber {
  my ($server, $port, $user, $pass, $resource, $initial_status) = @_;
  my $connection = new Net::Jabber::Client;

  # Connect
  my $status = $connection->Connect( hostname => $server,
                                     port     => $port );
  die "Cannot connect to Jabber server $server on port $port\n"
    unless $status;

  # Callbacks
  $connection->SetCallBacks( presence => \&InPresence );

  # Ident/Auth
  my @result = $connection->AuthSend( username => $user,
                                      password => $pass,
                                      resource => $resource );
  die "Ident/Auth failed: $result[0] - $result[1]\n"
    if $result[0] ne "ok";

  # Roster

  # Set initial presence
  &set_presence($connection, $initial_status);

  return $connection;

sub InPresence
  my $presence = $_[1];
  my $type = $presence->GetType();

  if ($type eq "subscribe") {
    $jabber->Send($presence->Reply(type => 'subscribed'));
  elsif ($type eq "unsubscribe") {
    $jabber->Send($presence->Reply(type => 'unsubscribed'));

sub set_presence {
  my ($connection, $s) = @_;
  my $presence = Net::Jabber::Presence->new();
  my ($show, $status) = split("/", $s, 2);
  $presence->SetPresence( show   => $show,
                          status => $status );

-----Original Message-----
From: jdev-bounces at jabber.org [mailto:jdev-bounces at jabber.org] On Behalf
Of Peter Saint-Andre
Sent: 14 December 2005 23:53
To: Jabber software development list
Subject: Re: [jdev] jabberSMTP

Jon Scottorn wrote:
> Hi all,
>    I have been trying to locate a jabber smtp transport, does anyone 
> know of any such thing.  I basically need something that will parse an

> email sent to say jonsmel at jabber.org <mailto:jonsmel at jabber.org> and 
> reformat it into a xmpp message and send it to the user.  I don't 
> really care about attachments, it will only be text.
> Does anyone know of this type of item?

The old theoretic-smtp project did something like that, but it
disappeared after the JabberStudio rootkit. I've sent the code to Jon on
an as-is basis. If anyone else has code like this (more recent than
theoretic-smtp), feel free to post to the list. :-)


More information about the JDev mailing list