How to make a single variable perform as a script - perl

I've been using this module to write simple scripts to log into a server and perform a single command.
use Net::SSH qw(sshopen2);
use strict;
my $user = "username";
my $host = "hostname";
my $cmd = "command";
sshopen2("$user\#$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
while (<READER>) {
chomp();
print "$_\n";
}
close(READER);
close(WRITER);
But I need the $cmd variable to perform this operation below.
my $dir = "/srv/archive/$date";
my $time = `date`;
open my $LOG, '>>', '/srv/archive/test.log';
if ( ! -e $dir )
{
my $to = 'xxxxx#xxxxx.com';
my $from = 'node0#example.com';
my $subject = '**DROP FILE TEST ALERT**';
my $message = "Please check if the $date directory exists";
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# # Email Body
print MAIL $message;
close(MAIL);
print "Email Sent Successfully\n" ;
print $LOG "$time (Bad)";
}
else
{
print "The directory exists!\n";
print $LOG "$time (Good)\n";
}
The reason why I'm using this is because I am not allowed to execute a script inside the server. Thanks in advance.

Manually printing headers to sendmail is no longer a good way to send email. Modern email contains multiple, complicated guards against spammers. Most mail servers and clients will reject your mail as spam.
There's no reason the email has to be sent on the remote server. Instead, send the mail via a normal SMTP connection. Consider using Email::Sender instead.
If you absolutely must send the mail from the remote server, write a script to do it (also using Email::Sender) and invoke that script.

Related

Sending email using perl with sendmail

I am trying to send email using sendmail in perl. Email is sent but the content which I send as Subject gets added to 'To:' in the email. For example, if from address is from#gmail.com, To address is to#gmail.com and subject is "test subject". I get the email with Reply-to: field as from#gmail.com,"Subject:test.email","To:to"#gmail.com,"Content-type:text/plain"
Here is my code:
open(SENDMAIL, "|/usr/lib/sendmail -oi -t '$to_email' -f '$from_email'") || ($error_message .= "<P>Unable to open email process.</P>");
print SENDMAIL $reply_to;
print SENDMAIL $subject;
print SENDMAIL $send_to;
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $content;
close(SENDMAIL);
And if I remove $reply_to and $send_to lines, the email comes with from: field
as Apache server.
Any help would be appreciated. I do not want to use any other library like Email::MIME since it doesn't exist on my system.
I am not sure what is wrong with what you show, but here is some working code
my ($header, $From, $To, $Cc, $ReplyTo, $Subject);
$From = "From: $from";
$To = "To: $to";
$Cc = "Cc: $cc_addresses";
$ReplyTo = "Reply-To: $replyto";
$Subject = "Subject: $subj";
# Form the header. The fields always submitted:
$header = join("\n", $From, $To, $ReplyTo, $Subject, '');
# Optional fields
$header .= "$Cc\n" if $cc_addresses;
open(SENDMAIL, "|/usr/sbin/sendmail -oi -t")
or carp "Can't fork for sendmail: $!\n";
say SENDMAIL "$header\n$msg_cont";
close(SENDMAIL);
Note. There is my $cc_addresses = ''; earlier, which then (possibly) gets built up.

How is it possible to use a special character (a star) in the "from" field using Perl's sendmail

I am using Perl's SENDMAIL to receive email from my website:
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL "From: $from\n";
print SENDMAIL "Subject: $subject\n";
print SENDMAIL "To: $to\n";
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $message;
close(SENDMAIL);
And I would like to modify the $from variable to be something like this:
$from = "★ David Jones <david.jones#oozicle.com>"
What happens right now is that I just see the ampersand etc., and the star is not shown.
I know that it is possible to use special characters because I receive spam containing them.
Is it possible to do this using SENDMAIL?
use Encode qw( encode );
my $name = "\x{2605} David Jones";
my $addr = 'david.jones#oozicle.com';
my $from_header = encode('MIME-Header', $name) . ' <' . $addr . '>';

attachment displaying full path when emailed using perl script

My perl script emails attachment which displays full path (/abc/xyz/report.xls) in outlook email client but not in lotus notes email client.
environment: 2010 Outlook on windows 7 OS.
How do I make sure path is not displayed in the attachment?
Code Snippet:
&sendEmail(#subject, #message, $exception_report_xls);
sub sendEmail
{
my $subject = "#subject";
my $distlist = 'ayc#abc.com';
my $message ="\n\n\n#message \n";
my $send = system("\(echo \"$message\" ; uuencode \"$exception_report_xls\" \"$exception_report_xls\"\) | mailx -s \"$subject\" \"$distlist\"");
}
Appreciate any resonse!
Ramya.
You are writing the full path and filename of the XLS file in the source of the message. So, some mail clients will display the full path.
If you are wedded to using (antiquated) UUENCODE, try it this way instead:
sub Send_Email_UUENCODE {
local($to, $from, $subj, $message, $attach) = #_;
local($i,$filename,$cmd,$att,$mailprog);
$i=length($attach)-1;
while( (substr($attach,$i,1) cmp '/')!=0 && $i>0) {$i--;}
$filename=substr($attach,$i+1,length($attach)-$i-1);
if ($attach) {
$cmd="uuencode " . $attach . ' ' . $filename;
$att=`$cmd`;
}
$mailprog = "/usr/sbin/sendmail -f " . $from;
open (MAIL, "|$mailprog $to") || die "Can't open $mailprog!\n";
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subj\n";
print MAIL "$message\n";
if ($attach) {
print MAIL "$att\n";
}
close (MAIL);
}
But, better might be to use MIME encoding instead of UUENCODE.

not able to send bulkmail through sendmail

I am trying to send e-mail to a group but not being able to send
catch is my script is sending e-mail to individual id, but not group.
Googled it but not much helpful.
For sending to bulk users i don't want to use alias, some restrictions.
Please Advice
#!/usr/bin/perl
#!/usr/sbin/sendmail
$to = 'xxxx#yyy.something.com,';
$from = 'abc#something.com';
$subject = 'Subject';
#$message = 'This is test mail';
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL "print something";
close(MAIL);
#print "Email Sent Successfully\n
Pass list of recipients as sendmail command line arguments - AFAIR it should work on Linuxes for a few hundredth recipients.
#!/usr/bin/perl
use strict;
use warnings;
my #to = ('xxxx#yyy.something.com','yyyy#xxx.something.com');
my $from = 'abc#something.com';
my $subject = 'Subject';
#my $child_pid = open(MAIL, "|-") // die "can't fork: $!";
defined( my $child_pid = open(MAIL, "|-")) || die "can't fork: $!";
if( $child_pid == 0 ) {
exec( '/usr/sbin/sendmail', '-i', '--', #to) || die "can't exec: $!";
}
# Email Headers & Body
print MAIL << "END" ;
From: $from
Subject: $subject
print something
END
close(MAIL) && print "Email Sent Successfully\n";

I'm sending a test email using perl but it comes back with system can't find path specified

I've tried:
#!/usr/bin/perl
$to = 'abcd#gmail.com';
$from = 'webmaster#yourdomain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;
close(MAIL);
print "Email Sent Successfully\n"
but it comes back with The system cannot find the path specified.
I've also tried:
#!/usr/bin/perl
use MIME::Lite;
$to = 'abcd#gmail.com';
$cc = 'efgh#mail.com';
$from = 'webmaster#yourdomain.com';
$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
);
$msg->send;
print "Email Sent Successfully\n";
but it comes back with SMTP Failed to connect to mail server: No such file or directory at G:\email_test.pl line 18
How do I fix this/these issues so I can send an email successfully? These seem to be the two common examples to use when sending email using PERL and I can't get them to work.
If you're getting G:\email_test.pl, I am assuming you're using a Windows machine. I see open(MAIL, "|/usr/sbin/sendmail -t"); in your program which refers to a program on a Unix/Linux system.
In Perl, the Net::SMTP module comes with almost all Perl distributions. I highly recommend people to use that unless they need to MIME encode mail.
The module is pretty simple to use too although admittedly not as simple as other email modules. Net::SMTP assumes you know how SMTP works. Fortunately, the S in SMTP stands for Simple. Of course, the people who called it Simple is the same group that thought Emacs was an intuitive program editor.
# /usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use constant {
SMTP_HOST => 'mailhost',
TO => 'abcd#gmail.com',
FROM => 'efgh#mail.com',
SUBJECT => 'Test Email',
USER => 'question_guy',
PASSWORD => 'swordfish,
};
my $smtp = Net::SMTP->new( SMTP_HOST ) # This is your SMTP host
or die qq(Cannot create Net::SMTP Object);
$smtp->auth( USER, PASSWORD ) # If you have to use authentication
or die qq(Can't authenticate into ) . SMTP_HOST;
$smtp->mail( FROM );
$smtp->to( TO );
my $message = "Subject: " . SUBJECT . "\n"
. "To: " . TO . "\n"
. 'This is test email sent by Perl Script';
$smtp->data;
$smtp->datasend( $message );
$smtp->dataend;
$smtp->quit;
This above wasn't tested, but I was typing it from a program I wrote that used Net::SMTP. Take a look at the Net::SMTP documentation and play around with it.