Linux系统中利用sendmail发送包含附件的邮件
思韵闪耀
2013-03-19
0
今天一个同事跑一个脚本需要把生成的日志发送到它邮箱里,现在脚本里加的报警邮件是使用 一个perl脚本来报的,想报一个附件,一看还不行,后从网上看了看找到一个
1.sendmail.pl
#!/usr/bin/perl
#file main.pl
use Net::SMTP;
my $mailhost = "mail.***.com"; # the smtp host 邮件服务器地址
my $mailfrom = '123@***.com'; # your email address 报警的邮箱
#my @mailto = ('fayland@gmail.com', 'not_four@hotmail.com'); # the recipient list
my @mailto = $ARGV[0]; # the recipient list
my $subject = $ARGV[1];
my $text = $ARGV[2];
open(FILE,$text) || die "Can not open list filen";
undef $/;
$text=<FILE>;
$smtp = Net::SMTP->new($mailhost, Hello => 'localhost', Timeout => 120, Debug => 1);
# anth login, type your user name and password here
$smtp->auth('user','pass');
foreach my $mailto (@mailto) {
# Send the From and Recipient for the mail servers that require it
$smtp->mail($mailfrom);
$smtp->to($mailto);
# Start the mail
$smtp->data();
# Send the header
$smtp->datasend("To: $mailto
");
$smtp->datasend("From: $mailfrom
");
$smtp->datasend("Subject: $subject
");
$smtp->datasend("
");
# Send the message
$smtp->datasend("$text
");
# Send the termination string
$smtp->dataend();
}
$smtp->quit;

保存为sendmail.pl,然后赋予执行权限,脚本里可以写以下的

红色的字是报警邮件的主题
蓝色的:报警邮件发给谁
粉色的:当前报警的日期、时间

123.txt中可以写上邮件的内容
比如,索引数据报警,请技术查看!
./sendmail.pl 123@***.com `date +%Y年%m月%d日%H点%M分`什么什么报警 123.txt

2.使用sendmail发附件,需要使用uuencode命令,它是包含在sharutiles组建

先检测一下yum search sharutils
======================================================== Matched: sharutils ========================================================
sharutils.x86_64 : The GNU shar utilities for packaging and unpackaging shell archives.
安装:centos: yum -y install sharutils
命令的话请自己查看man帮助
file_path:附件的目录
file:发送的附件
引号中是邮件的主题
uuencode file_path file | mail -s "file" 123@***.com

例如:uuencode /tmp/123.tar 123.tar | mail -s "123的包,请查收附件" jamesbond0479@163.com


【版权声明】
本站部分内容来源于互联网,本站不拥有所有权,不承担相关法律责任。如果发现本站有侵权的内容,欢迎发送邮件至masing@13sy.com 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

相关内容

将IIS设置克隆到新服务器...
将IIS设置克隆到新服务器的最佳方法是使用IIS配置文件。以下是详...
2025-06-06
网页保护、网页图片保护
1、禁止另存网页,把如下代码加入到网页body/body中 程序代...
2025-05-30
搭建Git服务器及本机克隆...
Git是什么?Git是目前世界上最先进的分布式版本控制系统。SVN...
2025-03-17
ubuntu环境下搭建gi...
操作环境:服务器:Ubuntu 24.04.2 LTS+git 2...
2025-03-17
docker-compos...
1.Compose介绍 DockerCompose是一个用来定义和...
2024-04-26
centos stream...
1、更新系统软件包确保系统已经更新到最新版本,打开终端,执行以下命...
2024-03-30

热门资讯

Discus X论坛配置sen... Discuz x2.5 后台邮件设置方法其实和X2/x1.5基本一样,但很多同学都不注意一些细节,比...
redhat Linux系统配... 1 .检查Send Mail 的安装包 [root@sql root]# rpm -qa | gre...
使用Sendmail的反垃圾邮... 在最近几年,无处不在的垃圾邮件已使得所有邮件箱不堪重负,不断地需要工具以抗击这一无穷尽的垃圾邮件流。...