-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add send_args to Log::Dispatch::Email and subclasses #17
base: master
Are you sure you want to change the base?
Changes from 1 commit
2c919de
4f66d71
f273609
271bf19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,10 +24,27 @@ sub send_email { | |
|
||
$mail{From} = $self->{from} if defined $self->{from}; | ||
|
||
local $?; | ||
unless ( MIME::Lite->new(%mail)->send ) { | ||
warn "Error sending mail with MIME::Lite"; | ||
} | ||
warn "Error sending mail with MIME::Lite" | ||
unless do { | ||
local $?; | ||
if ( defined $self->{send_args} && @{ $self->{send_args} } > 0 ) { | ||
my @args = @{ $self->{send_args} }; | ||
if ( @args >= 3 ) { | ||
MIME::Lite->new(%mail) | ||
->send( $args[0], $args[1], @args[ 2 .. $#args ] ); | ||
} | ||
elsif ( @args == 2 ) { | ||
MIME::Lite->new(%mail)->send( $args[0], $args[1] ); | ||
} | ||
else { | ||
MIME::Lite->new(%mail)->send( $args[0] ); | ||
} | ||
} | ||
else { | ||
MIME::Lite->new(%mail)->send; | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This all seems like a very complicated way of writing this:
There's no reason to care about the number of args since this code doesn't actually do anything with them. It just passes them along. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need to create a new PR. Just force push to your fork and this PR will be updated. |
||
|
||
} | ||
|
||
1; | ||
|
@@ -46,7 +63,8 @@ __END__ | |
'Email::MIMELite', | ||
min_level => 'emerg', | ||
to => [qw( [email protected] [email protected] )], | ||
subject => 'Big error!' | ||
subject => 'Big error!', | ||
send_args => [ 'smtp', 'smtp.example.org', { AuthUser => 'john', AuthPass => 'secret' } ] | ||
] | ||
], | ||
); | ||
|
@@ -58,4 +76,9 @@ __END__ | |
This is a subclass of L<Log::Dispatch::Email> that implements the | ||
send_email method using the L<MIME::Lite> module. | ||
|
||
=head1 CHANGING HOW MAIL IS SENT | ||
|
||
To change how mail is sent, set send_args to according to what | ||
L<< MIME::Lite->send >> expects. | ||
|
||
=cut |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ sub send_email { | |
|
||
local $?; | ||
eval { | ||
my $fh = $msg->open | ||
my $fh = $msg->open( @{ $self->{send_args} } ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect this will die in a very ugly way when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It works for me. Mail::Send will create a Mail::Mailer object with no attributes, which seems to be ok. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested it and there was no warning. That said, of course I can add a safeguard if you think its an issue. |
||
or die "Cannot open handle to mail program"; | ||
|
||
$fh->print( $p{message} ) | ||
|
@@ -54,7 +54,8 @@ __END__ | |
'Email::MailSend', | ||
min_level => 'emerg', | ||
to => [qw( [email protected] [email protected] )], | ||
subject => 'Big error!' | ||
subject => 'Big error!', | ||
send_args => [ 'smtp', Server => 'mail.example.org' ], | ||
] | ||
], | ||
); | ||
|
@@ -68,13 +69,26 @@ method using the L<Mail::Send> module. | |
|
||
=head1 CHANGING HOW MAIL IS SENT | ||
|
||
There are two ways to change how mail is sent: | ||
|
||
=over 4 | ||
|
||
=item 1 | ||
|
||
Since L<Mail::Send> is a subclass of L<Mail::Mailer>, you can change | ||
how mail is sent from this module by simply C<use>ing L<Mail::Mailer> | ||
in your code before mail is sent. For example, to send mail via smtp, | ||
you could do: | ||
|
||
use Mail::Mailer 'smtp', Server => 'foo.example.com'; | ||
|
||
=item 2 | ||
|
||
Set send_args to the same arguments as | ||
the constructor of L<Mail::Mailer> expects. | ||
|
||
=back | ||
|
||
For more details, see the L<Mail::Mailer> docs. | ||
|
||
=cut |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,9 @@ sub send_email { | |
From => $self->{from} || '[email protected]', | ||
); | ||
|
||
# merge options from %{send_args} | ||
%mail = ( %mail, %{ $self->{send_args} } ) if defined $self->{send_args}; | ||
|
||
local $?; | ||
unless ( Mail::Sendmail::sendmail(%mail) ) { | ||
warn "Error sending mail: $Mail::Sendmail::error"; | ||
|
@@ -46,7 +49,8 @@ __END__ | |
'Email::MailSendmail', | ||
min_level => 'emerg', | ||
to => [qw( [email protected] [email protected] )], | ||
subject => 'Big error!' | ||
subject => 'Big error!', | ||
send_args => { smtp => '127.0.0.1', retries => 10, delay => 5, debug => 0 } | ||
] | ||
], | ||
); | ||
|
@@ -58,4 +62,9 @@ __END__ | |
This is a subclass of L<Log::Dispatch::Email> that implements the | ||
send_email method using the L<Mail::Sendmail> module. | ||
|
||
=head1 CHANGING HOW MAIL IS SENT | ||
|
||
To change how mail is sent, set send_args to a hash reference just | ||
like L<< %Mail::Sendmail::mailcfg >>. | ||
|
||
=cut |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any email sending module where a scalar would make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't seen any, but if we disallow them, we remove the possibility of implementing a subclass of Log::Dispatch::Email that uses a scalar.