28

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?

2
  • SO is a programming Q&A platform and this question is not about programming. Questions about operating systems, their utilities, networking and hardware, are off topic here. What topics can I ask about here?. Please delete this and ask, instead, on Unix & Linux Stack Exchange or superuser.com Commented Jul 9, 2023 at 13:30
  • Generally when using sendmail (or whatever it actually is), it's a good idea to examine the log files (e.g. /var/log/mail in many cases). Also learn about the format of E-Mail messages ("RFC 822" should get you started). Commented May 29, 2024 at 9:59

3 Answers 3

27

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
Sign up to request clarification or add additional context in comments.

4 Comments

I want to use host name and port of the SMTP server in the command. How to use that?
This will help you: echo "This is the message body and contains the message" | mailx -v -r "[email protected]" -s "This is the subject" -S smtp="mail.example.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="[email protected]" -S smtp-auth-password="abc123" -S ssl-verify=ignore [email protected]
A minimal message should also include header fields to, from, date and preferably a message-id. Many mailers add those, but they have to guess.
There are multiple mail implementations, with different options. You need to know which one you are using (read its man page) before you blindly plunge ahead with the code from this answer.
12

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.

2 Comments

doesn't work on macos
@Vaughn What does fail?
6

I'm surprised nobody has mentioned it here but I've been using swaks for this for years. I only came here because I had forgotten the name and expected to find it recommended.

Swaks is in debian and has a home page here.

I normally use it like.

swaks --from $EMAIL --to $TESTADDR --server $TESTSERVER

It has options also to test auth etc, so is quite handy in more complex situations too.

2 Comments

Thank you for sharing this insight and the swaks. It helped in my case.
apt install swaks and everything works. Great.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.