`

perl mail

    博客分类:
  • perl
阅读更多
#!/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



分享到:
评论

相关推荐

    perl-Mail-Sender-0.903-7.el8.noarch(1).rpm

    官方离线安装包,亲测可用

    Perl的Email 发送程序与插件

    在提供的压缩包文件`Perl Email Send`中,可能包含了示例的Perl脚本或配置文件,演示如何使用`Mail::Sender`或其他相关模块来发送邮件。通过学习这些文件,你可以更好地理解如何在实际项目中实现Perl的邮件发送功能...

    局域网发送匿名邮件的perl脚本

    配置一下 ip地址 my $ADDR=pack('SnC4x8',$PF_INET,$port,192,168,60,10); 和要发送的邮件地址 就可以发送匿名邮件

    perl-Mail-Sendmail

    在perl中使用本机的sendmail发送邮件的代码如下: #!/usr/bin/perl use strict; my($r_mail) = 'xxxx@163.com'; my($s_mail) = 'root@abc.cn'; my($subject) = '邮件标题'; open(MAIL, '|/usr/lib/sendmail -t'); ...

    perl-Mail-Sender-0.8.16-1.el5.pp.src.rpm

    perl-Mail-Sender-0.8.16-1.el5.pp.src.rpm 邮件发送依赖资源包,需通过rpm -ivh perl-Mail-Sender-0.8.16-1.el5.pp.noarch.rpm 命令进行安装

    perl 邮件处理send_mail

    在unix自己的目录下自动记录emailaddress。自动发送标题邮件#######

    mail-dmarc:Mail :: DMARC,Perl中的完整DMARC实现

    在Perl中,`Mail::DMARC`模块提供了一个全面的解决方案,用于处理DMARC相关的任务。 **Perl**是一种通用的、解释型的编程语言,因其在文本处理和系统管理任务上的强大能力而在IT领域广泛应用。在Perl中,`Mail::...

    Mail-Sender-0.8.22

    《Perl模块Mail-Sender-0.8.22:SMTP邮件发送详解》 在IT行业中,Perl语言以其强大的文本处理能力和灵活的语法深受程序员喜爱。而`Mail-Sender`是Perl社区中一个非常实用的模块,它简化了通过SMTP协议发送电子邮件...

    perl神奇入门-最佳perl入门读物

    根据给定文件的信息,我们可以提炼出一系列关于Perl编程语言的基础知识点。这些知识点涵盖了Perl的基本特点、变量表示方式、以及简单的程序示例等。下面将详细展开这些内容。 ### Perl编程语言简介 Perl是一种多...

    perl-Mail-DKIM-0.54-1.el8.noarch(1).rpm

    官方离线安装包,亲测可用

    perl自动发邮件

    在提供的压缩包文件"Perl_mail"中,可能包含了一个或多个示例脚本,这些脚本演示了如何使用Perl发送邮件。通常,这些脚本会导入必要的邮件模块,定义邮件内容(包括收件人、主题、正文和附件),然后通过SMTP服务器...

    manager及perl依赖centos6版.zip

    5. `perl-Mail-Sendmail-0.79-12.el6.noarch.rpm`:Perl的Mail::Sendmail模块,提供了发送邮件的功能,与Mail::Sender配合使用,可能用于发送系统通知。 6. `perl-Config-Tiny-2.12-7.1.el6.noarch.rpm`:Perl的...

    manager及perl依赖centos7版.zip

    7. **perl-Mail-Sendmail-0.79-21.el7.noarch.rpm**:一个Perl接口,用于通过sendmail系统发送邮件,与perl-Mail-Sender一起使用可能用于发送管理程序的邮件通知。 8. **perl-Parallel-ForkManager-1.18-2.el7....

    perl-Mail-SPF-2.8.0-4.el7.noarch.rpm

    离线安装包,亲测可用

    利用perl给多人发送邮件

    在Unix系统中,Perl可以通过内置的`Mail::Sender`模块或者第三方的`Email::Sender`模块来发送邮件。在这个场景中,我们使用的可能是`Mail::Sender`,因为它在早期的Perl环境中更为常见。 1. **配置文件sendmail.cfg...

    用perl操作LDAP数据库

    Perl作为一种强大的脚本语言,因其灵活和丰富的库支持,常被用来编写处理这类任务的程序。本文将深入探讨如何使用Perl来操作LDAP数据库。 首先,你需要了解Perl中的Net::LDAP模块,这是Perl与LDAP服务器交互的核心...

    perl 写的发邮件smtp

    在IT行业中,Perl常被用来编写自动化脚本,其中之一就是通过SMTP(Simple Mail Transfer Protocol)发送电子邮件。SMTP是互联网标准,用于从一台邮件服务器向另一台邮件服务器发送邮件。下面将详细介绍如何使用Perl...

    使用Perl编写CGI时需要注意的几个问题

    print S "MAIL FROM: $from\n"; $a = ; print LOG "$a\n" if $doLog == 2; # 其他SMTP命令和邮件体发送代码... } ``` #### 四、总结 在跨平台使用Perl进行CGI编程时,需要注意不同操作系统之间的差异。特别是在...

Global site tag (gtag.js) - Google Analytics