简体   繁体   中英

Trying to send authenticated email using Net::SMTP::SSL

I'm attempting to send an authenticated email with the Net::SMTP::SSL module to a comcast email server. I'm using the following code.

#!/usr/bin/perl

use Net::SMTP::SSL;
use MIME::Base64;

$smtp = Net::SMTP::SSL->new
    (
    "smtp.comcast.net",
    Hello => "host.comcast.net",
    Port => 465,
    Timeout => 30,
    Debug => 1,
    );
$smtp->datasend("AUTH LOGIN\n");
$smtp->response();

# Mailbox info
$smtp->datasend(encode_base64('username')); # username
$smtp->response();
$smtp->datasend(encode_base64('password')); # password
$smtp->response();

# Email from
$smtp->mail('user\@comcast.net');

# Email to
$smtp->to('user\@host.com');

$smtp->data();

$smtp->datasend("To: user\@host.com\n");
$smtp->datasend("From: user\@comcast.net\n");
$smtp->datasend("Subject: Test");

# Line break to separate headers from body
$smtp->datasend("\n");

$smtp->datasend("Blah\n");
$smtp->dataend();

$smtp->quit();
exit;

I'm basically following the code from here for comcast.

I've ran telnet and can connect to the smtp server on the port and I can issue the AUTH LOGIN and successfully login, but issuing the

$smtp->datasend("AUTH LOGIN\n");

always results in:

Can't call method "datasend" on an undefined value

I've also tried executing the auth method to login and that fails as well.

What am I missing here? I know it's something simple I'm overlooking.

Pretty much all of the Net::SMTP methods optionally set error codes, so I suspect that Net::SMTP::SSL does the same. Try the following for your constructor:

use Net::SMTP::SSL;
use MIME::Base64;

use strict;
use warnings;

my $smtp = Net::SMTP::SSL->new(
    "smtp.comcast.net",
    Hello => "host.comcast.net",
    Port => 465,
    Timeout => 30,
    Debug => 1,
) or die "Failed to connect to mail server: $!";

Also, the fact that your smtp server is different than your Hello is a little suspect to me.

For email services that use STARTTLS, it's best to use the newer NET::SMTPS module. Try the following code:

my $msg = MIME::Lite ->new (  
From => 'from@bellsouth.net',
To => 'to@bellsouth.net',
Subject => 'Test Message',
Data => 'This is a test',
Type => 'text/html'
);

my $USERNAME = 'from@bellsouth.net';
my $PASSWORD = 'abc123'; 

my $smtps = Net::SMTPS->new("smtp.mail.att.net", Port => 587,  doSSL => 'starttls', SSL_version=>'TLSv1');

$smtps->auth ( $USERNAME, $PASSWORD ) or die("Could not authenticate with bellsouth.\n");

$smtps ->mail('from@bellsouth.net');
$smtps->to('to@bellsouth.net');
$smtps->data();
$smtps->datasend( $msg->as_string() );  
$smtps->dataend();  
$smtps->quit;

Originally from http://www.skipser.com/p/2/p/send-email-using-perl-via-live.com.html

Try this:

Email from

Change

$smtp->mail('user\\@comcast.net'); to $smtp->mail('user@comcast.net'); without '\\'

Email to

Change

$smtp->to('user\\@host.com'); to $smtp->to('user@host.com'); without '\\'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM