免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3799 | 回复: 4
打印 上一主题 下一主题

用perl发送邮件的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-09-27 13:15 |只看该作者 |倒序浏览
各位高人帮看一下,我从网上扒了一个可以发送邮件的脚本,但是发现,如果是通过命令行时输入发件人邮箱,密码、主题等,可以正常发送邮件,但如果通过配置文件就不行,参数传递过程中看,跟手工输入是一样的,请大家给点意见。
代码如下:

#!/usr/local/bin/perl

#*****************************************************************************#
#  Function Name:
#       SendMail($$\@$$\@)
#  Description:
#       Use Net::SMTP and MIME::Lite to send Email with attachment.
#  Argument:
#       1: Sending Email address
#       2: Password of sending Email.
#       3: The recipient Email array
#       4: The subject of the Email
#       5: The content of the Email
#       6: The attachments array of the Email
#  Return:
#       None
#*****************************************************************************#

use Net::SMTP;
use MIME::Lite;

sub SendMail($$\@$$\@)
{

my ($mailFrom, $password, $mailToRef, $subject, $content, $attachmentRef) = @_;
my ($userName,$mailHost) = split(/\@/, $mailFrom);
print "The Email user name is: $userName\n";
print "The mailHost is: $mailHost\n";
$mailHost = "mail.".$mailHost;
my $helloPara = $mailHost;
print "The Email user name is: $userName\n";
print "The mailHost is: $mailHost\n";

my @mailTo = @$mailToRef;   #The recipient list
for(my $i=0; $i<=$#mailTo; $i++) {
     print "$#mailTo\n";
     print "$mailTo[$i]\n";   }

my @attachment = @$attachmentRef;    #The attachments

my $smtp = Net::SMTP->new($mailHost, Hello => $helloPara, Timeout => 120, Debug => 1)
        ||die "Cannot connect to server \'$mailHost\'";

# anth login, type your user name and password here
  $smtp->auth($userName,$password)||print "Auth Error!\n";

  foreach my $mailTo (@mailTo)
  {
    # Create a new multipart message:
    my $msg = MIME::Lite->new(
        From    => $mailFrom,
        To      => $mailTo,
        Subject => $mailSubject,
        Type    =>'multipart/mixed',
        )or print "Error creating MIME body: $!\n";

    # Add parts:
    $msg->attach(Type     =>'TEXT',
                 Data    => $mailContent,
                );
#    foreach my $attachment (@mailAttachment)
#    {
#        $msg->attach(
#          Type     => 'AUTO',      # the attachment mime type
#          Path     => $attachment, # local address of the attachment
#          )or print "Error attaching test file: $!\n";
#    }

    my $str = $msg->as_string() or print "Convert the message as a string: $!\n";

        # 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 message
        $smtp->datasend("$str");

        # Send the termination string
        $smtp->dataend();
}
$smtp->quit;
return;
}


#获取命令行参数
if(@ARGV < 1) {
  $conf_file = './config/mailsetting.conf'; #默认配置文件
}
else {
  $conf_file = $ARGV[0]; #获取配置文件名
}

#打开配置文件和日志文件
open CONF_FILE, $conf_file or die "Open config file [$conf_file] failed! \n";
$i = 0;
while(<CONF_FILE>){
        #print $_;
        @line[$i] = $_;
        $i = $i+1;

}
#print @line;
close CONF_FILE;

my $mail_From = @line[0];
my $mail_password = @line[1];
#print "$mailFrom \n";
#print "$password \n";
my @mailTo = ('XXXX@XXXXXXX.com.cn');
my $mailSubject = 'this a test for perl sendmail pro';
my $mailContent = 'this a test';
my @mailAttachment = ();

SendMail($mail_From, $mail_password, @mailTo, $mailSubject, $mailContent, @mailAttachment);

exit();

配置文件内容是
XXX@DDD.com.cn
123456

论坛徽章:
0
2
发表于 2011-09-27 13:19
这个是我另一个脚本,总体上都是一样的,只是需要在运行时手动输入发件人,密码,主题等信息。
如:sendmail.pl XXXX@XXX.com.cn 123456 "test test"

#!/usr/local/bin/perl

#*****************************************************************************#
#  Function Name:
#       SendMail($$\@$$\@)
#  Description:
#       Use Net::SMTP and MIME::Lite to send Email with attachment.
#  Argument:
#       1: Sending Email address
#       2: Password of sending Email.
#       3: The recipient Email array
#       4: The subject of the Email
#       5: The content of the Email
#       6: The attachments array of the Email
#  Return:
#       None
#*****************************************************************************#

use Net::SMTP;
use MIME::Lite;

my ($mailFrom, $password, @mailTo, $mailSubject, $mailContent, @mailAttachment);

#die "Syntax: $0 <mailFrom> <password> <mailTo> <Subject> <Content> <Attachment>\n" if $#ARGV != 5;

#our $mailFrom = $ARGV[0];       #Send email address
#print "mailFrom is $mailFrom\n";
$mailFrom = $ARGV[0];
print "$mailFrom\n";
$password = $ARGV[1];
@mailTo = split(",",$ARGV[2]);  #The recipient list
print "$ARGV[2]\n";
print "mailTo are @mailTo";
print "\n";
$mailSubject = $ARGV[3];
print "mailSubject is $mailSubject\n";
$mailContent = $ARGV[4];
print "mailContent is $mailContent\n";
@mailAttachment = split(";",$ARGV[5]);    #The attachments for the test result
print "mailAttachment are @mailAttachment";
print "\n";
#SendMail($mailFrom, $password, @mailTo, $mailSubject, $mailContent, @mailAttachment);

#sub SendMail($$\@$$\@)
#{
#use Net::SMTP;
#use MIME::Lite;

#my ($mailFrom, $password, $mailToRef, $subject, $content, $attachmentRef)=@_;

my ($userName,$mailHost) = split(/\@/, $mailFrom);
#print "The Email user name is: $userName\n";
#print "The mailHost is: $mailHost\n";
$mailHost = "mail.".$mailHost;
my $helloPara = $mailHost;
print "The Email user name is: $userName\n";
print "The mailHost is: $mailHost\n";

#my @mailTo = @$mailToRef;   #The recipient list
for(my $i=0; $i<=$#mailTo; $i++) {
     print "$#mailTo\n";
     print "$mailTo[$i]\n";   }

#my @attachment = @$attachmentRef;    #The attachments

my $smtp = Net::SMTP->new($mailHost, Hello => $helloPara, Timeout => 120, Debug => 1)
        ||die 'Cannot connect to server \'$mailHost\'';

# anth login, type your user name and password here
  $smtp->auth($userName,$password)||print "Auth Error!\n";

  foreach my $mailTo (@mailTo)
  {
    # Create a new multipart message:
    my $msg = MIME::Lite->new(
        From    => $mailFrom,
        To      => $mailTo,
        Subject => $mailSubject,
        Type    =>'multipart/mixed',
        )or print "Error creating MIME body: $!\n";

    # Add parts:
    $msg->attach(Type     =>'TEXT',
                 Data    => $mailContent,
                );
    foreach my $attachment (@mailAttachment)
    {
        $msg->attach(
          Type     => 'AUTO',      # the attachment mime type
          Path     => $attachment, # local address of the attachment
          )or print "Error attaching test file: $!\n";
    }

    my $str = $msg->as_string() or print "Convert the message as a string: $!\n";

        # 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 message
        $smtp->datasend("$str");

        # Send the termination string
        $smtp->dataend();
}
$smtp->quit;
return;
#}

论坛徽章:
0
3 [报告]
发表于 2011-09-27 14:42 |只看该作者
找到问题所在了,是配置文件中的内容有问题。

论坛徽章:
0
4 [报告]
发表于 2011-09-29 14:43 |只看该作者
学习!

论坛徽章:
0
5 [报告]
发表于 2011-09-29 15:12 |只看该作者
chomp一下,有换行符的!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP