设为首页 收藏本站
查看: 733|回复: 0

[经验分享] perl mail

[复制链接]

尚未签到

发表于 2017-5-16 12:37:00 | 显示全部楼层 |阅读模式
#!/opt/perl/5.8.0/bin/perl
#ident"%W%"


# Usage:   mail_files -t mailid [ -c mailid ] [ -s subject ] [ -f mailid ] [ -x content_type ]
#          [-n file_list] [-u file_list] [-b file_list] file_list
#
#    -f      : The mailid of the sender ( defaults to your userid )
#              Only userids that have been defined as "trusted" in the sendmail
#              config file can make use of the -f option. For non-trusted users
#              any value specified by this parameter will be ignored by
#              sendmail.
#    -t      : The mailid of the recipient. Mandatory, no default
#              multiple mailids can be specified, separated by commas.
#    -c      : The mailid of any carbon-copy recipients. Optional.
#              multiple mailids can be specified, separated by commas.
#    -s      : The subject string. Optional, default = "Not specified".
#              Enclose in quotes.
#    -n      : no-encode: indicates a list of files which are NOT to be base64
#              or uuencode encoded. Multiple files may be enclosed in double
#              quotes. Usual wildcard notation can be used. This option is
#              for completeness and can be omitted because the default action
#              is not to encode the file-list.
#    -b      : base64 encoding: indicates a list of files which are to be
#              base64 encoded. Multiple files may be enclosed in double quotes.
#              Usual wildcard notation can be used.
#    -u      : uuencode encoding: indicates a list of files which are to be
#              uuencode encoded. Multiple files may be enclosed in double
#              quotes. Usual wildcard notation can be used.
#    -x      : the main body content-type: "plain" (default) or "html"
#  file_list : The list of files to send as attachments with no-encoding
#              (same as -n option, but the file list does not need to be
#              enclosed in quotes if more than one file specified).
#              Usual wildcard notation can be used.

# The program will also prompt for text to be supplied on standard input
# as the main text of the message.

# The script makes use of perl's MIME package to perform the base-64
# encoding/decoding.

# Note that files destined for Windows environments should have a name of
# the form aaaa.bbb where aaaa is up to 8 characters long, and bbb is a
# 3 character sufix. The suffix determines which program is used to
# display/process the data at the remote end.

# Simple text files can be emailed unencoded. Binary files, or text files
# with long lines ( ie > 1000 chars ) should use the  base64 or uuencode
# encoding procedures. Base64 is preferred because it is more universally
# supported. In particular, most PC mail-clients can automatically decode
# base64 encoded attachments. Note that simple text files with short lines
# which are destined for PC environments should not be base64 encoded.
# This is because PCs use a different line-break character to Unix.
# If the text is base64 encoded, the line-breaks are not converted
# automatically and so the data arrives at the remote end without
# line-breaks.

# set up a 'usage' routine
# ------------------------

sub usage()
{
  print @_;
  print <<EOF;
  Usage:   mail_files -t mailid [ -c mailid ] [ -s subject ] [ -f mailid ] [ -x content_type ]
           [-n file_list] [-u file_list] [-b file_list] file_list
EOF
  exit 4;
}

# Initialise main variables ...
# -------------------------

use MIME::Base64 qw(encode_base64);
use Getopt::Std;
getopt("ftcsnbux");

$content_type = "plain";

if ($opt_x ne "") {
    $content_type = $opt_x;
}

if ($opt_t eq "") {
    &usage("An addressee must be specified");
}

# All remaining parameters are files not requiring encoding ...
# ---------------------------------------------------------

# Build up $FILES as the list of non-encoded files. Use sed to remove
# any leading space from the variable.


#if ($opt_b eq "" && $opt_u eq "" && $opt_n eq "") {
#    &usage("At least one file must be specified");
#}

# Remove leading commas from TO, CC  ...
# ---------------------------------

$opt_t =~ s/^\.//;
$opt_c =~ s/^\.//;

# Validate that the files exist ...
# -----------------------------

@txt_list = split /,/, $opt_n;
@uue_list = split /,/, $opt_u;
@b64_list = split /,/, $opt_b;

foreach $f (@txt_list, @uue_list, @b64_list) {
    if (! -r $f) {
      print "Error: File $f does not exist / is not readable.\n";
      print "Exiting. ( Mail not sent ).\n";
      exit;
    }
}

# Get environment script is running from
$RTENV=$ENV{"WORLD"};
if ( "$RTENV" ne "" ) {
    $RTENV=~tr/[a-z]/[A-Z]/;
    $RTENV=" ($RTENV):";
}

print STDERR "Enter text of main message ( finish with CTRL-D ) ...";

# Now do the work ...
# ---------------

# The generated mail message is output onto standard out, which is then
# piped in to sendmail.


open(STDOUT, "| /usr/lib/sendmail -t") or open(STDOUT, "| /usr/sbin/sendmail -t") or die "Cannot find sendmail";

print <<EOF;
From: $opt_f
Subject:$RTENV $opt_s
To: $opt_t
EOF

if (defined($opt_c)) {
print "Cc: $opt_c\n";
}

print <<EOF;
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="DMW.Boundary.605592468"

This is a Mime message, which your mail program may not understand. Parts
of the message will appear as text. If the remainder appears as random
characters in the message body, instead of as attachments, then you'll
have to extract these parts and decode them manually.

--DMW.Boundary.605592468
Content-Type: text/$content_type; charset=US-ASCII

EOF
#Content-Type: text/plain; name="message.txt"; charset=US-ASCII
#Content-Disposition: inline; filename="message.txt"
#Content-Transfer-Encoding: 7bit

# Read the standard input as the main text of the message ...
# -------------------------------------------------------

while (<ARGV>) {
print;
}

print "\n";

# Now process the non-encrypted attachments ...
# -----------------------------------------

foreach $f (@txt_list) {
       $BASE=$f;
       $BASE =~ s/^.*\///;

       print <<EOF;
--DMW.Boundary.605592468
Content-Type: application/octet-stream; name="$BASE"
Content-Disposition: attachment; filename="$BASE"
Content-Transfer-Encoding: 7bit

EOF

       open(BFILE, "< $f");
       print <BFILE>;
       close(BFILE);
}

# Now process the base64 encrypted attachments ...
# --------------------------------------------

foreach $f (@b64_list) {
       $BASE=$f;
       $BASE =~ s/.*\///;

       print <<EOF;
--DMW.Boundary.605592468
Content-Type: application/octet-stream; name="$BASE"
Content-Disposition: attachment; filename="$BASE"
Content-Transfer-Encoding: base64

EOF

       open(BFILE, "< $f");
       binmode BFILE;
       local($/) = undef;
       print encode_base64(<BFILE>);
#       while (<BFILE>) {
#        print encode_base64($_);
#       }
       close(BFILE);
}

# Now process the uuencode encrypted attachments ...
# ----------------------------------------------

# Sorry, this bit is untested - I haven't got a mail-client which can
# handle uuencoded MIME messages automatically, so can't test if the
# 'Content-Transfer-Encoding: uuencode' line is correct and whether I
# need the uuencode "begin" and "end" lines.

foreach $f (@uue_list) {
       $BASE=$f;
       $BASE =~ s/.*\///;

       print <<EOF;
--DMW.Boundary.605592468
Content-Type: application/octet-stream; name="$BASE"
Content-Disposition: attachment; filename="$BASE"
Content-Transfer-Encoding: uuencode

EOF

       `uuencode < $f xxx`
}

# append the final boundary line ...

print "--DMW.Boundary.605592468--\n"

#) | /usr/lib/sendmail -t

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-378186-1-1.html 上篇帖子: perl学习资源 下篇帖子: perl -e
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表