vkk05
vkk05

Reputation: 3222

Send an email in Perl using sendmail command

I have written a script which needs to send an email to particular user.

Below is the script:

#!/usr/bin/perl

use strict; 
use warnings;

my $filename = "/usr/people/vkk/Downloads/report_daily_2022_08_30.txt";

my $mailprog='/usr/'.($^O eq 'irix'?'lib':'sbin').'/sendmail';

`echo "Subject:Report" | $mailprog -t $ENV{'USERMAIL'} < $filename `;

print "Done\n";

The script works fine, but as I have mentioned Subject as an Report, this fails to show the subject in the email.

How can I add the Subject in this case?

I don't want to use mail command, since attachments are sent as an attached file.

Using sendmail attachment are been shown in the message body itself while we open the email. So needed to achieve this in sendmail command itself.

Upvotes: 0

Views: 220

Answers (1)

brian d foy
brian d foy

Reputation: 132905

Typically you don't want to do these things in backticks. You also said you don't want to use modules.

You can open a pipe and write to it:

open my $ph, '|-', $mailprog, '-t', $ENV{'USERMAIL'};
print { $ph } ...stuff...
close $ph or die "Problem with $mailprog: $!";

With that, you can print the headers, then feed it the message instead of trying to pipe and redirect in the same shell statement. But this is kinda old school.

Upvotes: 2

Related Questions