Frage

Original problem: How to pass arguments in a string from an R script to a Perl script via the system() command where one flag may have 0 or more arguments.

For example: "Email.perl -t " p1@email.com; p2@email.com;" -c " p3@email.com; p4@email.com;" -s "Subject Line" -b "Body text" -f C:/file.txt"

I figured it out. Toolic was on the right path, but I had to pass ; after each name to get it to work. Below is the working code for anyone who's interested.

R function:

SendPerlMail <- function(email.to, email.cc=NA, email.sub=NA, email.body=NA, email.file=NA) {

  command <- 'perl ..\\..\\Email.perl -t "'
  for(to.p in email.to) {command <- paste0(command, paste0(' ', to.p, ';'))}

  command <- paste0(command, '" -c "')
  for(cc.p in email.cc) {command <- paste0(command, paste0(' ', cc.p, ';'))}

  command <- paste0(command, paste0('" -s ', email.sub))
  command <- paste0(command, paste0(' -b ', email.body))
  command <- paste0(command, paste0(' -f ', email.file))

  return(system(command, intern=T, invisible=F))
}

Perl script:

#!/usr/bin/perl -w

use Mail::Outlook;
use Getopt::Std;

my %opts;
getopts('t:c:s:b:f:', \%opts);

my $outlook = new Mail::Outlook();
my $msg = $outlook->create();

if(!$opts{t}) {die} else {$msg->To($opts{t});}
if($opts{c} ne 'NA') {$msg->Cc($opts{c});}
if($opts{s} ne 'NA') {$msg->Subject($opts{s});}
if($opts{b} ne 'NA') {$msg->Body($opts{b});}
if($opts{f} ne 'NA') {$msg->Attach(Type => 'text/plain', Path => "/", Filename => $opts{f});}

$msg->send;
War es hilfreich?

Lösung

Have to pass ; after each email address.

command <- 'perl ..\\..\\Email.perl -t "'
  for(to.p in email.to) {command <- paste0(command, paste0(' ', to.p, ';'))}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top