CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Stuck with email



Nov 2, 2012, 1:23 AM

Post #1 of 5 (3795 views)
Stuck with email Can't Post

Hi Gurus,

I am new to perl and have a requirement to send email from perl script running on the linux environment. I need to define the 'from' address in the syntax as well. The below command when run on linux works fine:

mail -s subject to_address -- -f from_address

However when i try the same from Perl script using the below code it gives me the error:

Sendmail invalid option -- -f

sub func_sendmail(){ 
`mail -s "$_[0]" "$_[1]" . "-- -f" . "$_[2]" < $input`;


I have also tried the use Mail::Sendmail but even that does not work.

Could someone please let me know how to resolve this.

Thanks in advance!

Veteran / Moderator

Nov 2, 2012, 3:42 AM

Post #2 of 5 (3791 views)
Re: [sheet.pangasa] Stuck with email [In reply to] Can't Post

There are a number of mail modules on the CPAN:


Nov 2, 2012, 3:56 AM

Post #3 of 5 (3789 views)
Re: [Laurent_R] Stuck with email [In reply to] Can't Post

Thanks a lot for the reply! Will definitely go through it. However, i doubt that i have the function written in wrong way or i am not calling it properly because if i type the below statement (not function) in a perl script and execute it, it sends the email:

use warnings;
use diagnostics -verbose;
`echo hello | mail -s subject -- -f`


Nov 2, 2012, 8:34 AM

Post #4 of 5 (3780 views)
Re: [sheet.pangasa] Stuck with email [In reply to] Can't Post

Your main problem is improper quoting. I can demonstrate what you re passing to the shell by printing your command.

sub func_sendmail { 
$input = 'my_mail.txt';
print qq(mail -s "$_[0]" "$_[1]" . "-- -f" . "$_[2]" < $input);
# `mail -s "$_[0]" "$_[1]" . "-- -f" . "$_[2]" < $input`;
func_sendmail ( "subject", "to_address", "from_address" );

There are several other errors.
  • Always use strict and warnings.

  • Place subroutine definlition at end of program.

  • Prototypes do not mean what you think. They should seldom be used.

  • The function system (refer: perldoc -f system) is a better choice than backticks in this application.

  • Combining these ideas:

    use strict; 
    use warnings;
    func_sendmail ( "subject", "to_address", "from_address" );
    sub func_sendmail {
    my $input = 'my_mail.txt';
    my $cmd = qq(mail -s $_[0] $_[1] -- -f $_[2] < $input);
    print $cmd, "\n";
    my $status = system $cmd;
    print "Email status = $status\n";

    (untested - I do not have access to linix)
    Good Luck,


    Nov 3, 2012, 7:39 AM

    Post #5 of 5 (3767 views)
    Re: [BillKSmith] Stuck with email [In reply to] Can't Post

    Hey Bill,

    Thanks a lot, that worked perfectly and thanks for all the suggestions. I will try to implement those in my scripts.

    Thanks once again!


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives