如何使用带附件的perl发送邮件?

时间:2013-04-02 10:24:16

标签: perl email smtp sendmail

我尝试使用带有Email::StuffMIME::Lite尝试的附件的perl发送邮件,在运行期间我收到一些错误Authentication failed或服务器未连接可以任何人帮我? 相应的代码是:

   use MIME::Lite;
use Net::SMTP;
### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'atme04@gmail.com';
my $to_address = 'thiyagu040@gmail.com';
my $mail_host = 'smtp.gmail.com';

### Adjust subject and body message
my $subject = 'A message with 2 parts ...';
my $message_body = "Here's the attachment file(s) you wanted";
my $your_file_zip = 'my.zip';

$msg = MIME::Lite->new (
  From => $from_address,
  To => $to_address,
  Subject => $subject,
  Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
MIME::Lite->send('smtp', 'smtp.gmail.com' ,
                          Port =>465 ,
                          Timeout=>320 ,
                          Debug => 1 ,
                          Hello => $mail_host,
                          User  => $from_address,
                          Password  => 'Thiyagu.04' );
#$mime_msg->send() or die "Error sending message: $!\n";

#MIME::Lite->send('smtp',$mail_host,AuthUser=> $from_address, AuthPass=>"apssword");

$msg->send();

错误信息是;

 SMTP Failed to connect to mail server: A connection attempt failed because the connected party did not properly respond aft
    er a period of time, or established connection failed because connected host has failed to respond.
     at mail.pl line 54.

提前致谢

1 个答案:

答案 0 :(得分:1)

错误消息对我来说非常清楚。问题在于您与邮件服务器的连接 - 因此更改您正在使用的模块不太可能实现任何有用的功能。我对Gmail的服务器设置了解不足以评论问题所在,但this page提供了一些您可能会遵循的建议。具体来说,您可以检查Gmail帐户是否已启用“允许身份验证”,以及您的邮件客户端(Perl程序)是否使用SSL进行连接。

此外,如果您使用专门设计用于Gmail的电子邮件,这可能会更容易。 Email::Send::SMTP::Gmail看起来适合您的要求。