发送电子邮件到Perl给地址中有撇号的人

时间:2015-07-21 14:03:03

标签: perl shell email

我目前正在尝试将Perl中的电子邮件写入其中包含撇号并且无法弄清楚如何发送它的用户。这是该行的样子:

$to = "o'connell\@website.com";

以下是我用来发送电子邮件的行:

system("echo $message | mailx -s $subject $to ");

这是o'connell@website.com的示例电子邮件。由于撇号,它不会发送它。我尝试过以下内容并没有奏效。我正在使用mailx发送电子邮件

$to = "o'connell\@website.com";
$to = "o''connell\@website.com";
$to = "o"'"connell\@website.com";

这些都没有奏效。我可以发送电子邮件给那些在他们的电子邮件中没有它的人。有什么建议吗?

4 个答案:

答案 0 :(得分:5)

这不是一个Perl问题,这是一个shell问题。你必须正确地逃避价值观:

system("echo $message | mailx -s $subject \"$to\" ");

或更具可读性:

system qq(echo $message | mailx -s $subject "$to");

我也会逃避这个问题,你也应该非常小心$ message,如果它包含

; rm -rf /

请参阅Task::Kensho::Email,了解如何直接从Perl发送电子邮件。

答案 1 :(得分:3)

您可以直接执行perl执行mailx而不使用shell“。” [看看wdebeaum answer]

中更简单的实现
$message="message\n";
$to='$to = 'o\'connell\@website.com';
$subject= "x's " . time(); # string with apostrophe and time mark for testing
#p>#separator之后使stackoverflow语法着色工作

if( my $child_pid = open(my $TO_KID,   "|-")) {
   # I am the parent:
   print $TO_KID $message;
   close($TO_KID);
   waitpid $child_pid, 0;
} elsif(defined($child_pid)) {
   # I am the child; use STDIN/STDOUT normally
   exec 'mailx','-s',$subject,$to;
   die "couldn't exec mailx: $!";
}else{
   # $child_pid is undefined - fork (in open) failed
   die "can't fork: $!";
}

答案 2 :(得分:3)

和Andrzej一样,我说避免使用shell。但是,实现这一目标的方法更简单:

open(FH, "|-", "mailx", "-s", $subject, $to) or die "Can't mailx: $!";
print FH $message;
close(FH);

如果您使用" | - "打开命令及其参数。模式,openclose将为您处理execwaitpid

来自perldoc -f open

         The following blocks are more or less equivalent:

            open(FOO, "|tr '[a-z]' '[A-Z]'");
            open(FOO, "|-", "tr '[a-z]' '[A-Z]'");
            open(FOO, "|-") || exec 'tr', '[a-z]', '[A-Z]';
            open(FOO, "|-", "tr", '[a-z]', '[A-Z]');

            open(FOO, "cat -n '$file'|");
            open(FOO, "-|", "cat -n '$file'");
            open(FOO, "-|") || exec "cat", "-n", $file;
            open(FOO, "-|", "cat", "-n", $file);

        The last two examples in each block show the pipe as "list form",
        which is not yet supported on all platforms. A good rule of thumb
        is that if your platform has a real "fork()" (in other words, if
        your platform is Unix, including Linux and MacOS X), you can use
        the list form. You would want to use the list form of the pipe so
        you can pass literal arguments to the command without risk of the
        shell interpreting any shell metacharacters in them. However, this
        also bars you from opening pipes to commands that intentionally
        contain shell metacharacters, such as:

            open(FOO, "|cat -n | expand -4 | lpr")
                // die "Can't open pipeline to lpr: $!";

        See "Safe Pipe Opens" in perlipc for more examples of this.

...

        Closing any piped filehandle causes the parent process to wait for
        the child to finish, then returns the status value in $? and
        "${^CHILD_ERROR_NATIVE}".

答案 3 :(得分:2)

您正在构建shell命令。使用以下内容:

use String::ShellQuote qw( shell_quote );

my $cmd1 = shell_quote('echo', $message);
my $cmd2 = shell_quote('mailx', '-s', $subject, $to);
system("$cmd1 | $cmd2");

但是您可以通过使用以下内容来避免使用echo并涉及shell:

open(my $pipe, "|-", "mailx", "-s", $subject, $to)
   or die("Can't execute mailx: $!\n");
print($pipe $message);
close($pipe);

更好的方法是使用旨在帮助您发送电子邮件的模块。

相关问题