Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an email using sendmail command in linux [closed]

I tried the below two commands.

  1. From: mail_id
    To: Recipient_mail_id
    Hi, this is my message, and I'm sending it to you!
    .
    
  2. echo "My message" | sendmail -s subject Recipient_mail_id
    

But didn't get any mail to the recipient's mail address.

SMTP server is installed on another server and it is up and running. So can anyone help me out on how to send a test email through that SMTP server using sendmail or smtp commands?

like image 782
Gayathri K Avatar asked Sep 07 '25 06:09

Gayathri K


2 Answers

Using sendmail command

Created a file with email content:

$ cat /tem/email.txt
Subject: Terminal Email Send

Email Content line 1
Email Content line 2

Now send email using the following command:

$ sendmail [email protected] < /tem/email.txt

Using mail command

$ mail -s "Test Subject" [email protected] < /dev/null

Also, you can send an attachment with this command. Use -a for mailx and -A for mailutils.

$ mail -a /opt/file.sql -s "Backup File" [email protected] < /dev/null

Also, we can add comma separated emails to send the email to multiple recipients together.

$ mail -s "Test Email" [email protected],[email protected] < /dev/null

Using mutt command

$ mutt -s "Test Email" [email protected] < /dev/null

Send an email including an attachment:

$ mutt -s "Test Email" -a /opt/backup.sql -- [email protected] < /dev/null
like image 95
Kundan Singh Avatar answered Sep 10 '25 09:09

Kundan Singh


MTAs (Message Transfer Agents) like sendmail expect email messages in Internet Message Format. Usually it is better to use a MUA (Message User Agent) (e.g. mail), unless the message is trivially simple.

A "sendmail look alike" command is provided also by MTA/SMTP servers (postfix/exim/…) and programs like msmtp. Basic sendmail command line options are the facto standard so it may be a good choice for sending simple emails.

You may try the following shell script

#!/bin/sh

# sendmail command line optons:
# -i - do not treat lines starting with dot specially
# -t - read recipients lists from message headers: TO,CC,BCC
# -v - use verbose mode (describe what is happening) 
#
# The empty line separates mail headers from mail body.
# Without -t recipients should be listed after command line options.

FROM='[email protected]'
TO='[email protected]' 
/usr/sbin/sendmail -i -t << MESSAGE_END
To: ${TO}
From: ${FROM}
Subject: Example Subject

Hi, this is my message, 
and I'm sending it to you! 
MESSAGE_END

WARNING:
Sending text emails with non US-ASCII characters in email body requires extra headers (MIME). Another special encoding is required for non US-ASCII characters in email headers. See RFC 2045 and related for further details.

like image 30
AnFi Avatar answered Sep 10 '25 09:09

AnFi