Allow forcing of messages over transports.

This is needed to ensure that users who have disabled email are still
able to get recovery messages.
This commit is contained in:
Chris 2013-02-18 11:42:18 +00:00
parent 3e0551a464
commit 3b677627a3
4 changed files with 17 additions and 12 deletions

View File

@ -140,7 +140,7 @@ sub queue_message {
# unacceptable delays if there are a lot of recipients and slow transports involved. # unacceptable delays if there are a lot of recipients and slow transports involved.
# That doesn't matter with the queue dispatcher, as that will normally be called from # That doesn't matter with the queue dispatcher, as that will normally be called from
# a cron job in the background, but this will happen during normal program flow. # a cron job in the background, but this will happen during normal program flow.
$self -> deliver_message($msgid) $self -> deliver_message($msgid, $args -> {"force_all_transports"})
if($args -> {"send_immediately"}); if($args -> {"send_immediately"});
} }
@ -156,7 +156,7 @@ sub queue_message {
$self -> {"logger"} -> log("messaging", 0, undef, "Queued message $msgid with recipients ".join(",", @{$args -> {"recipients"}})); $self -> {"logger"} -> log("messaging", 0, undef, "Queued message $msgid with recipients ".join(",", @{$args -> {"recipients"}}));
$self -> deliver_message($msgid) $self -> deliver_message($msgid, $args -> {"force_all_transports"})
if($args -> {"send_immediately"}); if($args -> {"send_immediately"});
} }
@ -421,17 +421,20 @@ sub deliver_queue {
} }
## @method $ deliver_message($messageid) ## @method $ deliver_message($messageid, $force_all)
# Given a message ID, attempt to send the message via all available transports. # Given a message ID, attempt to send the message via all available transports.
# This forces immediate delivery of the specified message, if possible, and # This forces immediate delivery of the specified message, if possible, and
# marks it as either sent or failed. # marks it as either sent or failed.
# #
# @param messageid The ID of the message to send. # @param messageid The ID of the message to send.
# @param force_all Force the message to be sent over all available transports, even if
# the user has disabled one.
# @return The number of transport send failures (0 indicates all transports delivered # @return The number of transport send failures (0 indicates all transports delivered
# the message successfully), undef on error. # the message successfully), undef on error.
sub deliver_message { sub deliver_message {
my $self = shift; my $self = shift;
my $messageid = shift; my $messageid = shift;
my $force_all = shift;
$self -> clear_error(); $self -> clear_error();
@ -449,7 +452,7 @@ sub deliver_message {
or return $self -> self_error("Transport loading failed: ".$self -> errstr()); or return $self -> self_error("Transport loading failed: ".$self -> errstr());
# Tru to send the message through this transport # Tru to send the message through this transport
my $sent = $transport -> {"module"} -> deliver($message); my $sent = $transport -> {"module"} -> deliver($message, $force_all);
++$failures if(!$sent); ++$failures if(!$sent);

View File

@ -28,12 +28,13 @@ use base qw(Webperl::Message);
# ============================================================================ # ============================================================================
# Delivery # Delivery
## @method $ deliver($message) ## @method $ deliver($message, $force)
# Attempt to deliver the specified message to its recipients. This function # Attempt to deliver the specified message to its recipients. This function
# does not actually do anything in the Message::Transport class - it must # does not actually do anything in the Message::Transport class - it must
# be overridden in subclasses to actually perform message delivery. # be overridden in subclasses to actually perform message delivery.
# #
# @param message A reference to hash containing the message data. # @param message A reference to hash containing the message data.
# @param force Send messages via this transport even if the user has disabled it.
# @return true if the message is sent successfully, undef if not. # @return true if the message is sent successfully, undef if not.
sub deliver { sub deliver {
my $self = shift; my $self = shift;

View File

@ -102,17 +102,18 @@ sub allow_disable {
} }
## @method $ deliver($message) ## @method $ deliver($message, $force)
# Attempt to deliver the specified message to its recipients. # Attempt to deliver the specified message to its recipients.
# #
# @param message A reference to hash containing the message data. # @param message A reference to hash containing the message data.
# @param force Send messages via this transport even if the user has disabled it.
# @return True on success, undef on failure/error. # @return True on success, undef on failure/error.
sub deliver { sub deliver {
my $self = shift; my $self = shift;
my $message = shift; my $message = shift;
my $force = shift;
if(!$self -> {"persist"}) { if(!$self -> {"persist"}) {
eval { $self -> {"smtp"} = Email::Sender::Transport::SMTP -> new($self -> _build_smtp_args()); }; eval { $self -> {"smtp"} = Email::Sender::Transport::SMTP -> new($self -> _build_smtp_args()); };
return $self -> self_error("SMTP Initialisation failed: $@") if($@); return $self -> self_error("SMTP Initialisation failed: $@") if($@);
} }
@ -122,16 +123,16 @@ sub deliver {
# Work out the the sender if needed... # Work out the the sender if needed...
if(!$self -> {"force_sender"} && $message -> {"sender"}) { if(!$self -> {"force_sender"} && $message -> {"sender"}) {
$from = $self -> _get_user_email($message -> {"sender"} -> {"sender_id"}) $from = $self -> _get_user_email($message -> {"sender"} -> {"sender_id"})
or return undef; or return $self -> self_error("Sender email lookup failed: ".$self -> errstr());
} }
# And the recipients # And the recipients
foreach my $recipient (@{$message -> {"recipients"}}) { foreach my $recipient (@{$message -> {"recipients"}}) {
# Skip users who shouldn't get emails # Skip users who shouldn't get emails
next unless($self -> use_transport($recipient -> {"recipient_id"})); next if(!$force && !$self -> use_transport($recipient -> {"recipient_id"}));
my $recip = $self -> _get_user_email($recipient -> {"recipient_id"}) my $recip = $self -> _get_user_email($recipient -> {"recipient_id"})
or return undef; or return $self -> self_error("Recipient email lookup failed: ".$self -> errstr());
$to .= "," if($to); $to .= "," if($to);
$to .= $recip; $to .= $recip;
@ -146,7 +147,6 @@ sub deliver {
attributes => { charset => 'utf8', attributes => { charset => 'utf8',
content_type => "text/plain", content_type => "text/plain",
encoding => 'base64' }); encoding => 'base64' });
try { try {
sendmail($email, { from => $self -> {"env_sender"}, sendmail($email, { from => $self -> {"env_sender"},
transport => $self -> {"smtp"}}); transport => $self -> {"smtp"}});

View File

@ -28,12 +28,13 @@ use base qw(Webperl::Message::Transport);
# ============================================================================ # ============================================================================
# Delivery # Delivery
## @method $ deliver($message) ## @method $ deliver($message, $force)
# Attempt to deliver the specified message to its recipients. This function # Attempt to deliver the specified message to its recipients. This function
# is always successful - it is impossible for local delivery to fail, as the # is always successful - it is impossible for local delivery to fail, as the
# message is already there! # message is already there!
# #
# @param message A reference to hash containing the message data. # @param message A reference to hash containing the message data.
# @param force Send messages via this transport even if the user has disabled it.
# @return Always returns true. # @return Always returns true.
sub deliver { sub deliver {
my $self = shift; my $self = shift;