Send an Email Perl Using MIME::Lite - perl

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.

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;

sending multipart mail in perl

i am trying to send a mail through Perl script using net::smtp module.It works fine when i send the normal mail without any attachment.i wont receive any mail.
use Net::SMTP;
use MIME::Base64;
use File::Basename;
use MIME::Base64 qw( encode_base64 );
use MIME::Base64 qw( decode_base64 );
#attachments = 'C:\Users\ups7kor\Desktop\scripts\commadnline\appending.pl';
$toAddress = '***';
$fromAddress = '***';
$ServerName = '***';
my $boundary = 'End Of mail';
my $smtp = Net::SMTP->new($ServerName, Timeout => 60) or print $failureLogHandler ++$errrorCount.")ERROR:Could not create SMTP object . \n\t please check SMPT Adress in $iniFileData{INI_SMTP_SERVER_NAME} of $iniFileSection{INI_EMAIL} section ";
$smtp->mail($fromAddress);
$smtp->recipient($toAddress, { SkipBad => 1 });
$smtp->data();
$smtp->datasend("To: $toAddress\n");
$smtp->datasend("From: $fromAddress\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n");
$smtp->datasend("--$boundary\n");
$smtp->datasend("Content-type: text/plain\n");
$smtp->datasend("Content-Disposition: quoted-printable\n");
$smtp->datasend("\n $messageBody\n");
if(#attachments)
{
$smtp->datasend("--$boundary\n");
foreach $attachment (#attachments)
{
open(DAT, $attachment) || die("Could not open text file!");
my #textFile = <DAT>;
close(DAT);
my $filename = basename($attachment);
$smtp->datasend("Content-Type: application/text; name=\"$filename\"\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$filename\"\n");
$smtp->datasend("\n");
$smtp->datasend("#textFile\n");
}
}
$smtp->datasend("--$boundary --\n");
$smtp->dataend();
$smtp->quit;
But if i try the same code in other machine it works file.
Why the same code is not working in my machine and working fine in other machine.
Please help out.
You're using some rather low-level tools for building your message. That would probably work, but you'd need to implement all of the rules for building MIME messages - which sounds far too much like hard work.
Whenever I want to do something with email and Perl, I look for the appropriate module in the Email::* namespace. I'd probably start with Email::MIME, but I note that now includes a pointer to Email::Stuffer, which might well be even simpler.
You could use MIME::Lite module.
See: https://metacpan.org/pod/MIME::Lite#Create-a-multipart-message
Synopsis:
### Create the multipart "container":
$msg = MIME::Lite->new(
From =>'me#myhost.com',
To =>'you#yourhost.com',
Cc =>'some#other.com, some#more.com',
Subject =>'A message with 2 parts...',
Type =>'multipart/mixed'
);
### Add the text message part:
### (Note that "attach" has same arguments as "new"):
$msg->attach(
Type =>'TEXT',
Data =>"Here's the GIF file you wanted"
);
### Add the image part:
$msg->attach(
Type =>'image/gif',
Path =>'aaa000123.gif',
Filename =>'logo.gif',
Disposition => 'attachment'
);
Update: As per Dave's comment:
Check out Email::Stuffer module. Creating multipart message with it is really simple.
Email::Stuffer->to('Simon Cozens<simon#somewhere.jp>')
->from('Santa#northpole.org')
->text_body("You've been good this year. No coal for you.")
->attach_file('choochoo.gif')
->send;
You can use Mail::Sender module to send mails with attachment with body included. Just a small example of how to implement if you have this module in place.
my $sender = new Mail::Sender {smtp => 'server name', from =>
'emailId'};
$sender->MailFile( {to => 'xxx.gmail.com,yyy.gmail.com', subject => 'some subject that you want to put',
msg => "Body of the mail", file => 'path for the attachment that you need to send'} );

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 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.

how to send SMTP email in perl

Below is what I wrote to send an email from my mailhost to my individual email address and the error I'm getting.
Could someone please help me with why we are getting the error:
Can't call method "mail" on an undefined value at cmm_ping.pl line 2.
use Net::SMTP;
$smtp->mail("jo-sched#abcd.com");
$smtp->recipient("Myname#XXX-XXXX.com");
$smtp->datasend("From: jo-sched#abcd.com");
$smtp->datasend("To: Myname#xxxx-xxxxxx.com");
$smtp->datasend("Subject: This is a test");
$smtp->datasend("\n");
$smtp->datasend("This is a test");
$smtp->dataend;
$smtp->quit;
The variable $smtp has not yet been defined. Take a look at the usage examples of Net::SMTP. This example pretty much does what your code shall do:
use Net::SMTP;
$smtp = Net::SMTP->new('mailhost');
$smtp->mail($ENV{USER});
$smtp->to('postmaster');
$smtp->data();
$smtp->datasend("To: postmaster\n");
$smtp->datasend("\n");
$smtp->datasend("A simple test message\n");
$smtp->dataend();
$smtp->quit;
Are you familiar with how Object Oriented Perl works?
In order to use an object oriented Perl module, you have to first create an object of that class type. Normally, this is done via the new method:
my $smtp = Net::SMTP->new($mailhost);
Now, $smtp is an object of class Net::SMTP. Basically, it's a reference to a glob where you can store your data structure (who are you sending to, your message, etc.). Then Perl can use this information during method calls (which are just subroutines that are part of the package Net::SMTP).
Here's an example from a program I wrote:
use Net::SMTP;
my $smtp = Net::SMTP->new(
Host => $watch->Smtp_Host,
Debug => $debug_level,
);
if ( not defined $smtp ) {
croak qq(Unable to connect to mailhost "#{[$watch->Smtp_Host]}");
}
if ($smtp_user) {
$smtp->auth( $watch->Smtp_User, $watch->Smtp_Password )
or croak
qq(Unable to connect to mailhost "#{[$watch->Smtp_Host]}")
. qq( as user "#{[$watch->Smtp_User]}");
}
if ( not $smtp->mail( $watch->Sender ) ) {
carp qq(Cannot send as user "#{[$watch->Sender]}")
. qq( on mailhost "#{[$watch->Smtp_Host]}");
next;
}
if ( not $smtp->to($email) ) {
$smtp->reset;
next; #Can't send email to this address. Skip it
}
#
# Prepare Message
#
# In Net::SMTP, the Subject and the To fields are actually part
# of the message with a separate blank line separating the
# actual message from the header.
#
my $message = $watch->Munge_Message( $watcher, $email );
my $subject =
$watch->Munge_Message( $watcher, $email, $watch->Subject );
$message = "To: $email\n" . "Subject: $subject\n\n" . $message;
$smtp->data;
$smtp->datasend("$message");
$smtp->dataend;
$smtp->quit;