Web mail script

Discussion in 'Perl' started by trend5, Apr 26, 2005.

  1. trend5

    trend5 Guest

    I have a simple mail script designed to use from command line.
    I want create a simple web interface to use this script as remotely
    hosted webscript, from webhost.
    The script contains two files- Data File.dat(which holds the list of
    email addresses, where the mail has to be sent;
    the addresses are separated by new lines); and Message template
    File.template(this file contains the message template to be sent.)
    Need some help and advices.
    thanks
    ------------------------

    #!/usr/bin/perl -w

    #user configuration begin#

    $mailprg = "/usr/sbin/sendmail"; #path to your mail program
    $delay = 0.25; #delay in seconds
    $from = 'Coname<>'; #from address in your mail
    $subject = 'Trial send'; #subject of newsletter. u can add the spl
    code $USERNAME$
    $maildata = "mail.dat"; #mail address file
    $msgtemplate = "message.template"; #message template file

    #user configuration end#

    if(@ARGV){
    $i = 0;
    while($ARGV[$i]){
    if($ARGV[$i] eq "--help"){
    print "Command Line Options:\n\n";
    print "-t\tTest how the message will be formatted\n";
    print "-d\tData file listing the email addresses\n";
    print "-m\tMessage template file\n";
    print "-s\tSubject\n";
    print "-f\tFrom address. Quote for security reasons\n";
    die "\nMore information at indiWiz.com\n";
    }
    elsif($ARGV[$i]=~/^-/){
    $arr = substr($ARGV[$i],1,1);
    if($arr eq "d"){
    $maildata = $ARGV[$i+1];
    $i+=2;
    }
    elsif($arr eq "m"){
    $msgtemplate = $ARGV[$i+1];
    $i+=2;
    }
    elsif($arr eq "t"){
    $testflag = 1;
    $i++;
    }
    elsif($arr eq "s"){
    $subject = $ARGV[$i+1];
    $i+=2;
    }
    elsif($arr eq "f"){
    $from = $ARGV[$i+1];
    $i+=2;
    }
    else{
    die "Wrong argument!";
    }
    }
    }
    }

    open TEMPLATE, $msgtemplate || die "Cannot open template file!\n";
    while(<TEMPLATE>){
    $msg .= $_;
    }
    close TEMPLATE || die "Cannot close template file!\n";

    open MAILFILE,"$maildata" || die "Cannot open data file!\n";
    while(<MAILFILE>){
    chomp($_);
    @arr = split(/\@/,$_);
    $arr[0] = ucfirst($arr[0]);
    $tmp = $msg;
    $tmp =~ s/\$USERNAME\$/$arr[0]/g;
    $subject =~ s/\$USERNAME\$/$arr[0]/g;
    $temp = "To: $arr[0]<$_>\n";
    $temp .= "From: $from\n";
    $temp .= "Subject: $subject\n\n\n";
    $temp .= $tmp;
    if($testflag){
    print $temp;
    last;
    }
    else{
    open MAILPRG,"|$mailprg -t" || die "Cannot open mail program!\n";
    print MAILPRG $temp;
    close MAILPRG || die "Cannot close mailprogram!\n";
    if($delay < 0){
    $delay = 1;
    sleep $delay;
    }
    elsif($delay < 1){
    select(undef,undef,undef,$delay);
    }
    else{
    sleep $delay;
    }
    print $_,"\n";
    }
    }
    close MAILFILE || die "Cannot close data file!\n";
     
    trend5, Apr 26, 2005
    #1
    1. Advertising

  2. trend5

    Joe Smith Guest

    trend5 wrote:

    > Need some help and advices.


    What advice are you asking for? Your post did not show
    1) what the program generated when you ran it
    2) what you expected it to do.

    > if(@ARGV){
    > $i = 0;
    > while($ARGV[$i]){
    > if($ARGV[$i] eq "--help"){ print;print;print }
    > elsif($ARGV[$i]=~/^-/){
    > $arr = substr($ARGV[$i],1,1);
    > if ($arr eq "d"){$maildata = $ARGV[$i+1]; $i+=2;}
    > elsif($arr eq "m"){$msgtemplate = $ARGV[$i+1]; $i+=2;}
    > elsif($arr eq "t"){$testflag = 1; $i++;}
    > elsif($arr eq "s"){$subject = $ARGV[$i+1]; $i+=2;}
    > elsif($arr eq "f"){$from = $ARGV[$i+1]; $i+=2;}
    > else{ die "Wrong argument!"; }
    > }
    > }
    > }


    That's a mighty ugly bunch of C code there.
    When creating Perl programs, you should program in Perl, not C.

    my $Usage = <<EOM;
    Command Line Options:
    -t Test how the message will be formatted
    -d Data file listing the email addresses
    -m Message template file
    -s Subject
    -f From address. Quote for security reasons

    More information at indiWiz.com
    EOM
    if (@ARGV and $ARGV[0] =~ /^-/) {
    $_ = shift;
    die $Usage if $_ eq '--help';
    my $opt = substr $_,1,1;
    if ($opt eq "d") { $maildata = shift; }
    elsif ($opt eq "m") { $msgtemplate = shift; }
    elsif ($opt eq "t") { $testflag = 1; }
    elsif ($opt eq "s") { $subject = shift; }
    elsif ($opt eq "f") { $from = shift; }
    else { die "Invalid option '$_'\n",$Usage; }
    }
    die "Unexpected command line arguments '@ARGV'\n",$Usage if @ARGV;

    > open TEMPLATE, $msgtemplate || die "Cannot open template file!\n";


    open TEMPLATE, $msgtemplate or
    die "Cannot open template file $msgtemplate - $!\n";

    > $subject =~ s/\$USERNAME\$/$arr[0]/g;


    That will work only once. You need to keep the original around.

    (my $newsubject = $subject) =~ s/\$USERNAME\$/$arr[0]/g;

    If you have any more questions, specific ones regarding perl, then
    post them to the comp.lang.perl.misc newsgroup.
    (comp.lang.perl is defunct; superceded by comp.lang.perl.misc)

    -Joe
     
    Joe Smith, Apr 28, 2005
    #2
    1. Advertising

  3. trend5

    PC Guest

    "Joe Smith" <> wrote in message
    news:...
    >
    > (comp.lang.perl is defunct; superceded by comp.lang.perl.misc)
    >


    Why?
     
    PC, Apr 28, 2005
    #3
  4. trend5

    Joe Smith Guest

    PC wrote:
    > "Joe Smith" <> wrote in message
    > news:...
    >
    >>(comp.lang.perl is defunct; superceded by comp.lang.perl.misc)

    >
    > Why?


    The original comp.lang.perl was split into several new newsgroups.
    This was voted on many many years ago.
    There is a lot more traffic in comp.lang.perl.misc than comp.lang.perl .
    -Joe
     
    Joe Smith, Apr 28, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. me
    Replies:
    0
    Views:
    456
  2. =?Utf-8?B?TXJGZXo=?=

    Mail message size problem (System.Web.Mail)

    =?Utf-8?B?TXJGZXo=?=, Mar 14, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    641
    =?Utf-8?B?TXJGZXo=?=
    Mar 14, 2005
  3. =?Utf-8?B?TWlja2VCb3k=?=

    Problem with System.Web.Mail.MailMessage and HTML mail

    =?Utf-8?B?TWlja2VCb3k=?=, Jun 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    912
    =?Utf-8?B?TWlja2VCb3k=?=
    Jun 21, 2005
  4. =?Utf-8?B?Unlhbg==?=
    Replies:
    2
    Views:
    3,903
    Steven Cheng[MSFT]
    Feb 28, 2006
  5. Brad
    Replies:
    2
    Views:
    10,203
Loading...

Share This Page