发送邮件的程序不会编译

时间:2017-04-29 12:39:38

标签: windows perl

use strict;
use warnings;

use Net::SMTP;

# to install Net::SMTP moudle package, run cpan command in command line
# in the shell type install Net::SMTP

sub send_mail
####################################################################################################
#
# SUBROUTINE : send_mail
#
# PURPOSE    : Send an email.
#
# INPUT(S)   : smtp_server - Simple Mail Transfer Protocol server
#              to          - Recipient address
#              from        - Sender address
#              subject     - Subject
#              body        - Reference to an array containing the message body
#
# OUTPUT(S)  : 0 - success
#              1 - failure
#
####################################################################################################
{


     my $from    = 'home.yosef@gmail.com';
     my $to      = 'home.yosef@gmail.com';
     my $data    = "A simple test message from Perl script\n";
     my $subject = "Hello World from Perl script";

     smtp_server => "smtp.gmail.com";



    # Connect to the SMTP server
    my $smtp = Net::SMTP->new($smtp_server);

    # If connection is successful, send mail
    if ($smtp) {

        # Establish to/from
        $smtp->mail($from);
        $smtp->to($to);

        # Start data transfer
        $smtp->data();

        # Send the header
        $smtp->datasend("To: $to\n");
        $smtp->datasend("From: $from\n");
        $smtp->datasend("Subject: $subject\n");
        $smtp->datasend("\n");

        # Send the body
        $smtp->datasend(@body);

        # End data transfer
        $smtp->dataend();

        # Close the SMTP connection
        $smtp->quit();

    # If connection fails return with error
    }
    else {

        # Print warning
        warn "WARNING: Failed to connect to $smtp_server: $!";

        return 1;
    }

    return 0;
}

# Define the message body
my @message_body = "Hello World! from Perl script\n";
push @message_body, "Add another line!\n";

# Send the email!
send_mail(
    smtp_server => <smtp_server_name>,
    to          => <to_address>,
    from        => <from_address>,
    subject     => 'This is a mail from Perl script',
    body        => \@message_body,
);

我安装了带有Strawberry Perl的Windows。当我从命令行运行此脚本时,我收到错误,我不知道出了什么问题。

这些是错误:

  

全球符号&#34; $ smtp_server&#34;需要显式的包名(你忘了在windows_send_mail_with_SMTP.pl第40行声明&#34;我的$ smtp_server&#34;?)   全球符号&#34; @ body&#34;需要明确的包名(你忘记在windows_send_mail_with_SMTP.pl第59行申报&#34;我的@ body&#34;?)   全局符号&#34; $ smtp_server&#34;需要显式的包名(你忘记在windows_send_mail_with_SMTP.pl第71行声明&#34;我的$ smtp_server&#34;?)   由于编译错误,windows_send_mail_with_SMTP.pl的执行中止。

1 个答案:

答案 0 :(得分:1)

我认为从消息中可以清楚地看出来。您尚未声明(或定义)$smtp_server@body

这一行没有做任何有用的事情

smtp_server => "smtp.gmail.com"

=>运算符只是一个逗号,恰好在第一个参数周围加上引号,所以它与

相同
"smtp_server", "smtp.gmail.com"

只评估两个字符串并丢弃它们

由于您之前已经成功定义了其他四个变量,我不明白为什么你选择在这里做一些不同的事情

至于@body,你永远不会声明它或放入任何东西。正如错误消息所示,

  

你忘了宣布“我的@body”吗?