LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Email from PERL/ bash (http://www.linuxquestions.org/questions/linux-newbie-8/email-from-perl-bash-856650/)

rkrasowski 01-16-2011 09:57 AM

Email from PERL/ bash
 
Hello,
I can not make this work:
I can send email from bash with this script:
#!/bin/bash
echo "Test message" | /usr/bin/mail -s "test2" "myEmail@yahoo.com"

When I am trying to run this in Perl using system function like this:
#!/usr/bin/perl
system("'Test message' | /usr/bin/mail -s 'test2' 'myEmail@yahoo.com');
I get message that program exited with code 0 , which is nice, but mail does not get to destination. Any idea what is wrong?? Can I do it in that way??
Thanks
Robert

ps Ubuntu 10 on the machine that I tried the script

bmarx 01-16-2011 11:52 AM

What happens when you try executing that command from the command line (as opposed to a perl script)? Try enabling debugging in the command from the perl script to see what happens (--debug-level).

If you can not get that to work, you could always use the perl smtp interface.
http://search.cpan.org/~gbarr/libnet-1.22/Net/SMTP.pm

Also, have you tried just using backticks as opposed to the system() command? e.g.

$result = `echo "Test message" | /usr/bin/mail -s "test2" "myEmail@yahoo.com"`;
print $result;

Tinkster 01-16-2011 01:50 PM

Quote:

Originally Posted by rkrasowski (Post 4226183)
Hello,
I can not make this work:
I can send email from bash with this script:
#!/bin/bash
echo "Test message" | /usr/bin/mail -s "test2" "myEmail@yahoo.com"

When I am trying to run this in Perl using system function like this:
#!/usr/bin/perl
system("'Test message' | /usr/bin/mail -s 'test2' 'myEmail@yahoo.com');
I get message that program exited with code 0 , which is nice, but mail does not get to destination. Any idea what is wrong?? Can I do it in that way??
Thanks
Robert

ps Ubuntu 10 on the machine that I tried the script

Check your logs ... /var/log, and grep for the recipient.

Code:

grep myEmail@yahoo.com /var/log/*



Cheers,
Tink

alizard 01-16-2011 05:09 PM

unless you really need to understand
 
how e-mail sending mechanisms in Linux work, you can install the SendEmail script from:
http://caspian.dotconf.net/menu/Software/SendEmail/

or install it from the distro repositories:

from the command line:
$ sudo aptitude install sendemail

fill in the name / message (from file is OK) / SMTP server (TLS is OK)... and you're up and running.

Detailed instructions can be found at the URL or via the man(ual) file.

$ man sendemail

Good luck. Versions are also available for Winblows (98-XP, probably untested on Vista/W7) and OSX. A careful examination of the Linux script might tell you quite a lot about how e-mail sending works.

rkrasowski 01-16-2011 08:29 PM

well,
Thanks for comments and help,
I ended up using Net::SMTP and it works great.
Thanks again
Robert

ps to BMARX - I tried with ` but it did,t work

scook 01-17-2011 07:03 AM

Is the command you are using system with the same that works from the command line? I don't see the 'echo' in the text you show sent to system.

zjoske 01-17-2011 09:05 AM

Hi

Your Perl statement is missing a quote (") at the end, before the semicolumn...
And ofcourse the echo statement is missing (as mentioned above)

Maybe that helps



system("echo 'Test message' | /usr/bin/mail -s 'test2' 'myEmail@yahoo.com'");

Quote:

Originally Posted by rkrasowski (Post 4226183)
Hello,
I can not make this work:
I can send email from bash with this script:
#!/bin/bash
echo "Test message" | /usr/bin/mail -s "test2" "myEmail@yahoo.com"

When I am trying to run this in Perl using system function like this:
#!/usr/bin/perl
system("'Test message' | /usr/bin/mail -s 'test2' 'myEmail@yahoo.com');
I get message that program exited with code 0 , which is nice, but mail does not get to destination. Any idea what is wrong?? Can I do it in that way??
Thanks
Robert

ps Ubuntu 10 on the machine that I tried the script



All times are GMT -5. The time now is 07:13 PM.