How to send mail in linux

Sending mail in Linux can be done using various commands and tools. Here are a few ways to send mail:

1. Using the mail command

The mail command is a simple way to send an email from the command line. You can use it to send a mail to a specific user or a group of users.

Syntax: mail -s "Subject" [email protected]

Example: mail -s "Hello World" [email protected]

2. Using the sendmail command

The sendmail command is a more powerful way to send mail. It allows you to specify the sender, recipient, subject, and body of the email.

Syntax: sendmail -v [email protected]

Example: sendmail -v -f [email protected] -t "Hello World" [email protected]

3. Using the mailx command

The mailx command is similar to the mail command, but it allows you to specify more options and features.

Syntax: mailx -s "Subject" [email protected]

Example: mailx -s "Hello World" -c "CC: [email protected]" [email protected]

4. Using a mail client

You can also use a mail client like mutt or alpine to send mail. These clients provide a graphical interface to compose and send emails.

Syntax: mutt -s "Subject" [email protected]

Example: alpine -s "Hello World" [email protected]

5. Using a scripting language

You can also use a scripting language like Python or Perl to send mail using the smtplib library.

Example (Python):

import smtplib
from email.mime.text import MIMEText

msg = MIMEText("Hello World")
msg['Subject'] = "Hello World"
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"

server = smtplib.SMTP('smtp.example.com')
server.sendmail('[email protected]', '[email protected]', msg.as_string())
server.quit()

6. Using a mail transfer agent (MTA)

You can also use a mail transfer agent like postfix or sendmail to send mail. These agents provide a way to send mail from your Linux system to a mail server.

Syntax: postfix -q

Example: sendmail -v -f [email protected] -t "Hello World" [email protected]

Note: The above examples assume that you have a mail server set up on your system or that you have a mail account with a mail provider.