简体   繁体   English

如何将多行输出从Perl发送到/ bin / mail?

[英]How do I send multi-line output from Perl to /bin/mail?

I have a Perl script that prints multiple lines of output to screen. 我有一个Perl脚本,可将多行输出打印到屏幕上。 I need to capture those lines and either pipe them into a single piece of email ( /bin/mail ) or into a text file that I can send by /bin mail in another operation. 我需要捕获这些行,然后将它们通过管道发送到一封电子邮件中(/ bin / mail)或一个文本文件中,该文件可以通过/ bin邮件在另一个操作中发送。 Actually, I have already figured out the easy (dumb) way whereby I use a bash wrapper to do the mailing bit. 实际上,我已经想出了一种简单的方法,可以使用bash包装器来完成邮件发送。 Looks like, 看起来像,

#!/usr/bin/bash
source /nethome/zog/.bash_profile 
cd /nethome/zog/bin/perl  
/nethome/zog/bin/perl/find_free_space.pl > text.file
/bin/mail -s FreeSpace@NJ3 zog@geemail.com < text.file

I want to be using Net::SMTP to do the smtp bit. 我想使用Net :: SMTP做smtp位。 The above is inelegant, to say the least. 至少可以说,以上内容并不雅致。

I saw something like this: 我看到了这样的东西:

open(MAIL, "| /bin/mail -s FreePorts me\@geemail.com") || 
   die "mail failed: $!\n"; print MAIL "This is how it goes."

on stackoverflow but failed to redirect the STDOUT into mail. 在stackoverflow上,但是无法将STDOUT重定向到邮件。 I'm using: 我正在使用:

$complete_output .= "\n"; "|/bin/mail -s FreePorts zog\@geeemail.com" || die "mail failed: $!\n";

I'm not sure if you all need to see the Perl script in order to help, but it is on pastebin . 我不确定是否所有人都需要查看Perl脚本才能提供帮助,但是它位于pastebin上 Please let me know. 请告诉我。

STDOUT isn't in play here. STDOUT不在这里进行。 Is there something you forgot to tell us? 您有事忘了告诉我们吗? What happened when you printed to the MAIL filehandle? 当您打印到MAIL文件句柄时会发生什么? Did that output make it into the message? 该输出是否使其成为消息? What happens when you send multiple lines to the MAIL filehandle? 当您向MAIL文件句柄发送多行时会发生什么?

Your code is: 您的代码是:

"|/bin/mail -s FreePorts zog\@geeemail.com" || die "mail failed: $!\n";

That does nothing. 那什么也没做。 It's a string, which is also a true value. 这是一个字符串,也是一个真实值。 The alternation never does to the die branch. 交替永远不会对模子分支造成影响。 What happened when you used the first bit of code in your question? 当您使用问题的第一部分代码时会发生什么? Write a small example script to see if you can send mail and test with that until you figure it out. 编写一个小的示例脚本,看看是否可以发送邮件并对其进行测试,直到弄清楚为止。 For example, this is a complete script that tests your problem: 例如,这是一个测试您的问题的完整脚本:

#!perl
use strict;
use warnings;
open my $mail, '| /bin/mail -s FreePorts me@geemail.com';
print $mail "This is a test message from $$ at " . localtime() . "\n";
close $mail or die "The pipe failed!\n $?";

What happens when you run that? 运行该怎么办? What happens when you try that same command from the command line? 当您从命令行尝试相同命令时会发生什么?

Why not use a Perl module (like one of the Email::* modules) instead of relying on an external command? 为什么不使用Perl模块(例如Email :: *模块之一)而不是依靠外部命令? There's a lot to know about interprocess communication, and I think you're behind the curve on that. 关于进程间通信,有很多要了解的东西,我认为您落后于此。 A module takes care of all of the details for you, has a nicer interface, and is more flexible. 一个模块可以为您处理所有细节,具有更好的界面,并且更加灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM