Community discussions

MikroTik App
 
23q
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 57
Joined: Thu Sep 02, 2010 2:54 pm
Location: Ukraine

e-mail "on-error="

Sat Aug 13, 2016 10:56 pm

:do {/tool e-mail send server=1.1.1.1 port=25 user=admin@test.com password=111 start-tls=yes to="111@gmail.com" from=admin@test.com subject="555" body="sdg"} on-error={ :log info "hh"}
if smtp-server 1.1.1.1 not work - "on-error={ :log info "hh"}" - not work!
You do not have the required permissions to view the files attached to this post.
 
23q
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 57
Joined: Thu Sep 02, 2010 2:54 pm
Location: Ukraine

Re: e-mail "on-error="

Mon Aug 15, 2016 3:05 pm

Hello,

E-mail tool already writes in Log errors about unsuccessful mail deliveries. For example:
11:52:05 system,e-mail,error Error sending e-mail <555>: timeout occurred

E-mail tool itself does not return error in CLI and works in background. On-error is not solution for e-mail tool.

Best regards,
Martins S.
 
Def
just joined
Posts: 23
Joined: Wed Sep 10, 2008 11:04 pm

Re: e-mail "on-error="

Thu Mar 16, 2017 5:35 pm

Hi all,
I encountered the same problem. I need to handle errors generated during sending an email.
How could be possible to catch such error (if 'on-error' is not the right solution)?
(My goal is detect error during sending an email and try to resend it again in that case.)

Thank you all in advance.
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 3035
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: e-mail "on-error="

Thu Mar 16, 2017 5:42 pm

Set in the on-error section global variable to send result. In the scheduled script resend e-mail if it is needed.
You could also use this variable as condition in the :do { <commands> } while=( <conditions> ); to effectively send the e-mail.
 
Def
just joined
Posts: 23
Joined: Wed Sep 10, 2008 11:04 pm

Re: e-mail "on-error="

Thu Mar 16, 2017 6:00 pm

Thanks for fast reply.
I'm not sure if I understand what you mean.
How could I fill global variable with result of sending an email (whether email was sent successfully or not)?
If I try:
:global RES
:set RES [/tool e-mail send to="..." body="..." subject="..."]
variable RES is empty after that.
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 3035
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: e-mail "on-error="

Thu Mar 16, 2017 10:08 pm

Rather like this
{
  :local RES 1;
  :do {
     :do { ... send an e-mail ..... } on-error={ :log info "!Error sending an e-mail"; :set RES 0; }
  } while( RES = 0 ) 
}
 
Def
just joined
Posts: 23
Joined: Wed Sep 10, 2008 11:04 pm

Re: e-mail "on-error="

Fri Mar 17, 2017 9:14 am

Thanks.
I tried something like this already, but it doesn't work. Like Martins S. wrote for 23q above: E-mail tool itself does not return error in CLI and works in background. On-error is not solution for e-mail tool.
Even though, I tried your suggestion like this:
{
	:local RES 1;
	:do {
		:do {
			:log info "Sending an email...";
			/tool e-mail send ... ;
			:log info "Email sent.";
		} on-error={
			:log info "!Error sending an e-mail";
			:set RES 0;
		}
	} while (RES = 0)
	:log info "Over.";
}
And here is output in log:
screen.jpg
Is there any other way how could email errors be handled?
You do not have the required permissions to view the files attached to this post.
 
krisjanis
MikroTik Support
MikroTik Support
Posts: 432
Joined: Tue Feb 05, 2013 5:00 pm

Re: e-mail "on-error="

Fri Mar 17, 2017 11:18 am

One more option to check if there was problems with sending email is to check "last-status" value. I sometimes use it in scripts to check if last attempt to send email was successful or not.
[admin@stationDude] > :global mailStatus [/tool e-mail get last-status]        
[admin@stationDude] > :put $mailStatus 
succeeded
 
Def
just joined
Posts: 23
Joined: Wed Sep 10, 2008 11:04 pm

Re: e-mail "on-error="

Fri Mar 17, 2017 12:42 pm

Thank you very much... this is pretty close to the goal!
I suggest it will work great in most of the common cases. But I think it will fail on some special cases.
I made script like this:
:local attempts 0;
do {
	:set attempts ($attempts+1);
	:if ($attempts > 1) do {:log info ("ERROR - last email status: ".[/tool e-mail get last-status];); :delay 2s;}
	/tool e-mail send ... ;
	do {:delay 200ms;} while ([/tool e-mail get last-status] = "in-progress")
} while ($attempts < 5 and [/tool e-mail get last-status] != "succeeded")
=> script tries to resend email every 2 seconds, if last status was not "succeeded".

But let's suppose there is one email sending in progress with large attachment, so it takes a lot of time to send it (last-status is 'in-progress'). Now let's suppose, that another short email is sent while the first one is still in progress. If 2nd email is sent with success, last-status will be updated to 'succeeded' and that will terminate first script - so it will not check if first email was sent with success or failure. And if first email crashes with error, first email will not be resent again.
Is that right?

Who is online

Users browsing this forum: TikYAN and 23 guests