MIME::Lite - Cannot send mail [SMTP auth() command not supported on smtp.gmail.com] - perl

use MIME::Lite;
use warnings;
use MIME::Base64;
use Authen::SASL;
use MIME::Lite;
use MIME::Base64;
use Authen::SASL;
use warnings;
use Net::SMTP::TLS;
use Data::Dumper;
use MIME::Lite;
$to = 'pratapchintha#gmail.com';
$cc = 'pratapchintha#gmail.com';
$from = 'pratapchintha#gmail.com';
$subject = 'Test Email';
$message = 'email';
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Type => 'multipart/mixed'
);
$msg->attach(Type => 'text',
Data => $message
);
$msg->attach(Type => 'image/gif',
Path => 'C:\Users\chintpra\Desktop\excel\Feb_4.xls',
Filename => 'logo.gif',
Disposition => 'attachment'
);
$msg->send('smtp', "smtp.gmail.com", AuthUser=>"$from", AuthPass=>"*******",Debug=>1);
print "Email Sent Successfully\n";
output
MIME::Lite::SMTP>>> MIME::Lite::SMTP
MIME::Lite::SMTP>>> Net::SMTP(3.05)
MIME::Lite::SMTP>>> Net::Cmd(3.05)
MIME::Lite::SMTP>>> Exporter(5.67)
MIME::Lite::SMTP>>> IO::Socket::INET(1.33)
MIME::Lite::SMTP>>> IO::Socket(1.34)
MIME::Lite::SMTP>>> IO::Handle(1.33)
MIME::Lite::SMTP=GLOB(0x2c030e0)<<< 220 mx.google.com ESMTP gj9sm3721288pbc.32 - gsmtp
MIME::Lite::SMTP=GLOB(0x2c030e0)>>> EHLO localhost.localdomain
MIME::Lite::SMTP=GLOB(0x2c030e0)<<< 250-mx.google.com at your service, [123.63.237.69]
MIME::Lite::SMTP=GLOB(0x2c030e0)<<< 250-SIZE 35882577
MIME::Lite::SMTP=GLOB(0x2c030e0)<<< 250-8BITMIME`enter code here`
MIME::Lite::SMTP=GLOB(0x2c030e0)<<< 250-STARTTLS
MIME::Lite::SMTP=GLOB(0x2c030e0)<<< 250-ENHANCEDSTATUSCODES
MIME::Lite::SMTP=GLOB(0x2c030e0)<<< 250-PIPELINING
MIME::Lite::SMTP=GLOB(0x2c030e0)<<< 250-CHUNKING
MIME::Lite::SMTP=GLOB(0x2c030e0)<<< 250 SMTPUTF8
SMTP auth() command not supported on smtp.gmail.com
Can anyone let me know whats going wrong and how to fix it ?

MIME::Lite - sending via Gmail [SMTPS - Net::SMTP 3.05]
WARNING: MIME::Lite filters parameters passed to Net::SMTP - see MIME::Lite 3.030 - NET::SMTP with smtps (port 465)
AFAIK Gmail offers SMTP AUTH overecypted connections
(over SMTPS connections or after STARTTLS SMTP command).
With Net::SMTP 3.05 you may use SMTPS as a clean fix.
Net::SMTP versions below 3.0 do not support SMTPS.
[ WARNING: see MIME::Lite 3.030 - NET::SMTP with smtps (port 465) ]
$msg->send('smtp', "smtp.gmail.com",
SSL=>1,
AuthUser=>"$from", AuthPass=>"*******",
Debug=>1);
Net::SMTP 3.05 documentation

MIME::Lite is deprecated.
This works for me, using the preferred Email::Sender:
use strict;
use warnings;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTPS ();
use Email::Simple ();
use Email::Simple::Creator ();
my $smtpserver = 'server';
my $smtpport = 587;
my $smtpuser = 'username';
my $smtppassword = 'password';
my $transport = Email::Sender::Transport::SMTPS->new({
host => $smtpserver,
port => $smtpport,
ssl => "starttls",
sasl_username => $smtpuser,
sasl_password => $smtppassword,
});
my $email = Email::Simple->create(
header => [
To => 'mymail#gmail.com',
From => 'sender#example.com',
Subject => 'Hi!',
],
body => "This is my message\n",
);
sendmail($email, { transport => $transport });

Related

Perl Email function Mime::Lite

Im fairly new to Perl programming. I need help on the basic SMTP using MIME::Lite, I have already downloaded the library and put it on the folder.
Here is my code
#!/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";
I'm getting an error of:
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
Am I missing something here? I saw the tutorial and it is working for them.
Thanks for everyone's advice. So Since Mime::lite not that good I changed my approach to Net::SMTP. Here is my code below for future reference.
Also note that I was not able to make it work for google but our own smtp server work fine for the code.
#!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;

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 '\'

Perl Mail::Sendmail cant connect to SMTP server

I'm trying to send an email from a perl script but I'm having a bad time connecting to my SMTP server. I'm using MailCatcher- https://github.com/sj26/mailcatcher/ - running with it's default settings (localhost:1025).
MailCatcher is running and I can send mail to it with other services. Any clues as to what could be wrong? (Also tried sending a mail with Net::SMTP but that also failed to connect)
print "Testing Mail::Sendmail version $Mail::Sendmail::VERSION\n";
my %mail = (
To => 'temp#test.com',
From => 'test#test.com',
Subject => 'Test message'
);
$mail{server} = 'localhost:1025';
$mail{'mESSaGE : '} = "The message key looks terrible, but works.";
print Dumper(%mail);
if (sendmail %mail) { print "Mail sent OK.\n" }
else { print "Error sending mail: $Mail::Sendmail::error \n" }
Output
Testing Mail::Sendmail version 0.79
Default server: localhost
Default sender:
$VAR1 = 'Subject';
$VAR2 = 'Test message';
$VAR3 = 'server';
$VAR4 = 'localhost:1025';
$VAR5 = 'To';
$VAR6 = 'temp#test.com';
$VAR7 = 'message : ';
$VAR8 = 'The message key looks terrible, but works.';
$VAR9 = 'From';
$VAR10 = 'test#test.com';
Error sending mail: connect to localhost failed (Connection refused)
connect to localhost failed
connect to localhost failed (Connection refused)
connect to localhost failed
connect to localhost failed (Connection refused) no (more) retries!
EDIT:
Turned out mailcatcher was running on another port and wasn't giving an error when I was trying to start it up on 25.
You don't set the mail server and port in a header within the email; rather you set it in the %mailcfg hash:
use Mail::Sendmail qw(sendmail %mailcfg)
$mailcfg{port} = 1025; #localhost is already the default server...
You can try this one .. it worked for me ..
use strict;
use warnings;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP();
use Email::Simple();
use Email::Simple::Creator();
my $smtpserver = 'server';
my $smtpport = 25;
my $smtpuser = 'User_Name';
my $smtppassword = 'pass';
my $transport = Email::Sender::Transport::SMTP->new({
host => $smtpserver,
port => $smtpport,
sasl_username => $smtpuser,
sasl_password => $smtppassword,
});
my $email = Email::Simple->create(header => [
To => 'me#example.com',
From => 'sender#example.com',
Subject => 'Hello there!',
], body => "This is one actually worked !!\n",
);
sendmail($email, { transport => $transport });
?>

(perl Net::SMTP) authentication failure using gmail smtp server

I have written a perl script to send email using the gmail's smtp server: smtp.gmail.com -
use Net::SMTP;
my $smtp = Net::SMTP->new('smtp.gmail.com',
Port=> 587,
Timeout => 20,
Hello=>'user#gmail.com'
);
print $smtp->domain,"\n";
$sender = "user#gmail.com";
$password = "mypassword";
$smtp->auth ( $sender, $password ) or die "could not authenticate\n";
$receiver = "user#gmail.com";
$subject = "my custom subject";
$smtp->mail($sender);
$smtp->to($receiver);
$smtp->data();
$smtp->datasend("To: <$reciever> \n");
$smtp->datasend("From: <$sender> \n");
$smtp->datasend("Content-Type: text/html \n");
$smtp->datasend("Subject: $subject");
$smtp->datasend("\n");
$smtp->datasend('the body of the email');
$smtp->dataend();
$smtp->quit();
print "done\n\n";
For 'user', I have my gmail username and for 'mypassword' I have the password. But when I run this code it stops at the auth itself giving : could not authenticate.
I am able to connect to the smtp serever of google as I get : 'mx.google.com' as the result of :
print $smtp->domain,"\n";
What is it that I am doing wrong? Please help.
use Net::SMTP;
my $smtp = Net::SMTP->new ....
afaik you need to use an encrypted connection to send via gmail, use Net::SMTP::TLS or Net::SMTP::SSL (with port 465)
Hello=>'user#gmail.com'
'Hello' is not an email address, put your hostname there instead
$sender = "user#gmail.com";
...
$receiver = "user#gmail.com";
put these in single quotes
if you still get "could not authenticate." make sure you have the modules MIME::Base64 and Authen::SASL installed.
$smtp->datasend("To: <$reciever> \n");
should be $receiver, not $reciever
Gmail uses TLS/STARTTLS on port 587, SSL on port 465. Please follow Sending email through Google SMTP from Perl. Alternatively you could use Email::Send::SMTP::Gmail. I recommend this as it is way easier to use and has many more features.

Send an Email Perl Using MIME::Lite

I am just trying to send a basic email using Perl and MIME::Lite and I am receiving the following error: SMTP mail() command failed: 5.1.7 Invalid adderess
Here is my code:
#!perl
use MIME::Lite;
#Create Mail
$msg = MIME::Lite->new(
From =>'someone#someplace.com',
To =>'someone#someplace.com',
Cc =>'some#other.com',
Subject =>'Subject Test',
Data =>"Data Test"
);
#Send Mail
$msg->send( "smtp", "mail.place.com" );
Thanks.
I ended up solving it:
sub EMailReport
{
use MIME::Lite;
my $theSubject = "Sub";
my $theData = "Data";
my $theEmail = MIME::Lite->new(
From =>'From#someplace.somewhere.com',
To =>'fistname.lastname#company.com',
Subject =>$theSubject,
Data =>$theData
);
$theEmail->add( "Type" => "multipart/mixed" );
$theEmail->send( "smtp", "somemail.company.com" );
}
You need to pass to send() the smtp arguments I think.