FedoraForum.org - Fedora Support Forums and Community
Results 1 to 10 of 10
  1. #1
    Mr Tux Guest

    How to use Mail in Bash ?

    Hi !

    How to send an email to an emailadress on the internet, ex info@company.com by using the Mail program in Bash ?

    Is there some configuration to do before ?

  2. #2
    jim's Avatar
    jim is offline Retired Community Manager & Avid Drinker Of Suds
    Join Date
    Feb 2005
    Location
    Rochester NY
    Age
    52
    Posts
    4,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mail you@somewhere.com

    CTRL D to send
    Registered Linux User: #376813
    Western NY
    My linux site
    Smolt Profile

    please remember to say if you problem was solved

    Did you get your id10t award today?

  3. #3
    Join Date
    Dec 2006
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, a simple use of mail is as follows:

    Code:
    & mail -s 'Subject' info@company.com < msg.txt
    where -s specifies the subject. Prepare your message (msg.txt) as follows:

    Code:
    From: your.address@foo.com
    To: info@company.com (mandatory)
    Subject: (optional)
    Cc: (optional)
    Bcc: (optional)
    
    Your message
    Send a message to your self and try :o)

    Best wishes, SRS

  4. #4
    Join Date
    Jul 2005
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    mail -s 'Subject' info@company.com < msg.txt
    would also work in a bash script.

  5. #5
    Mr Tux Guest
    Code:
    [MrTux@localhost ~]$ /home/MrTux/dead.letter... Saved message in /home/MrTux/dead.letter
    What have I missed ?

    I want to send to the Internetemailadress but i only get a 'dead.letter'

    I have sendmail installed but is there something to do with that before its working ?

  6. #6
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    10,351
    Mentioned
    131 Post(s)
    Tagged
    0 Thread(s)
    The problem with the standard 'mail' command is that it assumes that the machine it is running on is a full-fledged SMTP server. So unless you've configured your machine to be a bonafide mail server (or to act as an SMTP relay), then your mail will not go anywhere outside your machine (i.e. it will only work for addresses local to your machine, e.g. some_user@localhost). There is no way to specify an external SMTP server (like your ISP's) to use with the 'mail' command. That's why you're getting a dead letter.

    If you want a command-line mailer that does support using an external SMTP server, then get nail from Fedora Extras. Nail supports specifying an external SMTP server. You can do this on a per-mail basis, like this:

    nail -r "myaddress@something.com" -s "Some subject" -S smtp=some.smtp.server info@company.com < msg.txt

    or 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

    See the nail man page for more details. In my opinion, no one should be using 'mail' anymore, nail is vastly superior.

  7. #7
    Join Date
    Dec 2006
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For simple mails, an alternative is to use Python. nail is better than my very simple Python script put together before bed time, though :o)
    Code:
    #!/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()
    Best wishes, SRS

  8. #8
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    10,351
    Mentioned
    131 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by srs
    For simple mails, an alternative is to use Python. nail is better than my very simple Python script put together before bed time, though :o)
    Well, nail has a lot of options, like the ability to send attachments easily, so I'd agree that nail is better.

  9. #9
    Mr Tux Guest
    Thanks alot for the information.
    I skip Mail then and install nail instead.

    Update info from Internet:
    As of March 2006, nail has been integrated into the Heirloom project as mailx.
    http://heirloom.sourceforge.net/mailx.html

    Thanx

    Mr Tux
    Last edited by Mr Tux; 7th January 2007 at 02:07 PM.

  10. #10
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    10,351
    Mentioned
    131 Post(s)
    Tagged
    0 Thread(s)

    Re: How to use Mail in Bash ?

    In Fedora 13 the old mail command has been replaced by nail, which is now called Mailx, and /bin/mail is just a link to /bin/mailx. So now mail works as users expect it to.

Similar Threads

  1. Send e-mail from a bash script
    By BlueAxem in forum Using Fedora
    Replies: 4
    Last Post: 27th February 2008, 11:16 PM
  2. Using 'mail' in bash....
    By austinwilde in forum Using Fedora
    Replies: 0
    Last Post: 30th June 2006, 01:36 AM
  3. i like bash i want a mail list for it!
    By sunny_mm in forum Using Fedora
    Replies: 1
    Last Post: 28th March 2006, 07:36 AM
  4. Get bash completion working on fedora 3 (bash 3.0)
    By madcat in forum Guides & Solutions (Not For Questions)
    Replies: 2
    Last Post: 27th November 2004, 12:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •