sending mail from linux commad line

23,049

Solution 1

I would advise to use sendEmail:

sendEmail-1.56 by Brandon Zehm <[email protected]>

Synopsis:  sendEmail -f ADDRESS [options]

  Required:
    -f ADDRESS                from (sender) email address
    * At least one recipient required via -t, -cc, or -bcc
    * Message body required via -m, STDIN, or -o message-file=FILE

  Common:
    -t ADDRESS [ADDR ...]     to email address(es)
    -u SUBJECT                message subject
    -m MESSAGE                message body
    -s SERVER[:PORT]          smtp mail relay, default is localhost:25

  Optional:
    -a   FILE [FILE ...]      file attachment(s)
    -cc  ADDRESS [ADDR ...]   cc  email address(es)
    -bcc ADDRESS [ADDR ...]   bcc email address(es)
    -xu  USERNAME             username for SMTP authentication
    -xp  PASSWORD             password for SMTP authentication

  Paranormal:
    -b BINDADDR[:PORT]        local host bind address
    -l LOGFILE                log to the specified file
    -v                        verbosity, use multiple times for greater effect
    -q                        be quiet (i.e. no STDOUT output)
    -o NAME=VALUE             advanced options, for details try: --help misc
        -o message-content-type=<auto|text|html>
        -o message-file=FILE         -o message-format=raw
        -o message-header=HEADER     -o message-charset=CHARSET
        -o reply-to=ADDRESS          -o timeout=SECONDS
        -o username=USERNAME         -o password=PASSWORD
        -o tls=<auto|yes|no>         -o fqdn=FQDN


  Help:
    --help                    the helpful overview you're reading now
    --help addressing         explain addressing and related options
    --help message            explain message body input and related options
    --help networking         explain -s, -b, etc
    --help output             explain logging and other output options
    --help misc               explain -o options, TLS, SMTP auth, and more

It works very well for me. Remember to use TLS with gmail. You need to provide details of the server that will send the email with those options:

    -s SERVER[:PORT]          smtp mail relay, default is localhost:25
    -xu  USERNAME             username for SMTP authentication
    -xp  PASSWORD             password for SMTP authentication

It's best for me as it allows to add attachments and can be easily placed in the scripts.

Example usage:

sendEmail -f [email protected] -t [email protected] -s test -m messageBody -s smtp.gmail.com -xu [email protected] -xp xxxxxpass -o tls=auto
Aug 17 16:21:37 z sendEmail[22420]: Email was sent successfully!

Solution 2

The mail terminal program should do the trick. It usually works "straight out of the box" to allow users/programs to send messages locally, inside the system.

Type mail -s 'subject line' [email protected] and hit return. Then type your message and close/send using Ctl-D.

Share:
23,049

Related videos on Youtube

max
Author by

max

working as a linux system admin.

Updated on September 18, 2022

Comments

  • max
    max over 1 year

    Is it possible to send mail from linux terminal to any gmail account.

    If possible then what are the configuration are needed.

    I tried with mailx and sendmail but its not working.

    I also tried with mutt like this

    echo "test" | mutt -s this-is-my-subjest [email protected]

    but no use...

    I am using CentOS 6.2

    • Emil Vikström
      Emil Vikström over 11 years
      What is not working? Do you get an error message? Have you checked any log files?
    • Emil Vikström
      Emil Vikström over 11 years
      You should be able to find a log file in /var/log/. It is often called maillog or similar. What does it say after you try your command?
    • max
      max over 11 years
      nothing, next command prompt will appear.
    • max
      max over 11 years
      in var/log/maillog its showing Network is unreachable
    • Cameron Mark Lewis
      Cameron Mark Lewis about 9 years
      Pulling from a file is effective: echo "email from serverX" > ~/testemail; then: mail -s 'another test subject' [email protected] < ~/testemail
  • max
    max over 11 years
    can u provide one simple example?
  • mnmnc
    mnmnc over 11 years
    sure i will place it in the answer at the bottom
  • max
    max over 11 years
    I tried this but not working... any configuration are needed, before doing this?
  • danielcraigie
    danielcraigie over 11 years
    Can you try sending an email to yourself on the machine: mail -d -s subject your name@localhost. The -d option will provide debugging output to tell you what is causing the problem.
  • mnmnc
    mnmnc over 11 years
    possibly. but what you probably missed - i'm proposing send-E-mail. not sendmail. i've never had a chance to use sendmail but sendEmail does everything i need.
  • max
    max over 11 years
    no sendEmail command installed, so I tried yum install sendEmail but its giving No package sendEmail available
  • mnmnc
    mnmnc over 11 years
    download link : caspian.dotconf.net/menu/Software/SendEmail/… unzip it, then ./configure ./make ./make install
  • Kevin Versfeld
    Kevin Versfeld over 11 years
    On some systems you have to use mailx instead of mail.