-1

This is the code I am using for sending mail in perl . But I didn't receive mail .

#!/usr/bin/perl
print "Content-type: text/html\n\n";
$to = 'to@gmail.com';
$title='Perl Mail demo';
$from= 'from@gmail.com';
$subject='YOUR SUBJECT';

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 "This is a test message from Cyberciti.biz! You can write your
mail body text here\n";

close(MAIL);

I am not getting any errors but unable to find the reason why mail is not sending.

Please help me I have tried several examples and forums but nothing works.

This is happening after server migration

I know this question has been asked several times and none of them helped me.

  • Use mail(1). Might be mailx(1) on your system. – jira Nov 06 '17 at 12:26
  • Tell us a lot more about this server migration. What were the old and new operating systems. What versions? Are the two servers in the same location? Are they on the same network? Can you send email manually from the new server using `mailx` or something like that? Think like a programmer. Debug the problem. – Dave Cross Nov 07 '17 at 08:49
  • actually i am a php guy and i am new to perl. The project is using both php and perl ,i am able to send mail from php but not from perl – Prasanna Venkatesh Nov 07 '17 at 08:54
  • 1
    Ok. So how do you send mail from PHP? What is the difference between what you do in PHP and what you do in Perl? **Please update your question to add more details.** – Dave Cross Nov 07 '17 at 08:58
  • i used default php mail function and it is working well. Actually when I see the phpinfo sendmail path is '/usr/sbin/sendmail -t -i'. Does it matter? Even i tried with this path too still it is not working – Prasanna Venkatesh Nov 07 '17 at 09:00

5 Answers5

3

You should always check the return value from open() and take appropriate action if it fails.

open MAIL, '/usr/sbin/sendmail -t' or die $!;

In this case, you'll get an error message like:

No such file or directory

I'm not sure where you're copying your code from, but I'm guessing you're making a transcription error. You're trying to open a file called /usr/sbin/sendmail -t and it's very likely that your system doesn't have a file called that (particularly not with the -t on the end).

What you actually want to do is to open a pipe to the command. You've just omitted a single character:

open MAIL, '|/usr/sbin/sendmail -t' or die $!;

Note the | at the start of the filename.

I'd also recommend using a lexical filehandle instead of a bareword.

open my $mail_fh, '|/usr/sbin/sendmail -t' or die $!;

But, most importantly, why are you trying to do this without using modules? Perl modules are a large part of the power of Perl. If you can't use modules (and install new modules from CPAN) then you are missing out on much of the ease of Perl programming. Why waste time rewriting code when there are tried and tested versions available for your use?

Whatever the bottlenecks are that are preventing you from using Perl modules, I urge you to spend time working to remove them. Your Perl programming career will be much more enjoyable.

The Perl FAQ recommends Email::MIME and Email::Sender::Simple. Personally, I'd go with Email::Stuffer in most cases. The Email::* namespace is where the good stuff is.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • i have tried this open MAIL, '|/usr/sbin/sendmail -t' or die $!; And it looks fine,sendmail path is correct. But I didn't receive any mail. Is there a way to check error log in server. I am unable to install any modules for now. I need a quick solution for this – Prasanna Venkatesh Nov 07 '17 at 03:35
  • Yes. You can check the mail log. But you would need to ask your local sysadmin where it is. Why do you think you can't install modules? That is rarely true. – Dave Cross Nov 07 '17 at 05:45
  • As the project is done about 8 to 10 years back i am just trying to make it work fine – Prasanna Venkatesh Nov 07 '17 at 06:07
  • @PrasannaVenkatesh: Are you adding the email as a new feature? Or are you fixing an existing feature that has stopped working? – Dave Cross Nov 07 '17 at 08:39
  • i am fixing the existing one – Prasanna Venkatesh Nov 07 '17 at 08:42
  • @PrasannaVenkatesh: Ok. So this used to work. When did it stop working? What changed on the server at the time it stopped working? Please update your question to add more details. – Dave Cross Nov 07 '17 at 08:43
  • after migration to another server – Prasanna Venkatesh Nov 07 '17 at 08:44
  • @PrasannaVenkatesh: So tell us about the differences between the two servers. New operating system? New version of the same operating system? **Please update your question to add more details.** – Dave Cross Nov 07 '17 at 08:46
  • 1
    If you're opening a lexical FH, I'd suggest going the whole hog and using 3 arg open. `open ( my $mailer, '|-', '/usr/sbin/sendmail -t' ) or die $!;` – Sobrique Nov 07 '17 at 16:02
2

How to send mail in Perl without using modules?

Don't.

The modules in CPAN are well tested and secure. And they take care of problems you don't even realize you have. There is no excuse for a professional to avoid them.

shawnhcorey
  • 3,545
  • 1
  • 15
  • 17
1

Try something like this:

open MAIL, "| mailx -s '$subject' -r $from $to";
print MAIL $mail_message;
close MAIL;
jira
  • 3,890
  • 3
  • 22
  • 32
0

This line is opening a file called "/usr/sbin/sendmail -t" for reading.

open(MAIL, "/usr/sbin/sendmail -t");

If you'd checked what open returned, you'd find it returned an error because that file doesn't exist. Instead, you want to pipe your output to sendmail. Here is how your code should look using the more modern way of calling open

if(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 "This is a test message from Cyberciti.biz! You can write your
mail body text here\n";

    close($mail);
    }
else
    {
    print "open failed: $!\n";
    }
Chris Turner
  • 8,082
  • 1
  • 14
  • 18
0

I finally did it by running php commands in perl script.

I wrote a mail function in php and passed params to that file from perl.

#!/usr/bin/perl

print "Content-type: text/html\n\n";

use CGI::Carp 'fatalsToBrowser';
my $mail = 'example@gmail.com';
$mail =~  s/@/%40/g;
my $sub = 'Subject';
$sub =~ s/\s/+/g;
my $msg = 'Message';
$msg =~ s/\s/+/g;
my $output = `php path-to-php-file 'to=$mail&message=$msg&sub=$sub'`;

This link helped me.

But I really felt bad for doing this way. This shouldn't be appreciated.

I thank everyone.