您的位置:首页 > 运维架构 > Linux

linux下发送邮件

2015-12-29 10:28 711 查看
系统管理人员经常会遇到对于设备或者任务的预警与通知,通常情况有发送短信、邮件等方式。发送短信一般来说需要有短信猫(硬件)或者调用libfetion给飞信用户发送。本文介绍几种简单的发送邮件的方式。

本文环境:Ubuntu 10.04

基础:

Linux服务器发送邮件一般都是基于sendmail进行的,sendmail服务器提供对外的邮件发送功能。其他工具都是基于sendmail进行服务的。所以要在linux系统发送邮件,首先需要安装sendmail服务器安装方法:

#sudo apt-get install sendmail

然后通过ps查看是否有sendmail进程,如果存在,则安装成功:

#ps -ef|grep sendmail

root 1282 1 0 13:39 ? 00:00:00 sendmail: MTA: accepting connections

成功安装sendmail后,就可以向邮件账户发送邮件了。

=====================分割线===============================

本文列出了在linux下常用的发送邮件的方法,供大家参考。

方法1:

直接使用sendmail,编辑如下文件a.sh,通过chmod 更改权限后执行就可以。

#!/bin/bash
/usr/sbin/sendmail -t << EOF
From: Mail test
Sender: jkjl
To: test@qq.com
Cc: test@g.cn
Subject: mail testing
----------------------------------
This is the mail content ...
muhaha
---------------------------------
EOF

man sendmail

-t参数的含义

-t Read message for recipients. To:, Cc:, and Bcc: lines will be

scanned for recipient addresses. The Bcc: line will be deleted

before transmission.

另外,sendmail默认从标准输入读入内容直到结束或者遇到".",-oi 就是认为遇到"."不再认为是结束符了。如下:

echo "hahaha.my"|sendmail -oi ureamil@email.com

方法2:

利用mail工具发送,利用mail发送邮件必须安装mailutils

sudo apt-get install mailutils

然后发送邮件

$ mail -s "just a test" 收信人邮箱地址 < 要发送的邮件内容文件

mail -s "haha" test@163.com < hello.txt

mail 工具的-t 可以跟多个用户,如下:

mail -s Title -t test1@163.com -t test2@163.com < hello.txt

如果要发送带附件的邮件,则需要先安装uuencode,uuencode 在sharutils包中

sudo apt-get install sharutils

然后再发送

uuencode 附件名 显示附件名| mail -s 题目 目的邮箱

uuencode hello.txt bienvenu |mail -s Test test@163.com

如果按上面的方法,邮件只带一个附件,即将正文和附件组成联合文件发出。

方法3:

利用formail和sendmail联合发送:

formail可以封装邮件信息,然后调用sendmail发送,经典例子如下:

echo hello|formail -I "From:test@163.com" -I "MIME-Version:1.0" -I "Content-type:text/html;charset=gb2312" -I "Subject:test"|sendmail -oi aimEmail@mailserver.com

方法4:

使用mutt发送

mutt是一个linux下非常好用的email程序,最典型的一个例子如下:

mutt -s "Test mail" test@163.com -a test.jpg < hello.txt

其中:s—主题 a—附件

最后追加的是邮件内容mutt甚至可以使用pgp加密,利用mutt支持MIME,解决乱码问题等,总之mutt是个人认为的最好用的email工具。

以上转自:http://blog.chinaunix.net/uid-20465760-id-3046249.html

常用发送邮件方式如下:
1.如何写一般的邮件: mail test@126.com Cc 编辑抄送对象,Subject:邮件主题,输入回车,邮件正文后,按Ctrl-D结束
2.快速发送方式: echo “邮件正文” | mail -s 邮件主题 test@126.com
3.以文件内容作为邮件正文来发送: mail -s test test@126.com < test.txt
4.发送带附件的邮件: uuencode
附件名称 附件显示名称 | mail -s 邮件主题 发送地址

例如: uuencode test.txt test.txt | mail -s Test test@126.com
以上转自:/article/4492267.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: