2015-02-05 85 views
0

我一直在摸索着如何从Windows 7上的Perl发送电子邮件。我无法安装任何Perl模块,主动Perl安装。 (这意味着Mime :: Lite,Email :: Sender和Mail :: Sendmail都不在表格中)在Windows 7上使用Perl发送电子邮件,无需安装模块

我能找到的唯一电子邮件发送模块似乎是Net :: SMTP,但我一直无法弄清楚如何正确使用它。我不太了解SMTP服务器的功能,更不用说它们是如何工作的。我发现发送邮件的每一篇文章都建议使用我不具备的不同模块。

我看到another post建议使用Net :: SMTP :: SSL连接到Gmail,但我没有SSL模块。

这是一些代码,我到目前为止,我试图使用Gmail作为我的SMTP服务器:

use Net::SMTP; 

$smtp = Net::SMTP->new('smtp.gmail.com'); # connect to an SMTP server 

$smtp->mail('[email protected]'); # use the sender's address here 
$smtp->to('[email protected]');  # recipient's address 
$smtp->data();      # Start the mail 

# Send the header. 
$smtp->datasend("To: toAddress\@test.com\n"); 
$smtp->datasend("From: myAddress\@gmail.com\n"); 
$smtp->datasend("Subject: Test email\n"); 
$smtp->datasend("\n"); 

# Send the body. 
$smtp->datasend("Hello, World!\n"); 
$smtp->dataend();     # Finish sending the mail 
$smtp->quit;      # Close the SMTP connection 

我不断收到错误:

Can't call method 'mail' on an undefined value

这我假设手段它无法连接到SMTP服务器。我怎样才能解决这个问题?

也有任何其他模块标准与活动Perl更容易使用?

我真的在寻找类似于Linux SENDMAIL命令的东西,它非常简单,甚至不需要你连接或认证任何东西。 Linux SENDMAIL命令似乎甚至允许你组成你想要的任何“from”地址,这可能是非常危险但很棒!


编辑

而且它不是我去通过Gmail的要求。这只是我想到的第一件事。

+0

我知道我应该使用'strict'和'warnings',但是这仅仅是一个快速脚本来尝试并获得它的工作。我尽可能保持它尽可能接近文档中的示例。 – tjwrona1992 2015-02-05 16:48:44

+0

请参阅http://stackoverflow.com/q/11009844/176646 – ThisSuitIsBlackNot 2015-02-05 16:50:11

+0

这实际上是我为我的链接而发布的帖子,我必须将它与另一个混合起来,我现在将修复该链接。正如你可以看到的那样,这篇文章建议使用'Net :: SMTP :: SSL',这是我没有的。我只有'NET :: SMTP'。 – tjwrona1992 2015-02-05 17:10:51

回答

3

事实证明,该解决方案仅仅是要求我公司的Windows管理员使用合适的SMTP服务器。

一旦我找到合适的SMTP服务器,一切都按计划进行!

我得到它的工作后,我写了一个简单的子程序发送纯文本电子邮件:

#!/usr/bin/perl 

use strict; 
use warnings; 

use 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 
# 
#################################################################################################### 
{ 
    # Unpack input arguments 
    my %args = @_; 

    # Get required arguments 
    my $smtp_server = $args{smtp_server} or die "ERROR: \$smtp_server is not defined"; 
    my $to   = $args{to   } or die "ERROR: \$to is not defined"; 
    my $from  = $args{from  } or die "ERROR: \$from is not defined"; 

    # Get optional arguments 
    my $subject = $args{subject} if $args{subject}; 
    my @body = @{$args{body}} if $args{body }; 

    # 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!\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 subject', 
    body  => \@message_body, 
); 

所有此脚本需要的是“SMTP服务器名称”,“解决”,和“从地址”,你会开始运行!


我彻底迷路试图弄清楚这是怎么前几天的工作(我甚至不知道的SMTP server是什么),所以我希望这至少会帮助别人谁在类似的情况。

Here is a list of commonly used SMTP servers which may be helpful.

Net::SMTP是我唯一的选择,因为我无法安装这台电脑上的任何模块不来的activeperl标准,但如果你打算使用类似Gmail的具有更高的安全性,并且可能需要验证您将需要使用不止Net::SMTP发送您的消息。如果是这种情况,您可能需要查看Net::SMTP::SSL或其他邮件发送模块。


增加了奖励!

该脚本还会在Linux上工作;)

相关问题