Posts Tagged ‘SMTP’

How to send mail in command line interface

Hi again.
In the linux CLI you can easily send mail your files or texts you want. just you can use the nail command.

nail -r “” -s “Some subject” -S smtp=some.smtp.server < msg.txt

also you can permanently set the SMTP server in your ~/.mailrc file (or /etc/nail.rc if you want to set it system-wide), which removes the need for using the “-S smtp=…” option on the command-line:

set smtp=some.smtp.server

the second way you can use the following script i download from internet :

#!/usr/bin/env python

# Usage: ./

import sys
import smtplib

def prompt(prompt):
return raw_input(prompt).strip()

def create_message():
fromaddr = prompt(‘From: ‘)
toaddrs = prompt(‘To: ‘).split()
subject = prompt(‘Subject: ‘)
print ‘Enter message, end with ^D (Unix) or ^Z (Windows): ‘
msg = (‘From: %s\r\nTo: %s\r\nSubject: %s\r\n\n’ \
% (fromaddr, ‘, ‘.join(toaddrs), subject))
while 1:
line = sys.stdin.readline()
except EOFError:
if not line:
msg = msg + line
return fromaddr, toaddrs, msg

def send_mail(fromaddr, toaddrs, msg):
server = smtplib.SMTP(‘localhost’)
server.sendmail(fromaddr, toaddrs, msg)

def main():
[fromaddr, toaddrs, msg] = create_message()
send_mail(fromaddr, toaddrs, msg)

if __name__ == ‘__main__’:


Postfix Virtual Domain Hosting Howto


Hi again

regarding one of my previous post which was about installing the postfix with spam filtering, i decided to write about the another capability of the postfix. the ability of serving to multiple domain. it could be configured with my_destination option but i highly recommended you to  enable it as virtual domain hosting.

the option you should add to your file (main configuration file of postfix commonly placed in /etc/postfix) are :

virtual_alias_domains = ,,
virtual_alias_maps = hash:/etc/postfix/virtual

after all you should create a new file which name /etc/postfix/virtual and add the the following option in it.

$yourdomainname       virtual

postmaster@$yourdomainname  user1

info@$yourdomainname  user2

@$yourdomainname user1

the second line indicates that the the mail postmaster@$yourdomainname should be forwarded to user1 of your linux system.

and the last says all the other email address with your domain suffix will be forwarded to user1.

then you should create the db file for your virtual file.

postmap /etc/postfix/virtual

then you should link it to your postfile chroot address.

ln -s /etc/postfix/virtual.db /var/spool/postfix/etc/

then reload your postfix service :)

reference :

thats it.


fatal: open database /etc/postfix/virtual.db: No such file or directory


Hi again

in order to cover this error you should first create your virtual.db file, by typing the following command.

postmap /etc/postfix/virtual

but perhaps this problem still exists. as i wrote in my previous posts the postfix uses chroot by default. therefore you need to link the virtual.db file into the postfix root directory.

ln -s /etc/postfix/virtual.db /var/spool/postfix/etc/

after that the error should be eliminated from your mail log files.


ln -s /etc/postfix/virtual.db /var/spool/postfix/etc/

How to setup Linux Mail Server (Postfix + Dovecot + SASL + Spam Filtering)


Hi again.

Firstly i thank ray ban store , Robert Shumake and Mohsen Amiri for their reading and commenting my posts.

when i searching the internet i found that there is no complete reference for installing the mail server in linux. therefore i decided to add the blog entry to define the complete process of  installing the mail server with spam filtering.

note that all the actions i will talk are in Debian Linux Environment which is my favorite Linux distribution however all these action can be run in other distribution with a little changes. if you have problem with other distribution just let me know.

ok lets do it.

At first you should download the necessary packages:

now start installing MTA (Mail Transferring Agent)

aptitude install postfix postfix-tls libsasl2 sasl2-bin libsasl2-modules popa3d

after installing you should start configuring your postfix service therefore

vim /etc/postfix/

then add the following code to your configuration file.

myhostname =
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname  ## check that the name is correct
mydestination = ## in order to receive mails
relayhost =
mynetworks = ## Note that you should insert the

#due to connection to clamav in future

mailbox_command = procmail -a “$EXTENSION”
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

and the start to configuring the other option may you need to set in your postfix service. (please override the above configuration if any exist in your file).  honestly we should check our postfix config file again in few next steps.

ok, after installing the postfix, you should install the IMAP/POP3 servers. its so easier than the postfix :) just run the following commands.

aptitude install dovecot-imapd dovecot-pop3d dovecot-common

after installing process would be finished. open the configuration file and start to uncomment some configuration option of the dovecote

vim /etc/dovecot/dovecot.conf

and uncomment the following option :

protocols = pop3 imap

disable_plaintext_auth = no
pop3_uidl_format = %08Xu%08Xv

(Hummm, you can find these option in vim environment by type “/$your search Criteria ‘ )

now you can start to add users. simply add user with command adduser $yourNewUser

then restart your postfix and dovecote services.

now its the turn of SASL. but let me to explain something. for the security reason the postfix use the chroot directory and if you want to address some files to the postfix (eg some other extra files), you should copy or link them to the root directory of the postfix. (mostly /var/spool/postfix).

ok back to our mail server isntalatin.

again you should edit the postfix main configuration file.

vim /etc/postfix/

and add the following options

smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain =
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated, reject_unauth_destination ##
Note that this option should be optimized for spam filtering :)
smtpd_sasl_security_options = noanonymous

but for start using of the sasl ,still there is somthing to do.

you should remove the current sasl directory and link it to the postfix root folder.

rm -r /var/run/saslauthd/

mkdir -p /var/spool/postfix/var/run/saslauthd

ln -s /var/spool/postfix/var/run/saslauthd /var/run

chgrp sasl /var/spool/postfix/var/run/saslauthd

adduser postfix sasl

and then you should edit your dovecote configuration file again:( therefore again

vim /etc/dovecot/dovecot.conf

auth default {
mechanisms = plain login
passdb pam {
userdb passwd {
socket listen {
client {
path = /var/spool/postfix/private/auth
mode = 0660
user = postfix
group = postfix



ok almost done. now you have a mail server working on linux but there is very important **point.**

mail server works based on the MX (Mail Exchange) DNS records. so if you want to test you mail server, you should configure your DNS server. (i will let you know how to configure your dns in my next post)

OK, but there is something left. till now we install the mail server which is perfect for  clients. but if we want to connect your mail server to the internet there are many unauthorized users who use your mail server to send their spam or mails which cause many mail servers in internet block your mail server IP address. therefor we should configure mail server so that misuse are minimized.

in the postfix there are few configuration option which helps us to reduce the misusing of the unknown clients. there are some rules that the server should obey. for instance reject the unregistered IP address (mostly they are dynamic IPs which ISP leased to their clients eg).

iv made some configuration which you can add to your postfix configuration file.

default_destination_rate_delay = 10

default_destination_recipient_limit = 5

default_extra_recipient_limit = 50

smtpd_recipient_limit = 50

smtpd_helo_required = yes

disable_vrfy_command = yes

strict_rfc821_envelopes = yes

invalid_hostname_reject_code = 554

multi_recipient_bounce_reject_code = 554

non_fqdn_reject_code = 554

relay_domains_reject_code = 554

unknown_address_reject_code = 554

unknown_client_reject_code = 554

unknown_hostname_reject_code = 554

unknown_local_recipient_reject_code = 554

unknown_relay_recipient_reject_code = 554

unknown_sender_reject_code = 554

unknown_virtual_alias_reject_code = 554

unknown_virtual_mailbox_reject_code = 554

unverified_recipient_reject_code = 554

unverified_sender_reject_code = 554

disable_vrfy_command = yes

smtpd_delay_reject = yes

smtpd_helo_required = yes

smtpd_helo_restrictions = permit_mynetworks, reject_non_fqdn_hostname, reject_invalid_hostname, permit

smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated, reject_invalid_hostname, reject_non_fqdn_hostname, reject_non_fqdn_sender, reject_non_fqdn_recipient, reject_unknown_sender_domain, reject_unknown_recipient_domain, reject_rbl_client, reject_rbl_client, reject_rbl_client ,  reject_rbl_client,  reject_rbl_client,  reject_rbl_client,  reject_rbl_client,  reject_rbl_client, reject_rbl_client,  reject_rbl_client,  reject_rbl_client,  reject_rbl_client,  permit,  reject

### i changed the default values to my optimum way based on my network. you should do the same

smtpd_error_sleep_time = 1s

smtpd_soft_error_limit = 10

smtpd_hard_error_limit = 20

smtp_client_restrictions = permit_mynetworks,  permit_sasl_authenticated,  reject_maps_rbl, reject_unknown_client

(references : )

thats it.  the above configuration will prevent many spams. but i recommend you to add an anti virus to your mail server.

Be patient, there is one step left and that is installing the anti spam .

apt-get install clamsmtp clamav-freshclam

Edit the  /etc/clamsmtpd.conf  file and change  
OutAddress: 10025 to OutAddress: 10026.
also change
Listen: to Listen:

then edit the postfix main configuration file again
and add the following options :)  

vim  /etc/postfix/
content_filter = scan:
receive_override_options = no_address_mappings


vim etc/postfix/
# AV scan filter (used by content_filter)
scan      unix  -       -       n       -       16      smtp

        -o smtp_send_xforward_command=yes

# For injecting mail back into postfix from the filter inet  n -       n       -       16      smtpd

        -o content_filter=

        -o receive_override_options= no_unknown_recipient_checks,

        -o smtpd_helo_restrictions=

        -o smtpd_client_restrictions=

        -o smtpd_sender_restrictions=

        -o smtpd_recipient_restrictions=permit_mynetworks,reject

        -o mynetworks_style=host

        -o smtpd_authorized_xforward_hosts=

(reference : )

now restart your server and check for the errors and logs on
/var/log/mail.log or /vae/log/mail.err

now you can test your anti-virus by sending the mail with the
following content 


 :)  thats it.
it should be recognize the following command as virus.
it is not a real virus but a test string called the Eicar.

and finally you can update your antivirus engine manually by
entering freshclam command.
yeah thats it!!! you configure your mail server with spam filtering. 

Note: by the way dont forget to configure your sasld to start.
 vim /etc/default/saslauthd
and change START=no to START=yes.

there are many other option available but they are really should
be set based on your network conditioned.
therefore if you have any question dont hesitate to ask me later.

aptitude install dovecot-imapd dovecot-pop3d dovecot-common

     Copyrighted Mehdi Behamin , All Rights Reserved
Blog has been visited 207206 times