From b4497a1e80a56637f0e9487bd1ff2b17e3613d62 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 11 Oct 2014 12:45:17 +0200 Subject: [PATCH] Switch to Mojolicious::Plugin::Mail Close #81 --- lib/Mojolicious/Plugin/Mailer.pm | 90 ---------------------- public/vroom.pl | 73 ++++++++---------- templates/default/feedback.email_text.ep | 7 -- .../{feedback.email_html.ep => feedback.mail.ep} | 0 templates/default/invite.email_text.ep | 32 -------- .../{invite.email_html.ep => invite.mail.ep} | 0 templates/default/notification.email_text.ep | 17 ---- ...fication.email_html.ep => notification.mail.ep} | 0 8 files changed, 31 insertions(+), 188 deletions(-) delete mode 100644 lib/Mojolicious/Plugin/Mailer.pm delete mode 100644 templates/default/feedback.email_text.ep rename templates/default/{feedback.email_html.ep => feedback.mail.ep} (100%) delete mode 100644 templates/default/invite.email_text.ep rename templates/default/{invite.email_html.ep => invite.mail.ep} (100%) delete mode 100644 templates/default/notification.email_text.ep rename templates/default/{notification.email_html.ep => notification.mail.ep} (100%) diff --git a/lib/Mojolicious/Plugin/Mailer.pm b/lib/Mojolicious/Plugin/Mailer.pm deleted file mode 100644 index 529b367..0000000 --- a/lib/Mojolicious/Plugin/Mailer.pm +++ /dev/null @@ -1,90 +0,0 @@ -package Mojolicious::Plugin::Mailer; -use Mojo::Base 'Mojolicious::Plugin'; - -use strict; -use warnings; - -use Email::MIME; -use Email::Sender::Simple; -use Email::Sender::Transport::Test; -use Encode; -use utf8; - -our $VERSION = '0.05'; - - -sub register { - my ($self, $app, $conf) = @_; - - $app->helper( - email => sub { - my $self = shift; - my $args = @_ ? { @_ } : return; - - - my @data = @{ $args->{data} }; - - my @parts = ( - Email::MIME->create( - body => Encode::encode('UTF-8', $self->render( - @data, - format => $args->{format} ? $args->{format} : 'email_text', - partial => 1 - )), - attributes => { - charset => 'utf-8', - encoding => '7bit', - content_type => 'text/plain', - } - ), - Email::MIME->create( - body => Encode::encode('UTF-8', $self->render( - @data, - format => $args->{format} ? $args->{format} : 'email_html', - partial => 1 - )), - attributes => { - charset => 'utf-8', - encoding => '7bit', - content_type => 'text/html', - } - ), - ); - - my $transport = defined $args->{transport} || $conf->{transport} - ? $args->{transport} || $conf->{transport} - : undef; - - my $header = { @{ $args->{header} } }; - - $header->{From} ||= $conf->{from}; - $header->{Subject} ||= $self->stash('title'); - - my $email = Email::MIME->create( - header => [ %{$header} ], - parts => [ @parts ] - ); - - $email->charset_set ( $args->{charset} ? $args->{charset} : 'utf-8' ); - $email->content_type_set( $args->{content_type} ? $args->{content_type} : 'multipart/alternative' ); - - return Email::Sender::Simple->try_to_send( $email, { transport => $transport } ) if $transport; - - my $emailer = Email::Sender::Transport::Test->new; - $emailer->send_email( - $email, - { - to => [ $header->{To} ], - from => $header->{From} - } - ); - return unless $emailer->{deliveries}->[0]->{successes}->[0]; - - } - ); - -} - -1; - -__END__ diff --git a/public/vroom.pl b/public/vroom.pl index 9fe4cf8..2c690a8 100755 --- a/public/vroom.pl +++ b/public/vroom.pl @@ -7,13 +7,12 @@ use lib '../lib'; use Mojolicious::Lite; -use Mojolicious::Plugin::Mailer; +use Mojolicious::Plugin::Mail; use Mojo::JSON; use DBI; use Digest::MD5 qw(md5_hex); use Crypt::SaltedHash; use MIME::Base64; -use Email::Sender::Transport::Sendmail; use Encode; use File::stat; use File::Basename; @@ -208,12 +207,10 @@ plugin I18N => { support_url_langs => [qw(en fr)] }; -# Load mailer plugin with its default values -plugin Mailer => { - from => $config->{emailFrom}, - transport => Email::Sender::Transport::Sendmail->new({ - sendmail => $config->{sendmail} - }) +# Load mail plugin with its default values +plugin mail => { + from => $config->{emailFrom}, + type => 'text/html', }; # Wrapper arround DBI @@ -912,16 +909,13 @@ post '/feedback' => sub { my $self = shift; my $email = $self->param('email') || ''; my $comment = $self->param('comment'); - $self->email( - header => [ - Subject => encode("MIME-Header", $self->l("FEEDBACK_FROM_VROOM")), - To => $config->{feedbackRecipient} - ], - data => [ - template => 'feedback', - email => $email, - comment => $comment - ], + my $sent = $self->mail( + to => $config->{feedbackRecipient}, + subject => $self->l("FEEDBACK_FROM_VROOM"), + data => $self->render_mail('feedback', + email => $email, + comment => $comment + ) ); $self->redirect_to($self->get_url('feedback_thanks')); }; @@ -1217,19 +1211,17 @@ post '/*action' => [action => [qw/action admin\/action/]] => sub { } else{ my $inviteId = $self->add_invitation($room,$rcpt); - if ($inviteId && $self->email( - header => [ - Subject => encode("MIME-Header", $self->l("EMAIL_INVITATION")), - To => $rcpt - ], - data => [ - template => 'invite', - room => $room, - message => $message, - inviteId => $inviteId, - joinPass => ($data->{join_password}) ? 'yes' : 'no' - ], - )){ + my $sent = $self->mail( + to => $rcpt, + subject => $self->l("EMAIL_INVITATION"), + data => $self->render_mail('invite', + room => $room, + message => $message, + inviteId => $inviteId, + joinPass => ($data->{join_password}) ? 'yes' : 'no' + ) + ); + if ($inviteId && $sent){ $self->app->log->info($self->session('name') . " sent an invitation for room $room to $rcpt"); $status = 'success'; $msg = sprintf($self->l('INVITE_SENT_TO_s'), $rcpt); @@ -1491,17 +1483,14 @@ post '/*action' => [action => [qw/action admin\/action/]] => sub { my $subj = ($name eq '') ? sprintf($self->l('s_JOINED_ROOM_s'), $self->l('SOMEONE'), $room) : sprintf($self->l('s_JOINED_ROOM_s'), $name, $room); # Send notifications foreach my $rcpt ($self->get_notification($room)){ - $self->email( - header => [ - Subject => encode("MIME-Header", $subj), - To => $rcpt - ], - data => [ - template => 'notification', - room => $room, - name => $name - ], - ); + my $sent = $self->mail( + to => $rcpt, + subject => $subj, + data => $self->render_mail('notification', + room => $room, + name => $name + ) + ); } return $self->render( json => { diff --git a/templates/default/feedback.email_text.ep b/templates/default/feedback.email_text.ep deleted file mode 100644 index 62b1eba..0000000 --- a/templates/default/feedback.email_text.ep +++ /dev/null @@ -1,7 +0,0 @@ -<%==l 'FROM' %>: <%= $email %> - -<%==l 'COMMENT' %>: <%== $comment %> - --- -<%==l 'EMAIL_SIGN' %> - diff --git a/templates/default/feedback.email_html.ep b/templates/default/feedback.mail.ep similarity index 100% rename from templates/default/feedback.email_html.ep rename to templates/default/feedback.mail.ep diff --git a/templates/default/invite.email_text.ep b/templates/default/invite.email_text.ep deleted file mode 100644 index a80ed3c..0000000 --- a/templates/default/invite.email_text.ep +++ /dev/null @@ -1,32 +0,0 @@ -<% - my $url = $self->url_for('/')->to_abs; - $url .= ($url =~ m/\/$/) ? '' : '/'; -%> -<%==l 'YOU_ARE_INVITED_TO_A_MEETING' %> - - * <%=l 'A_MODERN_BROWSER' %> - * <%=l 'A_WEBCAM' %> - * <%=l 'A_MIC' %> - -<%==l 'WHEN_YOU_ARE_READY' %> - -<% my $roomAccess = $room; - if ($joinPass eq 'yes') { - $roomAccess .= '?token=' . $inviteId; - } -%> -<%== $url . $roomAccess %> - -<% if ($message && $message ne ''){ %> -<%==l 'MESSAGE_FROM_ORGANIZER' %>: - -<%== $message %> -<% } %> - -<%==l 'IF_YOU_CANNOT_JOIN' %> <%==l 'YOU_CAN_NOTIFY_THE_ORGANIZER' %> - -<%== $url . 'invitation?token=' . $inviteId %> - -<%==l 'HAVE_A_NICE_MEETING' %> --- -<%==l 'EMAIL_SIGN' %> diff --git a/templates/default/invite.email_html.ep b/templates/default/invite.mail.ep similarity index 100% rename from templates/default/invite.email_html.ep rename to templates/default/invite.mail.ep diff --git a/templates/default/notification.email_text.ep b/templates/default/notification.email_text.ep deleted file mode 100644 index 03153d7..0000000 --- a/templates/default/notification.email_text.ep +++ /dev/null @@ -1,17 +0,0 @@ -<% - my $url = $self->url_for('/')->to_abs; - $url .= ($url =~ m/\/$/) ? '' : '/'; -%> -<%==l 'SOMEONE_JOINED_A_ROOM' %> - -<% if ($name && $name ne ''){ %> -<%=l 'PARTICIPANT_NAME' %>: <%== $name %> -<% } %> - -<%=l 'WHEN_YOU_ARE_READY' %> - -<%== $url . $room %> - -<%=l 'HAVE_A_NICE_MEETING' %> --- -<%==l 'EMAIL_SIGN' %> diff --git a/templates/default/notification.email_html.ep b/templates/default/notification.mail.ep similarity index 100% rename from templates/default/notification.email_html.ep rename to templates/default/notification.mail.ep