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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Stuck with email

 



sheet.pangasa
Novice

Nov 2, 2012, 1:23 AM

Post #1 of 5 (1579 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:


Code
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


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

func_sendmail("subject","to_address","from_address");


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!


Laurent_R
Veteran / Moderator

Nov 2, 2012, 3:42 AM

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

There are a number of mail modules on the CPAN:

http://search.cpan.org/search?query=mail&mode=all


sheet.pangasa
Novice

Nov 2, 2012, 3:56 AM

Post #3 of 5 (1573 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:


Code
#!/usr/bin/perl 
use warnings;
use diagnostics -verbose;
`echo hello | mail -s subject to@address.com -- -f from@address.com`



BillKSmith
Veteran

Nov 2, 2012, 8:34 AM

Post #4 of 5 (1564 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.

Code
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:

    Code
    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,
    Bill


    sheet.pangasa
    Novice

    Nov 3, 2012, 7:39 AM

    Post #5 of 5 (1551 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