Ubuntu 终端发邮件 Send Email from the command line in Ubuntu

Send Email from the command line in Ubuntu

To have the ability to send email from the command line, you will need to install the mailutils and postfix packages with the following commands.
apt-get install mailutils
apt-get install postfix
 
Now configure Postfix by running the following command.
dpkg-reconfigure postfix
 
There will be several options on the main screen. For me, I selected "Internet Site" and used all of the default settings. You may want to play around with other options such as "Internet with smarthost."

After configuring Postfix, test out the new configuration with the following command.
// no subject
echo "test" | mail [email protected]

// method one
echo "here is text content " | mail -s "here is subject" [email protected]

// method two
mail -s "here is subject" [email protected] < here_is_text_content.text
 

from: http://tsengf.blogspot.com/2012/01/send-email-from-command-line-in-ubuntu.html

mail

NAME(名称)

mail - 发送和接收邮件

 

SYNOPSIS(总览)

mail [- iInv ] [- s subject ] [- c cc-addr ] [- b bcc-addr ] to-addr... 
mail [- iInNv - f ] [ name ] 
mail [- iInNv [- u user ] ][[]]

 

INTRODUCTION(介绍)

Mail 是一个智能化的邮件处理系统,它具有 ed1 的命令语法回忆功能,只是用消息替换了行罢了.

 

-v
详尽模式. 传输邮件的细节都呈现在用户的终端上.
-i
忽略tty中断信号. 这对于在嘈杂的电话线路上使用   mail  特别有用.
-I
强迫mail以交互模式运行,即使其不是通过终端输入的. 特别地,正在发送邮件时, ` ~  ' 特殊字符只在交互模式下才起作用.
-n
禁止在启动时读取 /etc/mail.rc
-N
当阅读邮件或编辑邮件夹时禁止消息头的初始化显示.
-s
在命令行上指定主题(仅把 - s  标识后的第一个参数用作主题;注意对包含空格的主题加上引号.)
-c
发送复件(carbon copy)给用户   list
-b
发送隐藏的复写副本(blind carbon copy)给用户   list  list应为以逗号分隔的名字列表.
-f
读入你的   mbox  (或指定文件) 中的内容进行处理;当你   quit  时,   mail  会把未删除的消息写回该文件.
-u
相当于:

 

mail -f /var/spool/mail/user

 实例:

[root@linux ~]# mail -s 'test title' -c [email protected] < mail.txt   #将mail.txt发给[email protected]

[root@linux ~]# mail [email protected]
Subject: nihao!    #邮件主题
nihao!  
linux 在线手册   #邮件内容Crl+D 完成内容输入

Cc: [email protected]

来源:http://l.51yip.com/search/mail

ubuntu安装postfix

postfix是一个smtp邮件服务器

1 为什么选择postfix

流行的smtp服务器还有sendmail,就个人感觉,sendmail的配置方式十分不友好,且据说性能和安全性都稍差一些。而postfix就是为了解决sendmail的一些问题而创建的。目前已经成为了非常流行的smtp服务器。

2 安装postfix

sudo apt-get install postfix

即可。

另外,postfix使用了syslog进行日志记录,然而ubuntu默认并没有安装,如果你是第一次使用postfix,查看log是非常重要 的,因此一定要装上syslog,安装之后,无需任何配置,即可在 /var/log/ 中查看到几个 mail 开头的log文件。

sudo apt-get install rsyslog
 

另外,postfix也使用了第三方模块cyrus-sasl实现smtp的验证服务,我并没有相关的需求,因此在此不再赘述,有类似需求的同学可参考其他文档。

3 配置postfix

postfix最重要的配置文件位于 /etc/postfix/main.cf ,在配置完成后,可通过

sudo /etc/init.d/postfix reload

重新加载配置文件生效。

postfix几个比较重要的配置:

  • myorigin

myorigin参数指明发件人所在的域名。如果你的用户的邮件地址为[email protected],则该参数指定@后面的域名。缺省 地,postfix使用本地主机名作为myorigin,但是建议你最好使用你的域名,因为这样更具有可读性。比如:安装postfix的主机为 mail.domain.com则我们可以这样指定myorigin:

myorigin = domain.com
 

当然我们也可以引用其他参数,如:

myorigin = $mydomain
 
  • mydestination

mydestination参数指定postfix接收邮件时收件人的域名,换句话说,也就是你的postfix系统要接收什么样的邮件。比如:你的用户的邮件地址为[email protected], 也就是你的域为
domain.com, 则你就需要接收所有收件人为[email protected]的邮件。与myorigin一样,缺省地,postfix使用本地主机名作为mydestination。如:

mydestination = $mydomain
mydestination = domain.com
 
  • notify_classes

在postfix系统中,必须指定一个postfix系统管理员的别名指向一个用户,只有这样,在用户遇到问题时才有报告的对象,postfix也 才能将系统的问题报告给管理员。notify_classes参数就是用来指定向postfix管理员报告错误时的信息级别。共有以下几种级别:

  1. bounce:将不可以投递的邮件的拷贝发送给postfix管理员。出于个人隐私的缘故,该邮件的拷贝不包含信头。
  2. 2bounce:将两次不可投递的邮件拷贝发送给postfix管理员。
  3. delay:将邮件的投递延迟信息发送给管理员,仅仅包含信头。
  4. policy:将由于UCE规则限制而被拒绝的用户请求发送给postfix管理员,包含整个SMTP会话的内容。
  5. protocol:将协议的错误信息或用户企图执行不支持的命令的记录发送给postfix管理员。同样包含整个SMTP会话的内容。
  6. resource:将由于资源错误而不可投递的错误信息发送给postfix管理员,比如:队列文件写错误等等。
  7. software:将由于软件错误而导致不可投递的错误信息发送给postfix管理员。

缺省值为:

notify_classes = resource, software
 
  • myhostname

myhostname 参数指定运行postfix邮件系统的主机的主机名。缺省地,该值被设定为本地机器名。你也可以指定该值,需要注意的是,要指定完整的主机名。如:

myhostname = mail.domain.com
 
  • mydomain

mydomain参数指定你的域名,缺省地,postfix将myhostname的第一部分删除而作为mydomain的值。你也可以自己指定该值,如:

mydomain = domain.com
 
  • mynetworks

mynetworks 参数指定你所在的网络的网络地址,postfix系统根据其值来区别用户是远程的还是本地的,如果是本地网络用户则允许其访问。你可以用标准的A、B、C类网络地址,也可以用CIDR(无类域间路由)地址来表示,如:

192.168.1.0/24
192.168.1.0/26
 
  • inet_interfaces

inet_interfaces 参数指定postfix系统监听的网络接口。缺省地,postfix监听
所有的网络接口。如果你的postfix运行在一个虚拟的ip地址上,则必须指定其监听的地址。如:

inet_interfaces = all
inet_interface = 192.168.1.1
 

4 测试

通过telnet,可测试邮件系统是否发送成功,在测试期间,通过:

tail -f /var/log/mail*
 

监控邮件发送log,可明确知道发送的问题在哪。

telnet localhost 25
 

如果正常,您将会看到以下来自 IMC 的响应:

220 xxx.xx Esmtp
 

键入以下命令开始进行通讯:

HELO :[email protected]
 

您应看到如下响应:

250 OK

键入以下命令来通知 IMC 邮件源于何处:

MAIL FROM: [email protected]

您应得到如下响应:

250 OK

键入以下命令来通知 IMC 邮件的目标地址。

RCPT TO: *******@qq.com

您应看到如下响应:

250 OK

键入以下命令以通知 IMC 您已准备好发送数据:

DATA

您应看到如下响应:

354 Send data.

键入以下命令以添加主题行:

Subject: test message

然后按两次 Enter 键。

该命令看不到任何响应。

备注: 两个 Enter 命令符合 RFC 822 规则,即 822 命令后必须跟空行。

键入以下命令来添加邮件正文:

This is a test message

您将看不到来自该命令的响应。

在紧接着的空行处键入句号(.),然后按 ENTER 键。

您应看到如下响应:

 250 OK

没有问题的话,你可以在log终端中看到postfix发送邮件的log,在****@qq.com中收到邮件了。

/etc/postfix/main.cf:
    myorigin = $myhostname (default: send mail as "user@$myhostname")
    myorigin = $mydomain   (probably desirable: "user@$mydomain")
 

5 参考文档:

http://www.centospub.com/make/postfix_smtp.html

http://linux.chinaunix.net/techdoc/net/2006/11/09/943684.shtml

 来源:http://jingwei.li/blog/?p=211

更多参看:

http://xiaozhuang.blog.51cto.com/4396589/865792

http://vbird.dic.ksu.edu.tw/linux_server/0380mail.php

https://help.ubuntu.com/community/PostfixBasicSetupHowto

https://help.ubuntu.com/community/PostfixVirtualMailBoxClamSmtpHowto

猜你喜欢

转载自justcoding.iteye.com/blog/1931890