How can I delete a message without mailbox file lock? I'm using Perl's Mail::Box - perl

I run Postfix on Ubuntu 16.04 server to send "internal email" messages and a crontab Perl job to parse the related bounce messages (delivered to local mailbox /var/mail/bounceparser). The Perl code basically checks the bounceparser mailbox, parse all the messages and take some actions (delete bounced addresses, etc).
The problem is that when I try to delete those already-parsed messages using Mail::Box library, the mailbox gets locked and if a new message arrives the postfix daemon throws an exception trying to deliver the message: "cannot update mailbox /var/mail/bounceparser for user bounceparser. cannot open file: Permission denied".
Is there a way to delete a message without locking the mailbox file? If it's not possible, any other suggested strategy?
The code I use to delete the messages:
my $mbox = Mail::Box::Mbox->new(folder =>'/var/mail/bounceparser', access => 'rw');
# #mailbox_pending_deletes contains the list of message ids to delete
for my $message_id (#mailbox_pending_deletes){
$message = $mbox->find($message_id);
$message->delete;
}
my $delete_result = $mbox->close(write=>'MODIFIED');
Thank you!

As suggested by #SteffenUllrich using mailbox single file box it's not a good idea (sincerely I was using it just because it's the default Postfix configured value ^_^).
So, if you have a similar issue 1.- Configure Postfix to use Maildir instead of Mailbox for messages delivery (main.cf file):
# Set Postfix to deliver messages to Maildir user folder
home_mailbox = Maildir/
and 2.- use Mail::Box:Maildir and not the Mail::Box:Mbox I was using to find-delete the messages.
my $mbox = Mail::Box::Maildir->new(folder =>'/home/bounceparser/Maildir', access => 'rw');
# #mailbox_pending_deletes contains the list of message ids to delete
for my $message_id (#mailbox_pending_deletes){
$message = $mbox->find($message_id);
$message->delete;
}
my $delete_result = $mbox->close(write=>'MODIFIED');
Fortunately the Sisimai library I use to parse the bounce/delivery/etc messages also accepts a Maildir path to go for the messages:
my $v = Sisimai->make('/home/bounceparser/Maildir/new','hook'=>$x);
Thanks for helping!

Related

syslog-ng - Passing FILENAME from client to server when using wildcard_file

I am using syslog-ng, to remote log the application logs of multiple containers of the same image. I am using the source config as below.
source s_wild { wildcard-file(
base-dir("/var/myapp/logs")
filename-pattern("*")
recursive(no)
flags(no-parse)
follow-freq(1)
); };
When I am using the logging in the local machine (for testing purposes), using the MACRO, ${FILE_NAME}, it works. But the filename is not being passed on, over network when testing with the remote server.
Aug 3 19:39:46 46fc878e92cf syslog-ng[2320]: Error opening file for writing; filename='', error='Is a directory (21)'
There are around 20-25 files and am looking for auto mapping of the filenames in both client and server side. Is it possible. Not sure how the wildcard_file maps to remote server. Logically it may not be possible. Still wondering on a solution.
I am wondering whether I can avoid manual 1-1 mapping by defining multiple source and destination or using log_prefix.
The $FILE_NAME macro works only if syslog-ng receives messages from a file or a wildcard-file source and it does not work over network(). A couple of options you have here to pass file names over network are:
Use the structured-data section of a RFC 5424 syslog message
Use template() with json-parser() to send messages from client-side and parse them on server side
Use ewmm() (Enterprise-wide message model) which supports delivery of structured messages
In the first method, sending the RFC5424-formatted (IETF-syslog) messages allows you to set the FILE_NAME in the SDATA field. Use the syslog() on the source and destination side instead of network() to send the messages using IETF syslog protocol. The source file wildcard can be defined like this. The whole configuration would be something like below:
syslog-ng client side
source s_wild {
wildcard-file(
base-dir("/var/log_syslog")
filename-pattern("*")
recursive(no)
follow-freq(1)
);
};
rewrite r_set_filename{
set(
"$FILE_NAME",
value(".SDATA.file#18372.4.name")
);
};
rewrite r_use_basename {
subst(
"/var/log_syslog/",
"",
value(".SDATA.file#18372.4.name")
type("string")
flags("prefix")
);
};
destination d_container_logs {
syslog(
"192.168.10.48"
transport("tcp")
port(5141)
);
};
log {source(s_wild); rewrite(r_set_filename); rewrite(r_use_basename); destination(d_container_logs);};
The r_set_filename gets the absolute path of file and we chop-off the path bit and retains only the filename using r_use_basename
syslog-ng server side
source s_network {
syslog(
transport("tcp")
port(5141)
keep_hostname(yes)
);
};
destination d_container_logs {
file(
"/var/sys_log/${.SDATA.file#18372.4.name}"
create_dirs(yes)
);
};
log {source(s_network); destination(d_container_logs);};

Spring Integration: Imap, continuous remote service polling caused by attribute mail-filter-expression in imap-idle-channel-adapter

I implemented a imap idle mail receiver with spring integration mail 4.2.6.
When, in the configuration of the receiver in the tag
<mail:imap-idle-channel-adapter mail-filter-expression="something" ... >
i use the attribute "mail-filter-expression" this causes continuous ( about every second) polling of the remote mail service with this repeated message:
10:29:21 INFO o.s.i.mail.ImapMailReceiver - attempting to receive mail from folder [INBOX]
If i remove the attribute "mail-filter-expression" this continuos polling disappears.
I don't know if this is the desired behavior of the framework.
My idea is to limit this comtinuos polling of the remote service, in fact i'm using the imap-idle-channel-adapter
this is my configuration of the receiver:
<mail:imap-idle-channel-adapter
id="mailAdapterMailInAccettazione"
store-uri="imaps://...the url of the remote service
channel="emailsInAccettazioneDaLeggere"
auto-startup="true"
should-delete-messages="false"
should-mark-messages-as-read="false"
java-mail-properties="javaMailProperties"
mail-filter-expression="subject matches '(?i).*somenthing: .*'" >
<mail:transactional synchronization-factory="syncFactoryAccettazione"/>
</mail:imap-idle-channel-adapter>
I want to cofigure the receiver in idle mode but using the mail-filter-expression attribute because i want to read only some emails.
UPDATE : after further investigation i discovered that the remote mail service doesn't support RECENT and USER flag:
This email server does not support RECENT or USER flags
So this is what was happening:
mail-filter-expression caused messages to get filtered, but no message was flagged as RECENT = false. This caused
continuous attempts to read the messages as all messages always appeared "
new"
.
I re implemented the flow not using the mail-filter-expression but filtering messages in subsequent channels

How to specify the Mailbin that the icinga2 uses when invoking the mail command

I have icinga2 set up on a Ubuntu xenial machine. I'm using postfix to send emails, which is working properly, i.e.
echo "hello world" | mail -s "test subject" sammy#example.com
sends an email as expected.
However, when an icinga sends an email, I find the following error message:
/var/log/icinga2/debug.log
[2017-12-12 02:56:05 +0000] notice/Process: PID 5512 >
('/etc/icinga2/scripts/mail-host-notification.sh' '-4' '$
$a.mydomain.com' (PID: 5512, arguments: '/etc/icinga2/scripts/mail-host-notification.sh' '-4' '127.0.0$
Can't canonicalize "./Maildir"
./Maildir/sent: No such file or directory
Failed to save message in "./Maildir/sent" - message not sent
I know that the error is caused by the Maildir directory not being set up. However, I can't figure out how to determine which user is invoking the mail command. I also don't know what the cwd is set to in ./Maildir/sent.
I would be very grateful if anyone could help me figure out either:
Where to set up the Maildir
How to specify the user that icinga uses so as to know where to set up the Maildir
A better method to get icinga to send emails
Thanks in advance
I was able to get my icinga2 instance to mail properly by creating the Maildir in the / directory and setting the owner:group to nagios:nagios. Currently not sure how to configure this, but it is a working configuration!

error while reading mails in camel

<camel:from uri="pop3://125.55.100.14?username=test#mydomain.com&consumer.delay=30000&mapMailMessage=false&delete=true&unseen=true&password=test"/>
I am using this route to read mail from my mailbox
However i m getting following error,
*javax.mail.AuthenticationFailedException*: The system was unable to log test#mydomain.com in. Maildrop (file ) can not be located or opened. Please contact your POP3 administrator.
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:208)[84:javax.mail:1.4.5]
at javax.mail.Service.connect(Service.java:317)[84:javax.mail:1.4.5]
at org.apache.camel.component.mail.MailConsumer.ensureIsConnected(MailConsumer.java:376)[218:org.apache.camel.camel-mail:2.10.0.fuse-71-047]
at org.apache.camel.component.mail.MailConsumer.poll(MailConsumer.java:82)[218:org.apache.camel.camel-mail:2.10.0.fuse-71-047]
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:142)[129:org.apache.camel.camel-core:2.10.0.fuse-71-047]
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:92)[129:org.apache.camel.camel-core:2.10.0.fuse-71-047]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)[:1.7.0_21]
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)[:1.7.0_21]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)[:1.7.0_21]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)[:1.7.0_21]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)[:1.7.0_21]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[:1.7.0_21]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)[:1.7.0_21]
at java.lang.Thread.run(Thread.java:722)[:1.7.0_21]
please help me
It's strange that it says Maildrop (file ) without naming the file. It should be Maildrop (file mail\xxxx) or something similar. So, my first guess is that this error hasn't to do anything with the Camel configuration.

Ant MailLogger doesn't send email

I run ant like this in order to have an email with the log of the build : ant deployDB -logger org.apache.tools.ant.listener.MailLogger
Here or my properties :
MailLogger.mailhost = myhost
MailLogger.port=25
MailLogger.user = myuser
MailLogger.password = mypassword
MailLogger.from = myfromemail
MailLogger.failure.to = myreceipeemail
MailLogger.success.to = myreceipeemail
MailLogger.failure.subject=[DRUPAL][MEP] ${TODAY} - Failure
MailLogger.success.subject=[DRUPAL][MEP] ${TODAY} - Success
I don't receive the email at the end of the build and I have no error message. When I look in /var/log/mail.info there is no log about the email which was supposed to be sent.
I tried my postfix SMTP server with a simple java program, with exactly the same params and I do receive an email, so I guess my SMTP server is working fine.
Since this question has never been marked as resolved I'm just copying what Rebse said in comment.
It was a classpath issue, mail.jar wasn't in it.
Thanks Rebse!