9

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!
.
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?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Gayathri K
  • 441
  • 2
  • 4
  • 11
  • 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?](https://stackoverflow.com/help/on-topic). Please delete this and ask, instead, on [Unix & Linux Stack Exchange](https://unix.stackexchange.com/) or https://superuser.com/ – Rob Jul 09 '23 at 13:30

2 Answers2

13

1. 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 user@example.com < /tem/email.txt

2. Using mail command:

$ mail -s "Test Subject" user@example.com < /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" user@example.com < /dev/null

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

$ mail -s "Test Email" user@example.com,user2@example.com < /dev/null

3. Using mutt command:

$ mutt -s "Test Email" user@example.com < /dev/null

Send an email including an attachment

$ mutt -s "Test Email" -a /opt/backup.sql user@example.com < /dev/null
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Kundan Singh
  • 131
  • 4
  • I want to use host name and port of the SMTP server in the command. How to use that? – Gayathri K Aug 08 '19 at 12:18
  • This will help you: echo "This is the message body and contains the message" | mailx -v -r "someone@example.com" -s "This is the subject" -S smtp="mail.example.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="someone@example.com" -S smtp-auth-password="abc123" -S ssl-verify=ignore yourfriend@gmail.com – Kundan Singh Aug 08 '19 at 13:29
6

sendmail expects email in "raw" format. Usually it is better to use higher level commands (e.g. mail).
"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

/usr/sbin/sendmail -i -t << MESSAGE_END
From: john.doe@examle.net 
To: jane.doe@example.com

Hi, this is my message, 
and I'm sending it to you! 
MESSAGE_END
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
AnFi
  • 10,493
  • 3
  • 23
  • 47