2012-06-07 60 views
2

我正嘗試使用Perl發送電子郵件。基本上我有一個Perl腳本,可以以很好的格式打印報告。我希望通過電子郵件發送該報告。我怎樣才能做到這一點?使用Perl發送電子郵件

+0

[?什麼是發送電子郵件的最好的Perl模塊(HTTP :(http://stackoverflow.com/questions/338896/what-is-the-best-perl-module-for-sending-email),[我應該使用哪個CPAN包發送郵件?](http:// stackoverflow。 com/questions/2919493/which-package-from-cpan-should-i-use-to-send-mail) – daxim

+0

CPAN是你的朋友d,新的或舊的Perl。例如,嘗試[電子郵件::發件人](https://metacpan.org/module/Email::Sender)。好的文檔在[快速入門手冊](https://metacpan.org/module/Email::Sender::Manual::QuickStart)。 – asjo

+0

使用Perl SMTP http://perldoc.perl.org/Net/SMTP.html –

回答

3

MIME::Lite是許多人使用的強大模塊。它很容易使用,包括如果你想附加文件。

use MIME::Lite; 
my $msg = MIME::Lite->new(
    From => $from, 
    To  => $to, 
    Subject => $subject, 
    Type => 'text/plain', 
    Data => $message, 
); 
$msg->send; 

由於它使用默認sendmail(而不是SMTP),你甚至都不需要進行配置。

+6

我知道這個答案是舊的,但MIME :: Lite文檔現在說:'等待! MIME :: Lite不被其當前維護者推薦。有許多替代方法,比如Email :: MIME或MIME :: Entity和Email :: Sender,你應該使用它。 MIME :: Lite繼續累積怪異的錯誤報告,並且由於更好的替代方案的可用性,它不會接收大量的重構。請考慮使用別的東西。' – ThisSuitIsBlackNot

3

值得一提的是,如果你碰巧有你的機器上Outlook和CPAN觀模塊:

# create the object 
use Mail::Outlook; 
my $outlook = new Mail::Outlook(); 

    # start with a folder 
    my $outlook = new Mail::Outlook('Inbox'); 

    # use the Win32::OLE::Const definitions 
    use Mail::Outlook; 
    use Win32::OLE::Const 'Microsoft Outlook'; 
    my $outlook = new Mail::Outlook(olInbox); 

    # get/set the current folder 
    my $folder = $outlook->folder(); 
    my $folder = $outlook->folder('Inbox'); 

    # get the first/last/next/previous message 
    my $message = $folder->first(); 
    $message = $folder->next(); 
    $message = $folder->last(); 
    $message = $folder->previous(); 

# read the attributes of the current message 
my $text = $message->From(); 
$text = $message->To(); 
$text = $message->Cc(); 
$text = $message->Bcc(); 
$text = $message->Subject(); 
$text = $message->Body(); 
    my @list = $message->Attach(); 

    # use Outlook to display the current message 
    $message->display; 


    # Or use a hash 
    my %hash = (
    To  => '[email protected]', 
    Subject => 'Blah Blah Blah', 
    Body => 'Yadda Yadda Yadda', 
); 

    my $message = $outlook->create(%hash); 
    $message->display(%hash); 
    $message->send(%hash); 

注意,.invalid TLD是不是真實的,所以上面的地址將無法實現。無論如何,我已經在模塊中提供了一些體面的解釋 - 這會發送一條消息!

+0

@Grep培根我試過這段代碼,但錯誤是'不能調用方法'從'在mail.pl第24行未定義的值「。在這段代碼中。我能做什麼? – mkHun

+0

@Hussain不要完全按照原樣運行此代碼 - 這只是一個CPAN部分,顯示您可以執行的所有方法。在第24行之前,它先運行的代碼(),next(),last()和previous()。您不應該將所有這些背對背運行......可能會嘗試訪問沒有From()的內容。 另外,在所有選擇當前文件夾兩次之前......不妨將它清理乾淨,只需抓住「收件箱」即可。 – PinkElephantsOnParade

+0

嗨,我遇到了這個問題,似乎'$ outlook = new Mail :: Outlook()'返回undef ...爲什麼是這樣?我已經安裝了模塊,我有Outlook 2007 ...我如何創建$ outlook對象? – catzilla

4

如果sendmail的配置的機器沒有,我通常使用Mail::Sendmail

use Mail::Sendmail; 

%mail = (smtp => 'my.isp.com:25', 
     to  => '[email protected]', 
     from => '[email protected]', 
     subject => 'Automatic greetings', 
     message => 'Hello there'); 

sendmail(%mail) or die; 
+0

+1 ::這對我有用。我使用了'localhost:25' –

0

最簡單的方式,而不CPAN庫:

#!/usr/bin/perl 

$to = '[email protected]';  # to address 
$from = '[email protected]'; # from address 
$subject = 'subject';   # email subject 
$body = 'Email message content';# message 

open(MAIL, "|/usr/sbin/sendmail -t");  
print MAIL "To: $to\n"; 
print MAIL "From: $from\n"; 
print MAIL "Subject: $subject\n\n"; 
print MAIL $body;  
close(MAIL); 

print "Email Sent Successfully to $to\n";