Friday 2 September 2011

Using mail command to send emails from script

I had to create a script which sends the output log file through e-mail but the mail's were not working as the main smtp server rejects emails from unknown domains such as "root@local.net" or "root@foo.bar.net" so I searched google and found some different way to send mails with the mail command in this situation.

Following are the tips which can help:

1) editing the sendmail.cf file to change the domain name while sending emails:



edit your sendmail.cf file (should be in /etc)
Scroll down to the

##################
#   local info   #
##################

section and look for these lines:

# my official domain name
# ... define this only if sendmail cannot automatically determine your domain
#Dj$w.Foo.COM
^^^^^^^^^^^^^ copy/paste this line

uncomment the copy and change this line so that it looks like this:

Djitercom.org

the Dj are important - dont edit them out.  Save your changes and send yourself a test email.

The email you recieve should now have the user it was sent from followed by your preferred domain name - instead of the node and complete domain name



I had to put a space between DJ and itercom.org (e.g. Dj itercom.org) to make it work.

ps - if you want to see what your version is and some other info, type this:

/usr/lib/sendmail -d0 -bt < /dev/null

the character after -d in the above is a zero.

You should get output similar to this:

Version AIX5.3/8.11.6p2
 Compiled with: _FFR_MILTER LDAPMAP MAP_REGEX LOG MATCHGECOS MIME7TO8
                MIME8TO7 NAMED_BIND NDBM NETINET NETINET6 NETUNIX NEWDB NIS
                NISPLUS QUEUE SCANF SMTP USERDB XDEBUG

============ SYSTEM IDENTITY (after readcf) ============
      (short domain name) $w = somenode
  (canonical domain name) $j = somenode.somedomain.com
         (subdomain name) $m = somedomain.com
              (node name) $k = somenode
========================================================

ADDRESS TEST MODE (ruleset 3 NOT automatically invoked)
Enter



Note: you will see  your host name and domain in place of somenode and somedomain. 

 
 2) changing the from address on the command line as a option to mail command


On Centos I'm able to do:
mail -s "Subject" user@address.com -- -f from@address.com < body
The double dash stops mail from parsing the -f argument and passes it along to sendmail itself.

References: 





No comments:

Post a Comment