Perl电子邮件功能Mime :: Lite

时间:2017-08-09 07:03:20

标签: perl email smtp

我是Perl编程的新手。我需要使用MIME :: Lite的基本SMTP帮助,我已经下载了库并将其放在该文件夹中。 这是我的代码

#!/usr/bin/perl

use MIME::Lite;
use Net::SMTP;

$to = 'sample@gmail.com';
$cc = 'sample@gmail.com';
$from = 'sample@gmail.com';
$pass = "my password here";
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Data     => $message
                 );

# MIME::Lite->send('smtp', "smtp.gmail.com");
MIME::Lite->send('smtp', "smtp.gmail.com", Timeout=>60,Auth=>'LOGIN',AuthUser=>$from,AuthPass=>$pass,Port => 465, Debug => 1);
$msg->send;
print "Email Sent Successfully\n";

我收到错误:

MIME::LITE::SMTP>>> MIME::LITE::SMTP
MIME::LITE::SMTP>>> NET::SMTP(3.10)
MIME::LITE::SMTP>>> NET::CMD(3.10)
MIME::LITE::SMTP>>> EXPORTER(5.72)
MIME::LITE::SMTP>>> IO::SOCKET::IP(0.39)
MIME::LITE::SMTP>>> IO::SOCKET(1.38)
MIME::LITE::SMTP>>> IO::HANDLE(1.36)
MIME::LITE::SMTP>>> NET::CMD::GETLINE(): UNEXPECTED EOF ON COMMAND CHANNEL: AT C:/STRAWBERRY/PERL/LIBE/MIME/LITE.PM LINE 2622 AT MAIN.PL LINE 23
SMTP FAILED TO CONNECT TO MAIL SERVER: BAD FILE DESCRIPTOR AT LINE 23

我在这里遗漏了什么吗?我看到了教程,它正在为他们工作。

1 个答案:

答案 0 :(得分:1)

感谢大家的建议。所以既然Mime :: lite不那么好我改变了我对Net :: SMTP的方法。以下是我的代码供将来参考。

另请注意,我无法让它适用于Google,但我们自己的smtp服务器可以正常运行代码。

#!perl
use warnings;
use strict;
use Net::SMTP;

my $smtpserver = 'mail server';
my $smtpport = 'port' ;
my $smtpuser   = 'sender@sample.com';
my $smtppassword = 'my password';


my $smtp = Net::SMTP->new($smtpserver, Port=>$smtpport, Timeout => 60, Debug => 1);
die "Could not connect to server!\n" unless $smtp; 

$smtp->auth($smtpuser, $smtppassword);
$smtp->mail('sender@sample.com');
$smtp->to('receiver@sample.com');

$smtp->data();
$smtp->datasend("To: receiver name <receiver\@sample.com>\n");
$smtp->datasend("From: sender name <sender\@sample.com>\n");
$smtp->datasend("subject: THIS IS A SUBJECT\n");
$smtp->datasend("\n");
$smtp->datasend("A simple test message\n");

$smtp->quit;