source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Services/Mailing.pm @ 1135

Last change on this file since 1135 was 1135, checked in by nanardon, 12 years ago

Merge branch 'dyngroup'

File size: 3.0 KB
Line 
1package LATMOS::Accounts::Web::Controller::Services::Mailing;
2use Moose;
3use namespace::autoclean;
4use Email::MIME::Creator;
5use Email::Sender::Simple qw(sendmail);
6use Email::Sender::Transport::SMTP::Persistent qw();
7
8
9BEGIN { extends 'Catalyst::Controller'; }
10
11=head1 NAME
12
13LATMOS::Accounts::Web::Controller::Services::Mailing - Catalyst Controller
14
15=head1 DESCRIPTION
16
17Catalyst Controller.
18
19=head1 METHODS
20
21=cut
22
23
24=head2 index
25
26=cut
27
28sub index :Path :Args(0) {
29    my ( $self, $c ) = @_;
30
31    if ($c->req->param('sto')) {
32        $c->session->{mailing}{subject} = $c->req->param('subject');
33        $c->session->{mailing}{body} = $c->req->param('body');
34        $c->go('to');
35    }
36    if ($c->req->param('add_to')) {
37        $c->go('to');
38    }
39    if ($c->req->param('remove_to')) {
40        $c->go('to');
41    }
42
43    if ($c->req->param('send')) {
44        $c->go('send');
45    }
46}
47
48sub to :Private {
49    my ( $self, $c ) = @_;
50}
51
52sub filter :Local {
53    my ( $self, $c ) = @_;
54    for ($c->req->param('filter') || '') {
55    /^dpmt/ and do {
56        $c->req->params->{attr} = [ qw(sutype) ];
57        $c->req->params->{attrval} = [ qw(dpmt) ];
58        $c->go('/ajax/select_objects_list', [ 'group', ]);
59        last;
60    };
61    /^quick$/ and do {
62        $c->go('/ajax/services/mailing/quick_input', [ ]);
63        last; 
64    };
65
66    # Else
67    $c->go('/ajax/attrvalues', [ 'user', $_ ]);
68    }
69}
70
71sub send :Private {
72    my ( $self, $c ) = @_;
73
74    my $from = $c->model('Accounts')->base->get_object(
75        'user', $c->user->username
76    )->get_attributes('mail');
77
78    my $bodypart = Email::MIME->create(
79        attributes => {
80            content_type => 'text/plain',
81            disposition  => 'attachment',
82            charset      => 'utf-8',
83            encoding     => '8bit',
84        },
85        body => $c->req->param('body'),
86    );
87
88    my $email = Email::MIME->create(
89        header_str => [
90            From => $from,
91        ],
92        attributes => {
93            charset      => 'utf-8',
94        },
95        parts      => [ $bodypart ],
96    );
97    $email->header_set( Subject => $c->req->param('subject') );
98
99    warn $email->as_string;
100    foreach (sort (keys %{ $c->session->{to} })) {
101        my $obj = $c->model('Accounts')->base->get_object(
102            'user', $_
103        ) or next;
104        my $email_addr = $obj->get_attributes('mail') or next;
105        my $to = sprintf(
106            '%s <%s>',
107            $obj->get_attributes('displayName'),
108            $email_addr
109        );
110        $email->header_set( To => $to );
111        sendmail(
112            $email,
113            {
114                from => $from,
115                transport => Email::Sender::Transport::SMTP::Persistent->new({
116                        host => 'localhost',
117                        port => 25,
118                })
119            }
120        );
121    }
122
123}
124
125=head1 AUTHOR
126
127olivier
128
129=head1 LICENSE
130
131This library is free software. You can redistribute it and/or modify
132it under the same terms as Perl itself.
133
134=cut
135
136__PACKAGE__->meta->make_immutable;
137
1381;
Note: See TracBrowser for help on using the repository browser.