![](/img/trans.png)
[英]Using a perl script on windows without installing ActivePerl?
[英]Sending an email in Perl on Windows 7 without installing modules
我一直在摸索試圖找出如何從Windows 7上的Perl發送電子郵件的方法。除了Active Perl安裝的默認值之外,我無法安裝任何Perl模塊。 (這意味着Mime :: Lite,Email :: Sender和Mail :: Sendmail不在桌面上)
我可以找到的唯一電子郵件發送模塊似乎是Net :: SMTP,但是我一直無法弄清楚如何正確使用它。 我對SMTP服務器不是很熟悉,更不用說它們如何工作了。 我發現的其他所有有關發送電子郵件的帖子都建議使用我沒有的其他模塊。
我看到了另一條建議使用Net :: SMTP :: SSL連接到gmail的帖子 ,但是我沒有SSL模塊。
到目前為止,這是一些代碼,我正在嘗試使用gmail作為SMTP服務器:
use Net::SMTP;
$smtp = Net::SMTP->new('smtp.gmail.com'); # connect to an SMTP server
$smtp->mail('fromAddress@gmail.com'); # use the sender's address here
$smtp->to('toAddress@test.com'); # 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
我不斷收到錯誤:
無法在未定義的值上調用方法“郵件”
我認為這意味着它無法連接到SMTP服務器。 我怎樣才能解決這個問題?
還有Active Perl隨附的其他標准模塊,這些模塊更易於使用嗎?
我真的在尋找類似於Linux SENDMAIL命令的東西,它非常簡單,甚至不需要您連接或認證任何東西。 Linux SENDMAIL命令似乎甚至允許您組成您想要的任何“發件人”地址,這可能確實很危險,但是非常棒!
編輯
也不要求我通過gmail。 這只是我想到的第一件事。
事實證明,解決方案只是向我公司的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服務器是什么),所以我希望這至少可以幫助處於類似情況的其他人。
這是一個可能有用的常用SMTP服務器列表 。
Net::SMTP
是我唯一的選擇,因為我無法在此PC上安裝ActivePerl不標配的任何模塊,但是如果您打算使用像gmail這樣的具有更高安全性並可能需要身份驗證的模塊,則需要使用不僅僅是Net::SMTP
來發送消息。 在這種情況下,您可能需要研究Net::SMTP::SSL
或其他郵件發送模塊。
增加了獎金!
該腳本還將在Linux上運行;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.