What is mailx in linux

Linux mail and mailx Commands Tutorial With Examples and Send Email From Command Line

Linux has a lot of tools, services, and applications related to email. An email has a different architecture than standard client-server. We will look at a command-line based mailing application named mailx. mailx more advanced version of the mail tool. mailx command supports the MIME, IMAP, POP3, SMTP, and S/MIME protocols and based Berkeley Mail 8.1 mail command.

General Concept

As stated in previous paragraph email systems are bit more complex than standard client server architecture. There are some terms we should learn before continue

  • Mail Client is an application used by user directly to send and get emails. Mail clients connects to the mail transfer agent to transmit emails.
  • Mail Transfer Agent (MTA) is an intermedia component used to receive, send, store mail messages. there are some protocol used to accomplish these tasks like smtp,pop3,imap etc.

Below are some architectural view of a simple email transmission.

Install mailx Command

Linux distributions provides two mailx command one from the mailutils package which is installed by default and the other one is from the bsd-mailx package. In order to work properly we should install the bsd-mailx package like below.

Brief help information about the mailx command can be printed with the —help option like below.

Send Email

We will simply mail some text without provide extra information. This is the fastest way to mail also. In this tutorial we will simply use local mail provider but in the SMTP configuration section we will deal with a SMTP server. In this example we will provide subject part of the mail with -s option and we will also provide email address which is the local user named ismail

There is a warning which says Null message body; hope that’s ok . We think it is ok.

Read Email

In previous example we have send mail to local system user ismail . When user ismail logins to the system he will get a message saying he have an email message. This email can be read from /var/mail/ismail where ismail is the users name with simple cat command.

Read Email Body From File

While sending email there will be body part of the mail. This body part can be written by hand. But if it is long and repetitive task we do not want to do always there is an alternative. Body part can be read from a file by simply redirecting the content to the mail command like below. In this example body.txt file contains body part of the mail. This file may also an html file too.

body.txt

There is other way to send mail by redirecting body content with pipe like below.

Set Multiple Recipient

Another useful feature of mail command is providing multiple recipients by simply delimiting recipients emails. In this example we will send email both to the [email protected] and [email protected] . Keep in mind that there may be more than two recipients.

As we can see mail notification is appeared very fast.

Add CC and BCC To The Mail

Carbon Copy — CC and Blind Carbon Copy — BCC are used to send copy of the mail to the other recipients in a visible or hidden way. To provide CC use -c option with email addresses. To provide BCC use -b option with email addresses. In the example we will send mail to the [email protected] with [email protected] in CC and [email protected] in BCC. From this example we will use same options but more featured mailx command.

If we look our mailbox we can see mail and cc but can not see bcc

Specify From Name And Address

As we know we can set sender name and email address explicitly. We will use -r option to set sender name and email address. Specify email address like . In this example we write sender name as İsmail Baydan and sender address as [email protected]

Читайте также:  Windows forms закрыть все формы

The sender address and name can be seen explicitly.

Specify “Reply To” Address

Reply to address is used to set return address for the mail if the receiver wants to write back. This can be especially useful for automated systems where return will be do a specific email address.Reply to information can be set with -S option. In the example we will set reply to address as [email protected]

Add Attachment

Attachments are crusial part of the email. Emai users generally attaches some documents, image, zip file to the emails to send to the receiver. In GUI email clients it can be easy as copy paste or selecting file. But how can we send email attachments in command line interface. Attachments can be added with -a option with the attachements path. In the example we will add file name a.txt to our mail as attachment.

Use External SMTP Server

Up to now we have used the the local mail system. Local mail system is provided as a simple mechanism by Linux operating system. In the real world examples email system generally uses SMTP, POP3, IMAP services. But in order to send emails we need to setup SMTP server for the mail and mailx command. SMTP configuraiont is put into command line and have some text to type.

We will look what this command options and arguments mean.

  • -S smtp=»mail.example.com:587″ specifies the SMTP server host name or IP address and port number which is 587
  • -S smtp-use-starttls specifies to use tls encrytion to make communication encrypted and secure
  • -S smtp-auth=login specifies what type of SMTP authentication will occur.
  • -S smtp-auth-user=»[email protected]» specified the username that will be used to authenticate
  • -S smtp-auth-password=»1q2w3e» specifies the password. Be sure that the system you are using is secure because the clear text password is provided.
  • -S ssl-verify=ignore this is used to ignore ssl verification of SMTP server. This option is used to prevent self signed ssl problems. But this makes the operation less secure.

Debug, Troubleshoot and Verbose Output

The general assumption is that there will be problems where sending emails. So wee need to debug to get details of the problem. For both mail and mailx commands. The -v option can be used to debug the mailing operation.

List Emails with mailx Command From Command Line

We list and read delivered emails with the mailx command like below. The list provides information like sender, date, time, size and the subject part.

Источник

mailx in Linux

Linux has an inbuilt Mail User Agent program called mailx. As the name suggests, it is a console application that is used for sending and receiving emails. The mailx utility is an enhanced version of the mail command. Along with the functionality provided by the original mail command, it provides extra features like the ability to send attachments by using the -a flag. The mailx command is available from a variety of different packages:

  • bsd-mailx
  • heirloom-mailx
  • mailutils

Installing mailx

For Ubuntu/Debian:

For fedore/centos:

Note: Even though the mailx command is a newer version of the original mail utility, it can still be referenced with the ‘mail’ keyword.

Sending an Email

1. Writing the message directly in the command line: To send a simple email, use the “-s” flag to set the subject in quotes which is followed by the email of the receiver. After this, mailx waits for the content of the email. To enter new lines, keep hitting enter. After the content is written, press Ctrl+D & EOT will be displayed by mailx.

2. Taking the message from a file

3. Using pipes

4. Sending same Mail to Multiple Recipients: We can send the same email to multiple receivers (not by cc or bcc) as follows:

5. Adding CC & BCC

We can send a carbon copy (CC) or a blind carbon copy (BCC) to send the same email to multiple recipients (visibly or in a hidden manner). For CC, we use the “-c” option & for BCC we use the “-b” option which is followed by the email addresses.

6. Adding Attachments
Attachments are a vital part of email communication. We can attach documents, images, text files, etc. by using the “-a” option followed by the path of the file that we want to attach.

Источник

9 mail/mailx command examples to send emails from command line on Linux

Send mails from command line

The mail command is an essential one that should be available on any linux server so that various services and other web applications can generate and transmit emails.

Читайте также:  Как поставить русскую раскладку kali linux

In a previous post on mail command we saw how the mail command can be used to send emails from the command line on your linux server.

In this tutorial we shall be using an enhanced version of the mail command. Its called mailx (or just mail when installed), and it can do many more things than what the older mail command from gnu mailutils package can do.

How does it work

The mail/mailx command needs a local smtp server (MTA) running in order to deliver the emails. THe route taken by the email is somewhat like this —

The recipient MTA would be gmail’s smtp server if your recipient is someone at gmail.com for instance. For the local MTA, you need to install an smtp server like Postfix. A basic installation of Postfix with minimal configuration would work in most cases.

Install the mailx command

On Ubuntu/Debian based systems the mailx command is available from 2 different packages —

We shall be using the heirloom-mailx package because it has more features and options.
On CentOS/Fedora based systems, there is only one package named «mailx» which is the heirloom package.

To find out what mailx package is installed on your system, check the «man mailx» output and scroll down to the end and you should see some useful information.

Using the mailx command

Once installed, the mailx command can be directly referenced with the name mail, so you just type in that in the command line.

1. Simple mail

Run the following command, and then mailx would wait for you to enter the message of the email. You can hit enter for new lines. When done typing the message, press Ctrl+D and mailx would display EOT.

After than mailx automatically delivers the email to the destination.

2. Take message from a file

The message body of the email can be taken from a file as well.

The message can also be piped using the echo command —

3. Multiple recipients

To send the mail to multiple recipients, specify all the emails separated by a comma

4. CC and BCC

The «-c» and «-b» options can be used to add CC and BCC addresses respectively.

5. Specify From name and address

To specify a «FROM» name and address, use the «-r» option. The name should be followed by the address wrapped in «<>«.

6. Specify «Reply-To» address

The reply to address is set with the internal option variable «replyto» using the «-S» option.

7. Attachments

Attachments can be added with the «-a» option.

8. Use external SMTP server

This is an exclusive feature, that you get only with heirloom mailx and not bsd mailx, or the mail command from gnu mailutils or the mutt command.

The mailx command can use an external smtp server to use to relay the message forward. The syntax is a bit lengthy but makes sense.

Here is a breakdown

You can use the gmail smtp servers and send emails via your gmail account. That is so cool!
For gmail specifically you would need to enable less secure apps settings before you can send mail like that.

9. Verbose — watch smtp communication

When using external smtp servers, you can choose to watch the entire smtp communication that is done in the background. This is useful specially when testing or debugging smtp servers.

Troubleshooting

In case the mails are not being delivered properly you need to check a few things. The first thing to check is that an smtp server (mta) is running locally. The netstat command can tell that

If an stmp server like Postfix is running and still mails are not going, then try re-configuring Postfix for example. On Ubuntu/Debian systems, this can be done with the dpkg-reconfigure command

Then retry, the mail command and it should work. If it still doesn’t, try contacting your server provider.

No mails from local systems

If you try to send mails from your local computer to a gmail address, your mail would most likely be rejected, so don’t try doing that.

This is because ordinary computers connected to internet address have an ip address that is not associated with any valid domain as such, and gmail strictly verifies such credentials before approving any mail to go through.

Notes and Resources

Apart from mailx, there are other tools like Swaks and smtp-cli that can be used to send mails from command line and support various features like specifying smtp servers and adding attachments and so on.

However the mailx command is available in the default repositories of most common distros, so can be installed easily. Further it maintains a syntax very similar to that of the mail command which makes it a drop in replacement for the older mail command.

Читайте также:  Windows 10 все версии чем отличаются

The mailx command is even capable of reading mails from remote IMAP servers, but that is something we kept out of this post and would talk later. To learn more check the man page for the mailx command with «man mailx».

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

14 thoughts on “ 9 mail/mailx command examples to send emails from command line on Linux ”

might be worth pointing out that most of this won’t work on a lot of systems,
illegal option — S
illegal option — a
illegal option — r

You don’t detail how mailx determines the smtp envelope from address
>>> MAIL FROM:
Obviously it’s [email protected], but how can this be over-ridden with mailx ?

Hello.
I want my printer to send emails to external email addresses like Gmail.com, I setup the ubuntu server as explained and set the ubuntu server as the smtp server on printer. I can send emails from ubuntu server to gmail, but it’s not relaying emails to gmail.com from the printer itself. What should I do? Please help.

I would like to know, whether we have option to send the attachment with password protected through mailx UNIX command? can anyone help me on this.

I have a cron job like this:
tar -czf /home/backup/myfolder/myfile_$(/bin/date +\%Y\%m\%d).tgz /home/public_html -X /home/backup/myfile-exclude.txt

I have edit my cron job to this:
tar -czf /home/backup/myfolder/myfile_$(/bin/date +\%Y\%m\%d).tgz /home/public_html -X /home/backup/myfile-exclude.txt | mail -s “Cron Daemon: mywebsite.com – files backup” [email protected]

Normally the Cron job puts some info into the Body, but how do I get this to work? I have tried with:

I am using attachment command in phone script but it is not working whether if the same command runing on emulator

You could setup home computer as well but it needs to integrate with the from address mail domain SMTP relays to send emails.

I have followd your “tuto” ( use Centos7 )

I still have this error ?? can you help me ?

Connecting to 82.216.111.2:25 . . . connected.

220 smtp3.tech.numericable.fr ESMTP Postfix
>>> EHLO centos7
250-smtp3.tech.numericable.fr
250-PIPELINING
250-SIZE 10240000
250-ETRN
250-ENHANCEDSTATUSCODES
250 8BITMIME
>>> AUTH LOGIN
503 5.5.1 Error: authentication not enabled
smtp-server: 503 5.5.1 Error: authentication not enabled

my mailrc file ;
#set smtp-use-starttls
set nss-config-dir=

/.mozilla/firefox/yyyyyyyy.default/
set ssl-verify=ignore
set smtpd_sasl_auth_enable
# set smtp
set smtp=smtp://smtp.numericable.fr:25
set [email protected]
# tell mailx that it needs to authorise
set smtp-auth=login
# set the user for SMTP
# set [email protected]
set [email protected]
# set the password for authorisation
set smtp-auth-password=xxxxxx
bye.

Hi,
I am not able to use FROM option using -r, specified in 5th point.

Good info. Just to clarify, sendmail/postfix/etc are MTAs and either relay through an smtp relay or directly communicates to the destination smtp servers, depending on how the MTA is configured. So the flow would be mail_client > mta > smtp_relay OR destination_smtp_server > recipient’s inbox. So using common programs as an example: mailx > postfix > smtp.yourorg.com > smtp.gmail.com > us [email protected] inbox. A common example where this configuration is used is Linux servers that need to send to external email recipients (alerts and stuff). In that case, you configure the Linux server to use your outgoing smtp server via sendmail or postfix or other MTA to relay the email by allowing the IP of the Linux server to relay through the smtp server.

Thank you for your post. Can I use Mailx for send up to 15000 mails ? I’m searching a alternative to online newsletter service (payment)!

I hope your ISP blocks you.

Yes, you can use Mailx or any other scripts to send any volume of emails provided that your domain SMTP relay is capable to handle volume. Do you run your own mail server or using one of the shared hosting? If you run yours … yes you could send any volume but may take few hours to send. You can’t send out emails just using home computer as those will not be delivered to end users or will directly go in users Spam folder. If you need to use home computer … you would need to hop / forward your emails to your domain SMTP relay of “from user” to send the emails to users. Reach-out if you need professional help for setting up mail server to handle your newsletter … you would need one dedicated public IPAddress on a Linux server. I am assuming that these are registered users for business and you are authorized to send them newsletter.

Источник

Оцените статью