1 | package LATMOS::Accounts::Web::Controller::Services::Mailing; |
---|
2 | use Moose; |
---|
3 | use namespace::autoclean; |
---|
4 | use Email::MIME::Creator; |
---|
5 | use Email::Send (); |
---|
6 | |
---|
7 | BEGIN { extends 'Catalyst::Controller'; } |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | LATMOS::Accounts::Web::Controller::Services::Mailing - Catalyst Controller |
---|
12 | |
---|
13 | =head1 DESCRIPTION |
---|
14 | |
---|
15 | Catalyst Controller. |
---|
16 | |
---|
17 | =head1 METHODS |
---|
18 | |
---|
19 | =cut |
---|
20 | |
---|
21 | |
---|
22 | =head2 index |
---|
23 | |
---|
24 | =cut |
---|
25 | |
---|
26 | sub 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 | |
---|
55 | sub to :Private { |
---|
56 | my ( $self, $c ) = @_; |
---|
57 | } |
---|
58 | |
---|
59 | sub 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 | |
---|
74 | sub 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 | |
---|
93 | sub 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 | |
---|
148 | olivier |
---|
149 | |
---|
150 | =head1 LICENSE |
---|
151 | |
---|
152 | This library is free software. You can redistribute it and/or modify |
---|
153 | it under the same terms as Perl itself. |
---|
154 | |
---|
155 | =cut |
---|
156 | |
---|
157 | __PACKAGE__->meta->make_immutable; |
---|
158 | |
---|
159 | 1; |
---|