https://tecadmin.net/ways-to-send-email-from-linux-command-line/

We all know the importance of emails these days for information transfer. There are many free emails service providers which we used for use like Gmail, Yahoo, RediffMail etc, which provides a web interface for sending and receiving emails. But this is not enough, sometimes we also required to send emails from system command line. This tutorial will provide you the ways to send emails from Linux command line. This is useful for sending email through our shell scripts, cron jobs etc.

There are various ways to send emails from the command line but here I am sharing few options used by most users. You can use anyone option given below to send email from Linux command line.


1. Using ‘sendmail’ Command

Sendmail is a most popular SMTP server used in most of Linux/Unix distribution. Sendmail allows sending email from command line. Use below instructions to send email using ‘sendmail‘ command.

Create a file using following content.

[root@tecadmin ~]# cat /tmp/email.txt

Subject: Terminal Email Send

Email Content line 1
Email Content line 2

Subject: line will be used as subject for email.

Now send email using the following command.

[root@tecadmin ~]# sendmail user@example.com  < /tmp/email.txt

Read more: Install and Configure Sendmail on CentOS/RHEL


2. Using ‘mail’ Command

mail command is most popular command to send emails from Linux terminal. Use few of below examples to send an email.

[root@tecadmin ~]# mail -s "Test Subject" user@example.com < /dev/null

-s is used for defining subject for email.

Also, you can send the email with an attachment.

# mail -a /opt/backup.sql -s "Backup File" user@example.com < /dev/null

You may face issue: Bash: mail: command not found

-a is used for attachments

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 is basically used for reading mails from Linux terminal from local user mail boxes, also useful to read emails from POP/IMAP servers. Mutt command is little similar to mail command. Use few of below examples to send email.

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

Send email including attachment

# mutt  -s "Test Email" -a /opt/backup.sql user@example.com < /dev/null

4. Using 'SSMTP' Command

sSMTP allows users to send emails from SMTP server from Linux command line. For example to send an email to user admin@example.com use following command. Now type your subject of the email as below with keyword Subject. After that type your message to be sent to the user, After finishing your message press CTRL+d (^d) to send the email.

# ssmtp admin@example.com
Subject: Test SSMTP Email
Email send test using SSMTP
via SMTP server.
^d

Read more: How to Setup SSMTP Server on Linux


5. Using 'telnet' Command

As my experience all system administrators use telnet command to test remote port connectivity test or login to server remotely. Most of newbie in Linux doesn't know that we can send email using telnet also, which is better way to trouble shoot email sending problems. Below is an example of email sending.

Red marked text is the user input and remaining is the responses of that commands.

# telnet localhost smtp

Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 fbreveal.com ESMTP Sendmail 8.13.8/8.13.8; Tue, 22 Oct 2013 05:05:59 -0400
HELO yahoo.com
250 tecadmin.net Hello tecadmin.net [127.0.0.1], pleased to meet you
mail from: sender@tecadmin.net
250 2.1.0 sender@tecadmin.net... Sender ok
rcpt to: myemail@ymail.com
250 2.1.5 myemail@ymail.com... Recipient ok
data
354 Enter mail, end with "." on a line by itself
Hey
This is test email only Thanks
.
250 2.0.0 r9M95xgc014513 Message accepted for delivery
quit
221 2.0.0 fbreveal.com closing connection
Connection closed by foreign host.

Thank you for using this article. We will add more ways soon with this list. We also request you to help me with more commands which you know and not listed above.

http://blog.csdn.net/ithomer/article/details/6776067

linux shell 发送email 邮件

sendmail -s 'Test mail'  toywei@dingtalk.com < mail.m

5 Ways to Send Email From Linux Command Line的更多相关文章

  1. 10 Interesting Linux Command Line Tricks and Tips Worth Knowing

    I passionately enjoy working with commands as they offer more control over a Linux system than GUIs( ...

  2. Linux Command Line Basics

    Most of this note comes from the Beginning the Linux Command Line, Second Edition by Sander van Vugt ...

  3. 15 Examples To Master Linux Command Line History

    When you are using Linux command line frequently, using the history effectively can be a major produ ...

  4. [笔记]The Linux command line

    Notes on The Linux Command Line (by W. E. Shotts Jr.) edited by Gopher 感觉博客园是不是搞了什么CSS在里头--在博客园显示效果挺 ...

  5. 《The Linux Command Line》 读书笔记04 Linux用户以及权限相关命令

    Linux用户以及权限相关命令 查看身份 id:Display user identity. 这个命令的输出会显示uid,gid和用户所属的组. uid即user ID,这是账户创建时被赋予的. gi ...

  6. 《The Linux Command Line》 读书笔记02 关于命令的命令

    <The Linux Command Line> 读书笔记02 关于命令的命令 命令的四种类型 type type—Indicate how a command name is inter ...

  7. 《The Linux Command Line》 读书笔记01 基本命令介绍

    <The Linux Command Line> 读书笔记01 基本命令介绍 1. What is the Shell? The Shell is a program that takes ...

  8. Linux Command Line 解析

    Linux Command Line 解析 0 处理模型 Linux kernel的启动包括很多组件的初始化和相关配置,这些配置参数一般是通过command line进行配置的.在进行后续分析之前,先 ...

  9. Reso | The Linux Command Line 的中文版

    http://book.haoduoshipin.com/tlcl/book/zh/ 本书是 The Linux Command Line 的中文版, 为大家提供了多种不同的阅读方式. 中英文双语版- ...

随机推荐

  1. 转 Centos下安装apahce的configure: error: APR not found. Please read the documentation解决办法

    转自: http://www.cnblogs.com/Anker/p/3355573.html 今天从Apache官网上http://httpd.apache.org/下载httpd web服务器,由 ...

  2. JQuery 入门学习附常用UI模块

    一.简介 jQuery 库可以通过一行简单的标记被添加到网页中 jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 CSS 操 ...

  3. iphoneXR的tabbar底部图片的适配

    在自定义tabbar的背景图时,发现在ihonexr上有点问题,主要是因为iphoneXr用的是二倍图,但是Xr的tabbar底部高度多了34像素,所以就出现了如下的情况: 解决这个问题,还比较简单, ...

  4. L2-3. 悄悄关注【STL+结构体排序】

    L2-3. 悄悄关注 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 新浪微博上有个“悄悄关注”,一个用户悄悄关注的人,不出现在 ...

  5. IP分段小记

    192.168.0.1 个人电脑:0.2-0.50 硬件开发板:0.51-0.100 机器人工控机:0.101-0.200 激光雷达:192.168.254.51~100 编码器板子:192.168. ...

  6. mac下安装pyQt4

    1.首先安装QT,同时要有gcc 2.然后就是先安装sip,然后安装pyqt4 python configure.py -q /usr/bin/qmake-4.8 -d /Library/Python ...

  7. 利用注解和反射,将Bean枚举字段的值填入相应的字段中,并转化为fastjson返回前台

    需求:需要将枚举类型的字段例如enable(是否启用)转化为enable:1,enableName:是.这种形式返回给前台. 思路:在bean字段上加上枚举类型的注解,通过字段的值和枚举类反射获取枚举 ...

  8. SpringMVC整合MongoDB

    首先,在pom文件中新增spring-data-mongodb的依赖: <dependency> <groupId>org.springframework.data</g ...

  9. P2P技术简介(包括BT软件的分析)(转)

    这是一篇别人发表的论文,里面很全面的解释了P2P技术的实现,以及BT网络中应用P2P技术所设计的原理,并列举BT软件的一些专业名词的定义.由于论文发表的比较早,2005年时还没有DHT技术. (链接: ...

  10. redis容量预估

    2.存储的数据内容:前端系统登录用到的Token,类型:key:string(32),value:string(32)3.业务场景存数据:用户登录验证成功后,ICORE-PAP后台产生Token(st ...