Sending email using perl with sendmail - perl

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.

Related

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

How to display the name of the sender's mail when sending a mail using Perl?

I am sending a mail using the following code in Perl
#!/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";
Now how do i get the name of the sender while i am seeing the name. For eg. when i keep $from as abcd#xyz.com it displays name as abc while i want to give it my name like James Lawson.?

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.

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.

Sendmail Format

Not sure where I am doing wrong, but when I am putting $hostname variable within "$from" variable, sendmail is not printing anything within subject line, else it works fine.
I want to put "from" as noreply#hostname.
Here is the code :
my $f="test.txt";
my $h=`hostname`;
if ( -s "$f" ) {
my $to='user#example.com';
my $from= "noreply\#$h";
my $subject='Test Error';
open(MAIL, "|/usr/sbin/sendmail -t");
## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "Test Body\n";
close(MAIL);
}
Your $h has a trailing newline on the end of it.
Add a chomp to fix it:
my $h=`hostname`;
chomp $h;
Also, it's a good idea to use lexical variables instead of global filehandles, and the three-argument form of open:
open my $mail, '|-', '/usr/sbin/sendmail -t';
## Mail Header
print $mail "To: $to\n";
print $mail "From: $from\n";
print $mail "Subject: $subject\n\n";
## Mail Body
print $mail "Test Body\n";
close $mail;

Categories