RT 83251 - moved script
[freeside.git] / bin / msg_template_http-demo.pl
1 =head1 NAME
2
3 FS::msg_template::http example server.
4
5 =head1 DESCRIPTION
6
7 This is an incredibly crude Mojo web service for demonstrating how to talk 
8 to the HTTP customer messaging interface in Freeside.
9
10 It implements an endpoint for the "password reset" messaging case which 
11 creates a simple password reset message using some template variables,
12 and a "send" endpoint that just delivers the message by sendmail. The 
13 configuration to use this as your password reset handler would be:
14
15 prepare_url = 'http://localhost:3000/prepare/password_reset'
16 send_url =    'http://localhost:3000/send'
17 No username, no password, no additional content.
18
19 =cut
20
21 use Mojolicious::Lite;
22 use Mojo::JSON qw(decode_json encode_json);
23 use Email::Simple;
24 use Email::Simple::Creator;
25 use Email::Sender::Simple qw(sendmail);
26
27 post '/prepare/password_reset' => sub {
28   my $self = shift;
29
30   my $json_data = $self->req->body;
31   #print STDERR $json_data;
32   my $input = decode_json($json_data);
33   if ( $input->{username} ) {
34     my $output = {
35       'to'      => $input->{invoicing_email},
36       'subject' => "Password reset for $input->{username}",
37       'body'    => "
38 To complete your $input->{company_name} password reset, please go to 
39 $input->{selfservice_server_base_url}/selfservice.cgi?action=process_forgot_password;session_id=$input->{session_id}
40
41 This link will expire in 24 hours.",
42     };
43
44     return $self->render( json => $output );
45
46   } else {
47
48     return $self->render( text => 'Username required', status => 500 );
49
50   }
51 };
52
53 post '/send' => sub {
54   my $self = shift;
55
56   my $json_data = $self->req->body;
57   my $input = decode_json($json_data);
58   my $email = Email::Simple->create(
59     header => [
60       From    => $ENV{USER}.'@localhost',
61       To      => $input->{to},
62       Subject => $input->{subject},
63     ],
64     body => $input->{body},
65   );
66   local $@;
67   eval { sendmail($email) };
68   if ( $@ ) {
69     return $self->render( text => $@->message, status => 500 );
70   } else {
71     return $self->render( text => '' );
72   }
73 };
74
75 app->start;
76