Linux Reader 4.0Ext 2/3/4, UFS2, HFS and ReiserFS/4 for Windows
Linux Reader 4.0
Ext 2/3/4, UFS2, HFS and ReiserFS/4 for Windows

How to Use a Shell Script to Send an Email

Email is old, yet is one of the most used means of communication. People can send documents, media, files, etc. via email. Let’s see how you can send an email using shell scripting. However, before an email can go through, you must connect to a mail server.

Here you will find out:

  • how to send an email with a shell script
  • how to use "mail", "sendmail" and "mutt" command
  • when DiskInternals can help you

Are you ready? Let's read!

How can a shell script help you to send an email?

There are quite many ways to send emails if you’re running Linux. You can send an email from the command line or using a shell script. It is possible to create multiple automated scripts and schedule when they should run using the crontab command.

Now, there is no possible way to monitor if the automated scripts execute successfully or not. Thus, most people add the email command so as to receive emails about the status of their automated scripts.

A simple bash script to send an email

Here’s a simple example of a bash script that will send an email message to an identified email address.

In the script above, the address is the default UNIX/Linux hostname used for logging in. Thus, it serves as the sender’s address. For example, if you logged in with "example.com," and then you specified “subject” in the FROM_ADDRESS: line, the final sender’s address will be subject@example.com.

This is the actual code to send this email:

Where –s is used to specify the mail’s subject.

How to attach files using a “mail” command

It is possible to include attachments in your email. Here’s the command to add attachment(s) to an email message.

-a is used for mailx and -A for mailutils (for Debian-based systems)

Example:

Example:

Above, the script contains up to three other email addresses that the message should also be sent to. So, you have to set these other email addresses for “carbon copying”.

Use “sendmail” command

Another way to send email messages on a Linux system is by using the “sendmail” command. This is one of the most common email-sending commands used by Linux system operators. However, you will have to install the “sendmail” program to be able to run this command.

Install with this command: $ Sudo apt install ssmtp

After installation, you can now send the email using the command below:

Here, the email message is saved in a file named “mail.txt.” The email message already contains the email subject and body.

Use “mutt” command to send email

Using “mutt” command, you can send an email on a Linux system. But “mutt” is used mainly to read emails on a terminal from local user mailboxes, POP, or IMAP servers. To install “mutt”, run this command:

Example 1:

Example 2:

If you wish to attach a file, then add the –a variable:

Open Linux files in Windows

Some people don't know it is possible to access files on a Linux partition when operating Windows. Using DiskInternals Linux Reader, you can not only view all your Linux partitions, but you can also access the files saved inside. It provides access to different file systems Ext2/3/4, ReiserFS, Reiser4, HFS, HFS+, FAT, exFAT, NTFS, ReFS, UFS2, RomFS(reader), ZFS (preview only*), XFS (preview only*), Hikvision NAS and DVR (preview only*)). DiskInternals Linux Reader is a handy software tool with a simple interface that is straightforward to use.

With DiskInternals Linux Reader installed on your dual-boot PC or virtual machine, it is very easy to access your Linux files at any time.


FREE DOWNLOADVer 4.0, Win Upgrade to PROFrom $29.95

2.1855640411377