How can I send an email from the command line?

Hal Burgiss hal at burgiss.net
Sun Mar 2 15:02:34 UTC 2008


On Sat, Mar 01, 2008 at 08:27:53PM -0600, Dan Farrell wrote:
> 
> if you apt-get install mailx, chances are you'll be able to use the
> mail command as such:
> $ mail dan at spore.ath.cx
> and type the subject, mail, then a . on a line by itself to terminate
> the message.  if you are really hard core, you can talk directly to the
> recipient's smtp server.  I emailed you an example.  (using that
> technique! :) ) 

>From within a script, it would be easier to something like ...


  echo "this is a test" | mail -s "My Subject" to at example.com

Or you can 'cat' a file to the mail command in the same manner.

Alternately, if there is a MTA installed (eg sendmail, or postifx),
this would work without a mailx package installed:

    ( echo "From: $from"
       echo "To: $to"
       echo "Subject: $subject"
       ! [ "$replyto" == "" ] && echo "Reply-To: $replyto"
       ! [ "$cc" == "" ] && echo "Cc: $cc" 
       ! [ "$bcc" == "" ] && echo "Bcc: $bcc" 
       ! [ "$x_lines" == "" ] && echo "$x_lines"
       if $HTML; then
            echo  "MIME-Version: 1.0"
            echo "Content-Type: text/html; charset=\"ISO-8859-1\""
       fi
       # Add required blank line to separate header/body.
       echo
       # Body goes now which we pull out of a saved textfile ...
       cat $mail_body_file 
     ) | /usr/sbin/sendmail -oi -t


And, mutt also has a commandline interface, and can do things like
send attachments (IIRC).

-- 
Hal
 




More information about the ubuntu-users mailing list