1 | package LATMOS::Accounts::Web::Controller::Services::Mailing; |
---|
2 | use Moose; |
---|
3 | use namespace::autoclean; |
---|
4 | use Email::MIME::Creator; |
---|
5 | use Email::Sender::Simple qw(sendmail); |
---|
6 | use Email::Sender::Transport::SMTP::Persistent qw(); |
---|
7 | |
---|
8 | |
---|
9 | BEGIN { extends 'Catalyst::Controller'; } |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | LATMOS::Accounts::Web::Controller::Services::Mailing - Catalyst Controller |
---|
14 | |
---|
15 | =head1 DESCRIPTION |
---|
16 | |
---|
17 | Catalyst Controller. |
---|
18 | |
---|
19 | =head1 METHODS |
---|
20 | |
---|
21 | =cut |
---|
22 | |
---|
23 | |
---|
24 | =head2 index |
---|
25 | |
---|
26 | =cut |
---|
27 | |
---|
28 | sub 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 | |
---|
48 | sub to :Private { |
---|
49 | my ( $self, $c ) = @_; |
---|
50 | } |
---|
51 | |
---|
52 | sub 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 | |
---|
71 | sub 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 | |
---|
127 | olivier |
---|
128 | |
---|
129 | =head1 LICENSE |
---|
130 | |
---|
131 | This library is free software. You can redistribute it and/or modify |
---|
132 | it under the same terms as Perl itself. |
---|
133 | |
---|
134 | =cut |
---|
135 | |
---|
136 | __PACKAGE__->meta->make_immutable; |
---|
137 | |
---|
138 | 1; |
---|