2015-02-05 82 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上工作;)

相關問題