§2024-04-26

alexlai@h2Jammy:~$ mail -s "test" alexlai@h2jammy.yushei.net
Cc: rai.sousuke@mac.com
Bcc: rai.sousuke@gmail.com
Subject: testing mail command
mail --> read from ~/mbox, after read mail willl be moved to /var/mailbox/alexlai   <--- wrong!!!!
.
quit
 
alexlai@h2Jammy:~$ mail
"/var/mail/alexlai": 1 message 1 new
>N   1 AlexLai            五  四  26 10:  29/1189  test
? 1
Return-Path: <alexlai@h2Jammy.yushei.net>
X-Original-To: alexlai@h2jammy.yushei.net
Delivered-To: alexlai@h2jammy.yushei.net
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=h2jammy.yushei.net;
        s=mail; t=1714098652;
        bh=Bc6nIJYxr86J+aIONTM3Azgxu0HQ0Hij/22aZC3QNLc=;
        h=Subject:To:Cc:Date:From:From;
        b=taIg1Rw54hXVnJMyB2JDYfFEryeAW2CWzXCZQZyJDrFcq4WqhJLN8GZZZDgpmnb6p
         NheWMCO3OPujAoIOJX8DGFH9S2czFMQy20EIkytIZ7U4LloIWblxebj2BRy8dyDRpq
         LcijoH3kKkQ7BBF5iSWFxjG7YCQwZIC0zj2amilPOwv3HhBTcGiMJTpv+OHXeMucZS
         2Ey3cQ7cbYLb48oDTWCMP015vMIm2H31QB/DOshFYbvnKTH9QwuzDnv6HUNXPnTKn1
         5eNpNLk7I9fxHUFfc7/Q7BXFgdL3/+SRMy2qLRWJfDqsPO6xG6INK0bO+fCGsDqThl
         o89Ip8Nw45nZA==
Received: by h2Jammy.yushei.net (Postfix, from userid 1026)
        id 22EB229649258; Fri, 26 Apr 2024 10:30:52 +0800 (CST)
Subject: test
To: <alexlai@h2jammy.yushei.net>
Cc: <rai.sousuke@mac.com>
User-Agent: mail (GNU Mailutils 3.14)
Date: Fri, 26 Apr 2024 10:30:52 +0800
Message-Id: <20240426023052.22EB229649258@h2Jammy.yushei.net>
From: AlexLai <alexlai@h2Jammy.yushei.net>

Bcc: rai.sousuke@gmail.com
Subject: testing mail command
mail --> read from ~/mbox, after read mail willl be moved to /var/mailboax/alexlai
.
quit

? q
Saved 1 message in /home/alexlai/mbox
Held 0 messages in /var/mail/alexlai
alexlai@h2Jammy:~$ mail
No mail for alexlai
alexlai@h2Jammy:~$ mail -f
"/home/alexlai/mbox": 1 message 1 new
>N   1 AlexLai            五  四  26 10:  32/1252  test
? q
Held 1 message in /home/alexlai/mbox```

---

- [Send Email in Linux from Command Line@digitalocean](https://www.digitalocean.com/community/tutorials/send-email-linux-command-line) Published on August 4, 2022

1. Using mail Command
Linux mail command is quite popular and is commonly used to send emails from the command line. Mail is installed as part of mailutils and mailx packages on Debian and Redhat systems respectively. The two commands process messages on the command line. To install mailutils in Debian and Ubuntu Systems, run:

`$ sudo apt install mailutils -y`

2. Testing Mail command

`$ mail –s "Test Email" james@example.com`

`$ echo "Tesing DKIM Postfix setup" | mail -s "Test" alexlai@h2jammy.yushei.net`

alexlai@h2Jammy:$ echo "Tesing DKIM Postfix setup" | mail -s "Test" alexlai@h2jammy.yushei.net alexlai@h2Jammy:$ mail "/var/mail/alexlai": 1 message 1 new

N 1 AlexLai 五 四 26 09: 24/1039 Test ? 1 Return-Path: alexlai@h2Jammy.yushei.net X-Original-To: alexlai@h2jammy.yushei.net Delivered-To: alexlai@h2jammy.yushei.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=h2jammy.yushei.net; s=mail; t=1714095675; bh=NTjiDrtKycA3pmHrgSpGq30FsTcNQa7UMY9Bo4OoMns=; h=Subject:To:Date:From:From; b=T0AT12KGCtnEaTX51ssPkwuayq/kfqlmpzHYXeZ29xqC/z0qIQ9zAiZvVlcOH+aIQ S0CUd1rk0SgG2fySv+u/jFAMS2xbyVfjsrLRYwVF0AojhTJM7WDd8sq+FjHXj7txdi NDdgcjjcOOUhNOvDYb9syi44vnRG44ctmhe7M6UYZJIcEQAEfo6RDbgrZTNyOTKda5 AfP53L6ipK5329yP7u5ddYktRByEwBlEBsRm+Ob0sBff0K8aywG5hgKsZNcP5YBu8C OtnW+MVj2tAeBTsbwVjZsV1IKCzO0/q87MId4ojIoL8YWJUxRZenM8CO83BFVNdNvD YwRHms/EJmsJg== Received: by h2Jammy.yushei.net (Postfix, from userid 1026) id 5E9FF29649258; Fri, 26 Apr 2024 09:41:15 +0800 (CST) Subject: Test To: alexlai@h2jammy.yushei.net User-Agent: mail (GNU Mailutils 3.14) Date: Fri, 26 Apr 2024 09:41:15 +0800 Message-Id: 20240426014115.5E9FF29649258@h2Jammy.yushei.net From: AlexLai alexlai@h2Jammy.yushei.net

Tesing DKIM Postfix setup


3. [How to Use the Linux mail Command](https://phoenixnap.com/kb/linux-mail-command)

The basic syntax for sending an email using the mail command is:

`mail [ options ] -s [ subject ] [recipient_address]`

- The available [options] are explained in the section below.
    - The -s flag specifies the email subject.
    - The [recipient_address] is the email address/username of the person you are emailing.
    
| Option	| Description |
| :-------- | :---------- |
| -A [file_path]	| Attaches a file to the email. |
| -a=[header]	 | Allows users to append the specified message to the email. It can also be used to send Carbon copies or Blind carbon copies of the email. |
| -d	| Outputs various information useful for debugging. |
| -E	| Prevents sending messages with an empty body. |
| -f	| Use to specify an alternate mailbox. Defaults to the user's mbox if no other file is specified. After quitting, mail writes the undeleted messages back to this file. |
| -I	| Forces the interactive mode, even when input is not a terminal. For example, the special ~ command character is only available in interactive mode. |
| -i	| Causes mail to ignore tty interrupt signals. |
| -N	| Prevents initial message headers when reading mail or editing a mail folder. |
| -n	| Inhibits reading /etc/mail.rc on startup. |
| -s "subject"	| Used to specify the email subject on the command line. Encase the subject in double quotes. |

Linux mail Command Examples
In this tutorial, we will send mail to specific users on the system by specifying the username in place of the recipient address. To send mail to external domains, make sure to set up postfix to relay an external SMTP server.

Example 1: Send Mail with Subject and Body
The basic way of using mail to send emails is to specify the email subject using the -s flag and add the recipient. The command then enters the interactive mode, asking if you want to send Ccs and asking for the email body.

After entering all the information, press Ctrl+D to send the email.

For example, the following email is sent from the user 'bosko' to the 'root' user:

Sending an email using the mail command.
After sending the email, the command exits the interactive mode.

Example 2: Send Mail Using echo
Use the echo command to send an email without entering the interactive mode of the mail command. Write the email body and pipe the echo command output to the mail command.

The syntax is:

echo "[email body]" | mail -s "[subject]" [recipient]

For example:

Piping the echo command output to the mail command.
The echo command takes the string, and the pipe (|) passes the string to the mail command.

Example 3: Send Email Using Redirection
Another way to provide the email body is to use redirection (<<<). Using redirection also avoids the mail command interactive mode and sends the email without any prompts.

The syntax is:

mail -s "[subject]" [recipient] <<< "[email body]"

For example:

Passing a string to mail using redirection.
In the above example, we create an email using the mail command and pass the string as the message body using redirection.

Example 4: Send Email from File
The mail command allows users to provide the email body from an existing file. This option saves time and facilitates sending large messages without writing them in the command line.

The syntax is:

mail -s "[subject]" [recipient] < [file_path]

For example:

Providing mail command input using a file.
The command takes input from the specified file and sends it as the email body. Providing input from files is useful when calling the mail command from shell scripts or other programs, such as the ones written in Perl or PHP.

Example 5: Send Mail to Multiple Receivers
To send mail to multiple receivers, specify the list of recipients in a comma-separated list. The syntax is:

mail -s "[subject]" [recipient1], [recipient2] <<< "[mail body]"

For example:

Sending mail to multiple recipients in Linux via the CLI.
Alternatively, send a carbon copy by entering the command's interactive mode. To send a blind carbon copy of the email, use the -a (--append) flag. The syntax is:

mail -s "[subject]" --append='BCC:[recipient1],[recipient2]...'

Sending email Cc and Bccs using the mail command. 
First, we specify the subject and the Bcc recipients, and then the command enters interactive mode, asking for the email recipient, Cc recipients, and email body. The difference between Cc and Bcc is that Cc allows all recipients to see who else got the email, which isn't the case with Bcc.

Example 6: Send Attachments
The mail command allows users to send email attachments using the -A flag. Specify the file path to attach the file to the email. Note that the file is base64 encoded before sending and that the recipient must decode it upon receiving.

The syntax is:

mail -s "[subject]" -A [file_path]

Sending an email attachment via the command line.
Press Ctrl+D to send the mail with the encoded attachment. Refer to the section below on saving attachments to see the result.