Archive for the ‘Email’ Category

Sending emails with subject line by using sendmail

Monday, October 12th, 2009

(echo “Subject: e-mail subject here” ; cat your_file ) | sendmail admin@xyz.com

or if you want text to be inside command line:

(echo “Subject: e-mail subject here” ; echo “test”) | sendmail admin@xyz.com

Delete emails with "permanent error" from qmail queue

Monday, October 12th, 2009

bash-2.05# /etc/init.d/qmailctl stat
/service/qmail-send: up (pid 22206) 5153360 seconds
/service/qmail-send/log: up (pid 22207) 5153360 seconds
/service/qmail-smtpd: up (pid 22210) 5153360 seconds
/service/qmail-smtpd/log: up (pid 22211) 5153360 seconds
messages in queue: 8011
messages in queue but not yet preprocessed: 1

When tailing the mail log (/var/log/maillog) I noticed that most of the messages cannot be delivered and normally they are queued for another delivery attempt and they had errors like:

deferral: Sorry,_I_wasn’t_able_to_establish_an_SMTP_connection.

deferral: Sorry,_I_couldn’t_find_any_host_by_that_name

failure:172.24.148.18_does_not_like_recipient./Remote_host_said:_550_5.1.1_<manaobeld@greatdomains.com>…_User_unknown/Giving_up_on_172.24.148.18….

Many of these messages will not be delivered in the future deliveries and they’ll end up in the mail queue with permanent errors:

From: MAILER-DAEMON@mx.crsnic.net
To: georginagarneys@tips.love.mobilmir.com
Subject: failure notice

Hi. This is the qmail-send program at mx.crsnic.net.
I’m afraid I wasn’t able to deliver your message to the following addresses.
This is a permanent error; I’ve given up. Sorry it didn’t work out.

These messages will be in the /var/qmail/queue/mess/ directory and each one has it’s ID number. For example if you look in the /var/qmail/queue/mess/5 and do more 720664 you’ll see the actual message:

bash-2.05# more 720664
Received: (qmail 24858 invoked for bounce); 12 Apr 2006 16:34:48 -0000
Date: 12 Apr 2006 16:34:48 -0000
From: MAILER-DAEMON@mx.crsnic.net
To: courtney589ae@fuse.net
Subject: failure notice

Hi. This is the qmail-send program at mx.crsnic.net.
I’m afraid I wasn’t able to deliver your message to the following addresses.
This is a permanent error; I’ve given up. Sorry it didn’t work out.

Now we know that if we search for “permanent error;” that we’ll find undeliverable messages in the queue and once we have all their IDs we can remove them from the mail queue and here is the script that does that. This script is tested on core mx servers and directory locations within the script may need adjusting if run it on other mx servers:

#!/bin/bash

cd /var/qmail/queue/mess

find . -exec grep -l “permanent error;” {} /dev/null \; > /tmp/junkmail

cd /var/qmail/queue

for i in `cat /tmp/junkmail`
do

find . -name `basename $i` -exec rm {} \;

done

You can copy it in the temp dir and later run it as root. Before you run it make sure that you stopped qmail (/etc/init.d/qmailctl stop). Do not run the script if the qmail is not down. Below procedure should be run only at one mx server at the time:

Here are the steps to run the script:

1. Stop qmail

bash-2.05# /etc/init.d/qmailctl stop
Stopping qmail…
qmail-smtpd
qmail-send

2. make sure qmail is down:

bash-2.05# /etc/init.d/qmailctl stat
/service/qmail-send: up (pid 22206) 5154619 seconds, want down
/service/qmail-send/log: down 23 seconds, normally up
/service/qmail-smtpd: down 23 seconds, normally up
/service/qmail-smtpd/log: down 23 seconds, normally up
messages in queue: 8050
messages in queue but not yet preprocessed: 3

When you stop qmail it attempts to flush all the messages from the mail queue so it may take a while, that is why /service/qmail-send is still up

3. Check if any service is listening on port 25

bash-2.05# netstat -an|grep LIST
tcp        0      0 0.0.0.0:2700            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:4750            0.0.0.0:*               LISTEN
tcp        0      0 172.24.24.19:22         0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:2780            0.0.0.0:*               LISTEN

4. After about 10-15 min or so you’ll see:

bash-2.05# /etc/init.d/qmailctl stat
/service/qmail-send: down 184 seconds, normally up
/service/qmail-send/log: down 531 seconds, normally up
/service/qmail-smtpd: down 531 seconds, normally up
/service/qmail-smtpd/log: down 531 seconds, normally up
messages in queue: 8048
messages in queue but not yet preprocessed: 3

bash-2.05# ps -eaf|grep mail
root       741   738  0  2005 ?        00:00:00 supervise qmail-send
root       743   738  0  2005 ?        00:00:00 supervise qmail-smtpd
qmailq    3541  3030  0 12:48 pts/0    00:00:00 /var/qmail/bin/qmail-queue
qmaild   11085     1  0 16:01 ?        00:00:00 /var/qmail/bin/qmail-smtpd
root     12391 10994  0 16:20 pts/3    00:00:00 grep mail

If you don’t want to wait longer you can kill /var/qmail/bin/qmail-queue and /var/qmail/bin/qmail-smtpd but leave running supervise qmail-send and supervise qmail-smtpd processes.

5. Now we run the script. I called it /tmp/rmqueue:

bash-2.05# bash /tmp/rmqueue &
[1] 12539
bash-2.05# grep: .: Is a directory
grep: ./0: Is a directory
grep: ./1: Is a directory
grep: ./10: Is a directory
grep: ./11: Is a directory
grep: ./12: Is a directory
grep: ./13: Is a directory
grep: ./14: Is a directory
grep: ./15: Is a directory
grep: ./16: Is a directory
grep: ./17: Is a directory
grep: ./18: Is a directory
grep: ./19: Is a directory
grep: ./2: Is a directory
grep: ./20: Is a directory
grep: ./21: Is a directory
grep: ./22: Is a directory
grep: ./3: Is a directory
grep: ./4: Is a directory
grep: ./5: Is a directory
grep: ./6: Is a directory
grep: ./7: Is a directory
grep: ./8: Is a directory
grep: ./9: Is a directory
[1]+  Done                    bash /tmp/rmqueue
bash-2.05#

6. Now we can check for number of emails in the queue:

bash-2.05# /etc/init.d/qmailctl stat
/service/qmail-send: down 1202 seconds, normally up
/service/qmail-send/log: down 1549 seconds, normally up
/service/qmail-smtpd: down 1549 seconds, normally up
/service/qmail-smtpd/log: down 1549 seconds, normally up
messages in queue: 25
messages in queue but not yet preprocessed: 2

7. Here we check for number of the messages that had permanent error:

bash-2.05# cat /tmp/junkmail |wc -l
8023

So 8023+25=8048 which was the number of the messages in the mail queue before we run the script. This number may be slightly off if you havan’t stopped qmail immedately after running qmailctl stat.

8. Now we can start qmail:

/etc/init.d/qmailctl start
Starting qmail

10. tail -f /var/log/maillog to check that new messages are being received and delivered:

Apr 15 16:40:29 mx1 qmail: 1145133629.796907 starting delivery 332: msg 712723 to remote postmaster@mx.crsnic.net
Apr 15 16:40:29 mx1 qmail: 1145133629.797000 status: local 0/10 remote 3/70
Apr 15 16:40:29 mx1 qmail: 1145133629.915132 delivery 332: success: 172.24.148.16_accepted_message./Remote_host_said:_250_2.0.0_k3FKeTjW003860_Message_accepted_for_delivery/
Apr 15 16:40:29 mx1 qmail: 1145133629.915336 status: local 0/10 remote 2/70
Apr 15 16:40:29 mx1 qmail: 1145133629.915355 end msg 712723

And check mail queue

bash-2.05# /etc/init.d/qmailctl stat
/service/qmail-send: up (pid 28556) 80 seconds
/service/qmail-send/log: up (pid 28557) 80 seconds
/service/qmail-smtpd: up (pid 28560) 80 seconds
/service/qmail-smtpd/log: up (pid 28561) 80 seconds
messages in queue: 36
messages in queue but not yet preprocessed: 1

Check connections:

bash-2.05# netstat -an|grep EST
tcp        0      0 172.24.16.19:25         82.75.115.254:21870     ESTABLISHED
tcp        0      0 172.24.16.19:25         222.35.7.153:6091       ESTABLISHED
tcp        0      0 172.24.24.19:48665      172.24.148.18:25        ESTABLISHED
tcp        0      0 172.24.16.19:25         201.27.21.147:8855      ESTABLISHED
tcp        0      0 172.24.16.19:25         195.128.188.104:24254   ESTABLISHED
tcp        0   4311 172.24.16.19:48671      218.75.86.19:25         ESTABLISHED
tcp        0      0 172.24.16.19:25         218.71.255.113:8426     ESTABLISHED
tcp        0      0 172.24.16.19:25         60.222.162.51:13354     ESTABLISHED
tcp        0      0 172.24.16.19:25         61.141.229.37:22632     ESTABLISHED

You should see qmail receiving connections to port 25 and making connections to port 25 on remote machines.

Try to connect to qmail-smtpd daemon:

bash-2.05# telnet 0 25
Trying 0.0.0.0…
Connected to 0.
Escape character is ‘^]’.
220 mx.crsnic.net ESMTP
^]

Everything looks good and we are done.