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

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

ensure from is properly set

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