zoukankan      html  css  js  c++  java
  • linux CentOS6的mail/sendmail命令发送电子邮件实现

    由于经常工作在linux下,所以很多时候需要将自己工作的报告或其他有用的东东发送给相关的人,所以花时间研究了一下在linux下如何发送mail。我们通常能用到下面3中发送方式:

    1. 使用Shell当编辑器发送邮件

    这种方式可以直接在shell窗口编辑邮件正文,当编辑完成之后使用Ctrl+D退出,同时邮件也会被发送出去

     代码如下 复制代码
    # -s后面是邮件的主题,主题后面是目标邮件地址[root@centos6 ~]# mail -s “Lucky mail” xlapn@126.com
    Hi Tomas,
    Thank you for you help!
    Regards,
    Daniel2. 使用管道发送邮件

    echo后面的是邮件正文

     代码如下 复制代码
    [root@centos6 ~]# echo "Hi Tomas,
    > Thank you for you help!
    > Regards,
    > Daniel" | mail -s "Thank you mail" xlapn@126.com3. 以文件的形式发送邮件正文
     

    这种形式适合比较长的邮件正文编写,将邮件正文写入到文件mail.body,然后通过<定位到目标邮件

     代码如下 复制代码
    [root@centos6 ~]# mail -s "Thanks mail" xlapn@126.com < mail.body4. 发送带有附件的邮件

    要发送带有附件的邮件,需要安装uuencode软件包,在配置好YUM源的前提下可以使用下面的命令安装uuencode包

     代码如下 复制代码
    [root@centos6 ~]# yum install sharutils安装好uuencode就可以发送带有附件的邮件了

    # uuencode后面跟了两个参数,第一个参数”/root/batch.file“是附件全路径,第二个参数”batch“是附件在邮件中显示的名字[root@centos6 ~]# uuencode /root/batch.file batch | mail -s "Batch file" xlapn@126.com < mail.body以上的一些命令都是在CentOS6上测试过的


    使用sendmail发送邮件


    sendmail是linux/unix系统下用来发送邮件的客户端。sendmail使用SMTP协议将邮件发送到目的SMTP服务器。其工作流程大概如下:

        首先要说一下DNS的MX记录:SMTP服务器基于DNS中的MX(mail exchange)记录来路由电子邮件,MX记录注册了域名和相关的SMTP中继主机,属于该域的电子邮件都应向该主机发送。
       (1)Sendmail 请求DNS给出主机sh.abc.com的CNAME 记录,如有,假若CNAME(别名记录)到shmail.abc.com,则再次请求shmail.abc.com的CNAME记录,直到没有为止。
       (2)假定被CNAME到shmail.abc.com,然后sendmail请求@abc.com 域的DNS给出shmail.abc.com的MX记录(邮件路由及记录),shmail MX 5 shmail.abc.com 10 shmail2.abc.com。
       (3)Sendmail组合请求DNS给出shmail.abc.com的A记录(主机名(或域名)对应的IP地址记录),即IP地址,若返回值为10.0.0.4(假设值)。
       (4)Sendmail与10.0.0.4连接,传送这封给user@sh.abc.com 的信到1.2.3.4 这台服务器的SMTP后台程序。

    1. 构造邮件
        在使用sendmail发送邮件之前,首先需要按邮件格式构造一封邮件。包括邮件头,邮件消息体。邮件格式在

     代码如下 复制代码
    RFC5322:internet message format(http://tools.ietf.org/html/rfc5322)中有详细说明。
    ?
    Code highlighting produced by Actipro CodeHighlighter (freeware)
    http://www.CodeHighlighter.com/

    -->From: John Doe <jdoe@machine.example>

    Sender: Michael Jones <mjones@machine.example>

    To: Mary Smith <mary@example.net>

    Subject: Saying Hello

    Date: Fri, 21 Nov 1997 09:55:06 -0600

    Message-ID: <1234@local.machine.example>

    This is a message just to say hello.

    So, "Hello".
     
     


    可以暂不指定date头(邮件发送时间)和messageid,这个信息将由sendmail程序填写。


    2. 使用sendmail发送邮件
        将邮件构造好之后,保存到一个本地文件,如/data/mail_content。然后调用sendmail发送,发送时指定接收邮箱地址:
        cat /data/mail_content | sendmail user@163.com

    3. 发送HTML格式的邮件
        如果要发送html格式的邮件, 就是说,邮件的消息体为一个html文件,需要在邮件头中指定 content-type为 text/html。如果不指定,默认情况下,content-type为text/plain,即普通文本。

     代码如下 复制代码
    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->From: John Doe <jdoe@machine.example>
    Sender: Michael Jones <mjones@machine.example>
    To: Mary Smith <mary@example.net>
    Content-type: text/html
    Subject: Saying Hello

    <div style="border:solid 1px #1D448C;">
    <h1>This is a message just to say hello.</h1>
    <p>So, "Hello".</p>
    </div>
     

    4. 字符编码
        在发送中文邮件中,字符编码是一个比较重要的问题,如果设置不正确,会导致邮件标题或邮件内容显示乱码。

        邮件内容的编码可以在邮件头content-type中设置,如设置邮件内容为utf-8编码:
        Content-type: text/html;charset=utf-8
      
        邮件头中,如From,To,Subject等,如果需要用到中文,可以这样设置:
      

     代码如下 复制代码
    “=?UTF-8?B?”+base64encode(内容UTF8编码)+"?="

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->From: =?UTF-8?B?5L2g5aW9?= <jdoe@machine.example>
    Sender: Michael Jones <mjones@machine.example>
    To: Mary Smith <mary@example.net>
    Content-type: text/html;charset=utf-8
    Subject: =?UTF-8?B?5L2g5aW9?=

    <div style="border:solid 1px #1D448C;">
    <h1>This is a message just to say hello.</h1>
    <p>So, "Hello".</p>
    </div>
     

    上面我们也看到sendmail邮件发送配置超麻烦,下面我们不用sendmail来发,实例


    bin/mail会默认使用本地sendmail发送邮件,这样要求本地的机器必须安装和启动Sendmail服务,配置非常麻烦,而且会带来不必要的

    资源占用。而通过修改配置文件可以使用外部SMTP服务器,可以达到不使用sendmail而用外部的smtp服务器发送邮件的目的:

    修改/etc/nail.rc (/etc/mail.rc)

     代码如下 复制代码
    set from=fromUser@domain.com smtp=smtp.domain.comset smtp-auth-user=username smtp-auth-password=passwordset smtp-auth=login


    说明:

    from是发送的邮件地址

    smtp是发生的外部smtp服务器的地址

    smtp-auth-user是外部smtp服务器认证的用户名

    smtp-auth-password是外部smtp服务器认证的用户密码

    smtp-auth是邮件认证的方式

    配置成功后,就可以使用了

    可以发送一封邮件测试一下:

     代码如下 复制代码
    mail -s "test" user@sohu.com <content.txt 

    其中-s后面的是邮件标题,user@sohu.com是收件人地址,content.txt里面是邮件正文

    更多详细内容请查看:http://www.111cn.net/sys/linux/43376.htm

  • 相关阅读:
    struts2的类型转换
    简单的图形验证码
    无法添加某个relationship给SAP CRM Product category的一个可能原因
    如何找到ABAP里被动态调用的update function module
    ABAP数据库表的元数据
    SAP CRM Product hierarchy,Category和Application的三个问题
    使用javap分析Java字节码的一个例子
    在SAP ABAP里使用注解@Inject模拟Java Spring
    SAP系统里的胖接口Fat interface
    如何使用ABAP代码创建SAP Product Category
  • 原文地址:https://www.cnblogs.com/phpfans2012/p/2643100.html
Copyright © 2011-2022 走看看