Tuesday, September 7, 2010

Change domain name in mail command for Unix

I was using mail command to send mail from the command line. However, my from address was coming as username@hostname and my mail server was not accepting this mail. I searched google for solutions but none worked for me. So atlast this is what I did.

1. Installed sendmail using "sudo apt-get install sendmail"
2. Install mailutils - "sudo apt-get install mailutils"
2. sudo vim /etc/mail/sendmail.mc
3. Changed the parameter "MASQUERADE_AS"

MASQUERADE_AS(`mydomain.com')dnl

4. Run "sudo sendmailconfig"
5. Now sent mail using
echo "this is mail body" | mail -s "this is test " sabu@mydomain.com

My last few lines of sendmail.c looks like :-

include(`/etc/mail/m4/dialup.m4')dnl
include(`/etc/mail/m4/provider.m4')dnl
dnl #
dnl # Default Mailer setup
dnl # Masquerading options
FEATURE(`always_add_domain')dnl
MASQUERADE_AS(`mydomain.com')dnl
FEATURE(`allmasquerade')dnl
FEATURE(`masquerade_envelope')dnl
MAILER_DEFINITIONS
MAILER(`local')dnl
MAILER(`smtp')dnl

Sendmail installation issue

While installing sendmail, I was thrown the following error :-

*** ERROR: FEATURE() should be before MAILER()
*** MAILER(`local') must appear after FEATURE(`always_add_domain')*** ERROR: FEA TURE() should be before MAILER()
*** MAILER(`local') must appear after FEATURE(`allmasquerade')*** ERROR: FEATURE () should be before MAILER()
NONE:0: m4: ERROR: EOF in string

I searched google only to find people suggesting to install postfix instead of sendmail. Now thats not what I want to do. I want to fix the above issue. Here is how I did it :-
sudo vim /etc/mail/sendmail.mc
Search and move the following lines to end of the file :-

MAILER_DEFINITIONS
MAILER(`local')dnl
MAILER(`smtp')dnl

And now it works.