如何使用perl与用户和SSL Auth进行SMTP连接并发送带有附件的电子邮件 [英] How to use perl for SMTP connection with user and SSL Auth and send emails with attachment

查看:72
本文介绍了如何使用perl与用户和SSL Auth进行SMTP连接并发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SMTP邮件服务器,该服务器需要用户+ ssl身份验证才能进行连接.我正在寻找连接到邮件服务器并发送电子邮件的perl模块,但没有任何帮助.

I am using a SMTP mail server which require user + ssl authentication for connection. I am looking for the perl modules to connect to the mail server and send emails but doesn't found anything helpful.

任何对perl模块或任何perl代码的建议都将不胜感激.

Any suggestion for perl module or any perl code would be really appreciated.

我尝试使用Mail :: Sendmail和Net :: SMTP :: SSL连接到sendmail服务器并发送邮件.下面是示例代码,但使错误用户未知.

I have tried to use Mail::Sendmail and Net::SMTP::SSL to connect to the sendmail server and send mail. Below is the sample code but getting the error user unknown.

错误:

mail: Net::SMTP::SSL=GLOB(0x9599850) not found 
RCPT TO: error (550 5.1.1 <user@mail.com>... User unknown).

代码:

#!/usr/bin/perl

use strict;
use warnings;
use Mail::Sendmail;
use Net::SMTP::SSL;

my %mail = (
From=> 'user1@server.com',
To=> 'user2@server.com',
# Cc will appear in the header. (Bcc will not)
Subject => 'Test message',
'X-Mailer' => "Mail::Sendmail version $Mail::Sendmail::VERSION",
);

$mail{Smtp} = Net::SMTP::SSL->new("mail.server.com", Port=> 465, Debug=>1);
$mail{auth} = {user=>'username', password=>"password", required=>1 };
$mail{'X-custom'} = 'My custom additionnal header';
$mail{Message} = "The message key looks terrible, but works.";
# cheat on the date:
$mail{Date} = Mail::Sendmail::time_to_date( time() - 86400 );
if (sendmail %mail) { print "Mail sent OK.\n" }
else { print "Error sending mail: $Mail::Sendmail::error \n" }

print "\n\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log;

推荐答案

我假设您已验证以下行中的数据:

I assume you verified the data in this line:

$mail{auth} = {user=>'username', password=>"password", required=>1 };

在mail.server.com上是否知道密码为"password"的用户"username"(在您的真实代码"user@mail.com"中?)?

is the user 'username' (in your reallife code 'user@mail.com'?) with the password 'password' known at mail.server.com?

如果没有,我会期望出现User unknown错误.

If not, I would expect a User unknown error.

EDIT1 我只是看到您的邮件中没有收件人",抄送"中只有一个抄送,您的邮件服务器可能不是这样(我不介意,于是:-),或者是在整理邮件时出现了偶然的情况"代码?

EDIT1 I just saw that you have no 'To' in that mail, 'only' a cc, might your mail server not like that (mine didn't mind, so then :-), or did that 'happen' in trimming down the code?

EDIT2 通过替换该行,我能够重现您的错误

EDIT2 I was able to reproduce your error by replacing the line

$mail{Smtp} = Net::SMTP::SSL->new("mail.server.com", Port=> 465);

$mail{Smtp} = Net::SMTP::SSL->new("smtp.mail.com", Port=> 465);

您需要给邮寄者有效的地址,才能将邮件发送到!当我向地址提供现有地址时(Cc=>'user2@server.com'行..它起作用了!

you need to give the mailserer a valid address to send the message to! When I supplied an existing to address (the Cc=>'user2@server.com' line .. it worked!

这篇关于如何使用perl与用户和SSL Auth进行SMTP连接并发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆