Posts Tagged ‘Mail’

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 “myaddress@something.com” -s “Some subject” -S smtp=some.smtp.server info@company.com < 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: ./mail.py

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:
try:
line = sys.stdin.readline()
except EOFError:
break
if not line:
break
msg = msg + line
return fromaddr, toaddrs, msg

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

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

if __name__ == ‘__main__’:
main()

regards

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/main.cf

then add the following code to your configuration file.

myhostname = MailServer.mehdibehamin.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname  ## check that the name is correct
mydestination = mehdibehamin.com ## in order to receive username@mehdibehamin.com mails
relayhost =
mynetworks = 10.10.0.0/24 10.10.10.0/24 127.0.0.1/32 ## Note that you should insert the 127.0.0.1

#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/main.cf

and add the following options

smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = yourdomain.com
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 list.dsbl.org, reject_rbl_client sbl.spamhaus.org, reject_rbl_client cbl.abuseat.org ,  reject_rbl_client dul.dnsbl.sorbs.net,  reject_rbl_client multi.uribl.com,  reject_rbl_client dsn.rfc-ignorant.org,  reject_rbl_client dul.dnsbl.sorbs.net,  reject_rbl_client sbl-xbl.spamhaus.org, reject_rbl_client bl.spamcop.net,  reject_rbl_client ix.dnsbl.manitu.net,  reject_rbl_client combined.rbl.msrbl.net,  reject_rbl_client rabl.nuclearelephant.com,  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 :  http://www.howtoforge.com/virtual-users-domains-postfix-courier-mysql-squirrelmail-ubuntu-9.10-p3

http://www.cs.uu.nl/technical/services/email/spam-policy.html

http://www.ax11.de/howtos/postfix-antispam-howto.html#files

http://www.debianadmin.com/debian-mail-server-setup-with-postfix-dovecot-sasl-squirrel-mail.html )

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: 127.0.0.1:10026 to Listen: 127.0.0.1:10025

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

vim  /etc/postfix/main.cf
content_filter = scan:127.0.0.1:10025
receive_override_options = no_address_mappings

also 

vim etc/postfix/master.cf
# 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

127.0.0.1:10026 inet  n -       n       -       16      smtpd

        -o content_filter=

        -o receive_override_options= no_unknown_recipient_checks,
no_header_body_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=127.0.0.0/8

(reference : http://www.debian-administration.org/articles/259 )

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 

X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*

 :)  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.

regards
aptitude install dovecot-imapd dovecot-pop3d dovecot-common

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