sendmail to more than 1 email address - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

sendmail to more than 1 email address

james9james9 Posts: 3Member
1 email address is no problem, more than one is not working for me. I'm a flash developer so not familiar with perl syntax, can anyone assist?

sorry in advance for the swathe of code
#!/usr/bin/perl
$recipient = "[email protected]";
$mailprog = "/usr/sbin/sendmail -t";
@ordered = ('daterequester','namerequester','adresrequester','placerequester','namecontact', 'telephonerequester','faxrequester','emailrequester','pickupadres','pickupplace','pickuppostcode','pickuppostdate','pickupposttime','nameclient','languageclient','telephoneclient','airport','carrier','checkintime','flightnr','flightnr');

################################################################################
## SCRIPT CODE STARTS HERE ##
################################################################################

# GET "POST"ED DATA
&GetData();

# CHECK DATA FOR VALIDITY
&CheckData();

# SEND EMAIL TO RECIPIENT
&SendEmail();

# SEND RESPONSE EMAIL IF REQUIRED
if ($SendResponse)
{
&SendResponseEmail();
}

# RESPOND TO FLASH MOVIE
&Success();

# ALL DONE
exit;


################################################################################
## SUBROUTINES ##
################################################################################

#------------------------------[ SUBROUTINE HEADER ]---------------------------#
# NAME: GetData #
# DATA: [NONE] #
# DESC: Used to get GETed and POSTed data. Also performs decoding (unURL) of #
# data before storing it in a hash table for later use. #
#------------------------------------------------------------------------------#
sub GetData
{
# DETERMINE REQUEST METHOD
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
# GET "GET"ED DATA FROM QUERY STRING
$buffer = $ENV{'QUERY_STRING'};
}
else
{
# GET "POST"ED DATA FROM STDIN STREAM
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}

# SPLIT DATA INTO NAME/VALUE PAIRS
@pairs = split(/&/, $buffer);

# DECODE (UNURL) EACH NAME/VALUE PAIR INTO HASH TABLE
foreach $pair (@pairs)
{
# SPLIT PAIR INTO SEPERATE NAME/VALUE
($name, $value) = split(/=/, $pair);

# DECODE (UNURL) VALUE AND NAME
$value =~ tr/+/ /;
$value =~ s/%0D/
/sg;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/seg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

# STORE DATA IN HASH TABLE
$FORM{$name} = $value;
}
}


#------------------------------[ SUBROUTINE HEADER ]---------------------------#
# NAME: CheckData #
# DATA: [NONE] #
# DESC: Checks the validity of data such as email address (syntactical) and #
# that all required fields have been filled out. #
#------------------------------------------------------------------------------#
sub CheckData
{
# CHECK THE REQUIRED FIELD HAVE BEEN FILLED OUT
foreach $checkfield(@required)
{
unless ($FORM{$checkfield})
{
&Fail("You have omitted the $checkfield field!");
}
}

# REMOVE INGORE FIELDS FROM FORM HASH TABLE
foreach $ignorefield(@ignore)
{
if ($FORM{$ignorefield})
{
$FORM{$ignorefield} = "";
}
}
}


#------------------------------[ SUBROUTINE HEADER ]---------------------------#
# NAME: SendEmail #
# DATA: [NONE] #
# DESC: Sends email to designated address. Body consists of referring page URL #
# name/value pairs and, optionally, user details such as IP address. #
#------------------------------------------------------------------------------#
sub SendEmail
{
$FromAddress = $ENV{'SERVER_NAME'};
$FromAddress =~ s/^www.//;
$FromAddress = "client request maximum security ";

if ($recipient eq "")
{
if ($FORM{'recipient'})
{
$recipient = $FORM{'recipient'};
}
else
{
Fail("Variable 'recipient' expected but not sent from Flash.");
}
}

open (MAIL,"|$mailprog") or Fail("Couldn't find sendmail program. Please contact the webmaster");
print MAIL "To: $recipient
";
print MAIL "From: " . $FromAddress . "
";
print MAIL "Subject: request of service:Max Limo Formulier
";
print MAIL "Martin, iemand heeft jou gemailed:

";

foreach $orderedfield (@ordered)
{
if ($FORM{$orderedfield})
{
print MAIL "$orderedfield: $FORM{$orderedfield}
";
$FORM{$orderedfield} = "";
}
}

foreach $key (keys %FORM)
{
if ($FORM{$key})
{
print MAIL "$key: $FORM{$key}
";
}
}

if ($adduserdata)
{
print MAIL "
";
print MAIL "User Information
";
print MAIL "================

";
print MAIL "Host Address: $ENV{'REMOTE_ADDR'}
";
print MAIL "User Agent: $ENV{'HTTP_USER_AGENT'}
";
print MAIL "Referring Page: $ENV{'HTTP_REFERER'}

";
}


close MAIL;
}


#------------------------------[ SUBROUTINE HEADER ]---------------------------#
# NAME: SendRespionseEmail #
# DATA: [NONE] #
# DESC: Sends a coutresy email to the user who filled out the form. Email is #
# taken from a formatted text file. #
#------------------------------------------------------------------------------#
sub SendResponseEmail
{
# Attempt to open response file
if (open(RESPFILE, $ResponseFile))
{
# Read file into array and close
@ResponseText = RESPFILE>;
close(RESPFILE);

# Chomp newlines from first 3 lines
chomp($ResponseText[0]);
chomp($ResponseText[1]);
chomp($ResponseText[2]);

# Formulate to, from and subject fields from relevant data
$ToAddress = $FORM{'name'} . " <" . $FORM{'email'} . ">";
$FromAddress = $ResponseText[0] . " <" . $ResponseText[1] . ">";
$Subject = $ResponseText[2];

# Initialise line count and email body
$Count = 3;
$Body = "";

# For ther remaining lines in the array
while($Count <= $#ResponseText)
{
# Fetch line
$Line = $ResponseText[$Count];

# If line is not a comment
if (substr($Line, 0, 2) ne "//")
{
# Translate incline variables for values
$Line =~ s/%NAME%/$FORM{'name'}/g;
$Line =~ s/%EMAIL%/$FORM{'email'}/g;
$Line =~ s/%RECIPIENT%/$recipient/g;

# Add line to email body
$Body = $Body . $Line;
}

# Next line
$Count++;
}

# Attempt to open mailprog
if (open(MAIL, "|$mailprog"))
{
# Send email and close
print MAIL "To: " . $ToAddress . "
";
print MAIL "From: " . $FromAddress . "
";
print MAIL "Subject: " . $Subject . "

";
print MAIL $Body . "

";
print MAIL "datum: " .$daterequester . "

";
print MAIL "naam: " .$namerequester . "

";
print MAIL "adres: " .$adresrequester . "

";
print MAIL "plaats: " .$placerequester . "

";
print MAIL "naam contact persoon/opdrachtgever: " .$namecontact . "

";
print MAIL "telefoon nummer: " .$telephonerequester . "

";
print MAIL "fax: " .$faxrequester . "

";
print MAIL "email: " .$emailrequester . "

";
print MAIL "straat: " .$pickupadres . "

";
print MAIL "plaats: " .$pickupplace . "

";
print MAIL "postcode: " .$pickuppostcode . "

";
print MAIL "date: " .$pickuppostdate . "

";
print MAIL "tijd:" .$pickupposttime . "

";
print MAIL "naam klant: " .$nameclient . "

";
print MAIL "spreektaal: " .$languageclient . "

";
print MAIL "telefoon nummer: " .$telephoneclient . "

";
print MAIL "naam luchthaven: " .$airport . "

";
print MAIL "maatschappij: " .$carrier . "

";
print MAIL "inchecktijd: " .$checkintime . "

";
print MAIL "vlucht nr: " .$flightnr . "

";
print MAIL "datum: " .$flightdate . "

";
print MAIL "straat: " .$deststreet . "

";
print MAIL "plaats: " .$destplace . "

";
print MAIL "naam locatie: " .$destlocation . "

";
print MAIL "postcode: " .$destpostcode . "

";
print MAIL "land: " .$destcountry. "

";
print MAIL "Gewenste aankomstdatum bestemming: " .$destarrivaldate . "

";
print MAIL "Gewenste aankomsttijd bestemming: " .$destarrivaltime . "

";
print MAIL "Andere bestemming nl: " .$tel . "

";
print MAIL "tijd:" .$end . "

";
print MAIL "gewenste krant: ".$krant . "

";
print MAIL "persoonsbeveiliging: " .$persoonsbeveiliging . "

";
print MAIL "Hotelboeking: " .$hotelbooking . "

";
print MAIL "Welkomst attentie: " .$welcome . "

";
print MAIL "Muziek keuze: " .$muziek . "

";
print MAIL "Oerige wensen: " .$overige . "

";
close(MAIL);
}
}
}

#------------------------------[ SUBROUTINE HEADER ]---------------------------#
# NAME: Success #
# DATA: [NONE] #
# DESC: Sends the appropriate success message back to Flash. #
#------------------------------------------------------------------------------#
sub Success
{
print "Content-type: text/plain

";
print "&result=okay";
print "&junk=1";
}

#------------------------------[ SUBROUTINE HEADER ]---------------------------#
# NAME: Fail #
# DATA: $errormsg: Error message to send to Flash movie. #
# DESC: Sends the fail message along with an error message back to the Flash #
# movie. Performs URL encoding of errormessage before sending! #
#------------------------------------------------------------------------------#
sub Fail
{
my $errormsg = pop(@_);

# URL ENCODE ERROR MESSAGE STRING
print "Content-type: text/plain

";
print "&result=fail";
print "&errormessage=$errormsg";
print "&junk=1";
exit;
}

Comments

  • WeirdofreakWeirdofreak Posts: 439Member
    Logically, if it works for one but not several, just call it several times, each with a single address. Other than that, posting saying what the problem actually is would help - it'll be in your log files.
  • james9james9 Posts: 3Member
    did you read my question?
    the script works fine for sending to one person.i need it to send to many and i don't know how to write perl.

    I think i need to create an array to send the mail to more than 1 person. Does anyone know how to append this file to send this to more than one person?
    Thankx

    j
  • JonathanJonathan Posts: 2,914Member
    : 1 email address is no problem, more than one is not working for me.
    : I'm a flash developer so not familiar with perl syntax, can anyone
    : assist?
    [blue]Look out for the stuff in blue...[/blue]

    : sorry in advance for the swathe of code
    : #!/usr/bin/perl
    : $recipient = "[email protected]";
    [blue]Unless I'm mistaken, can you not just put:-
    $recipient = "[email protected], [email protected]";[/blue]

    : $mailprog = "/usr/sbin/sendmail -t";
    : @ordered = ('daterequester','namerequester','adresrequester','placerequester','namecontact', 'telephonerequester','faxrequester','emailrequester','pickupadres','pickupplace','pickuppostcode','pickuppostdate','pickupposttime','nameclient','languageclient','telephoneclient','airport','carrier','checkintime','flightnr','flightnr');
    :
    : ################################################################################
    : ## SCRIPT CODE STARTS HERE ##
    : ################################################################################
    :
    : # GET "POST"ED DATA
    : &GetData();
    :
    : # CHECK DATA FOR VALIDITY
    : &CheckData();
    :
    : # SEND EMAIL TO RECIPIENT
    : &SendEmail();
    :
    : # SEND RESPONSE EMAIL IF REQUIRED
    : if ($SendResponse)
    : {
    : &SendResponseEmail();
    : }
    :
    : # RESPOND TO FLASH MOVIE
    : &Success();
    :
    : # ALL DONE
    : exit;
    :
    :
    : ################################################################################
    : ## SUBROUTINES ##
    : ################################################################################
    :
    : #------------------------------[ SUBROUTINE HEADER ]---------------------------#
    : # NAME: GetData #
    : # DATA: [NONE] #
    : # DESC: Used to get GETed and POSTed data. Also performs decoding (unURL) of #
    : # data before storing it in a hash table for later use. #
    : #------------------------------------------------------------------------------#
    : sub GetData
    : {
    : # DETERMINE REQUEST METHOD
    : if ($ENV{'REQUEST_METHOD'} eq "GET")
    : {
    : # GET "GET"ED DATA FROM QUERY STRING
    : $buffer = $ENV{'QUERY_STRING'};
    : }
    : else
    : {
    : # GET "POST"ED DATA FROM STDIN STREAM
    : read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    : }
    :
    : # SPLIT DATA INTO NAME/VALUE PAIRS
    : @pairs = split(/&/, $buffer);
    :
    : # DECODE (UNURL) EACH NAME/VALUE PAIR INTO HASH TABLE
    : foreach $pair (@pairs)
    : {
    : # SPLIT PAIR INTO SEPERATE NAME/VALUE
    : ($name, $value) = split(/=/, $pair);
    :
    : # DECODE (UNURL) VALUE AND NAME
    : $value =~ tr/+/ /;
    : $value =~ s/%0D/
    /sg;
    : $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/seg;
    : $name =~ tr/+/ /;
    : $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    :
    : # STORE DATA IN HASH TABLE
    : $FORM{$name} = $value;
    : }
    : }
    :
    :
    : #------------------------------[ SUBROUTINE HEADER ]---------------------------#
    : # NAME: CheckData #
    : # DATA: [NONE] #
    : # DESC: Checks the validity of data such as email address (syntactical) and #
    : # that all required fields have been filled out. #
    : #------------------------------------------------------------------------------#
    : sub CheckData
    : {
    : # CHECK THE REQUIRED FIELD HAVE BEEN FILLED OUT
    : foreach $checkfield(@required)
    : {
    : unless ($FORM{$checkfield})
    : {
    : &Fail("You have omitted the $checkfield field!");
    : }
    : }
    :
    : # REMOVE INGORE FIELDS FROM FORM HASH TABLE
    : foreach $ignorefield(@ignore)
    : {
    : if ($FORM{$ignorefield})
    : {
    : $FORM{$ignorefield} = "";
    : }
    : }
    : }
    :
    :
    : #------------------------------[ SUBROUTINE HEADER ]---------------------------#
    : # NAME: SendEmail #
    : # DATA: [NONE] #
    : # DESC: Sends email to designated address. Body consists of referring page URL #
    : # name/value pairs and, optionally, user details such as IP address. #
    : #------------------------------------------------------------------------------#
    : sub SendEmail
    : {
    : $FromAddress = $ENV{'SERVER_NAME'};
    : $FromAddress =~ s/^www.//;
    : $FromAddress = "client request maximum security ";
    :

    [blue]From here....[/blue]
    : if ($recipient eq "")
    : {
    : if ($FORM{'recipient'})
    : {
    : $recipient = $FORM{'recipient'};
    : }
    : else
    : {
    : Fail("Variable 'recipient' expected but not sent from Flash.");
    : }
    : }
    [blue]To here should probably be deleted for security reasons - we really don't ever want a script to take a recipient directly from the web. No, it won't as long as you specify it at the top of the script but better safe than sorry.[/blue]

    :
    : open (MAIL,"|$mailprog") or Fail("Couldn't find sendmail program. Please contact the webmaster");
    : print MAIL "To: $recipient
    ";
    [blue]Looking at this line, my suggestion about changing $recipient appears to be the right thing to do.[/blue]

    : print MAIL "From: " . $FromAddress . "
    ";
    : print MAIL "Subject: request of service:Max Limo Formulier
    ";
    : print MAIL "Martin, iemand heeft jou gemailed:

    ";
    :
    : foreach $orderedfield (@ordered)
    : {
    : if ($FORM{$orderedfield})
    : {
    : print MAIL "$orderedfield: $FORM{$orderedfield}
    ";
    : $FORM{$orderedfield} = "";
    : }
    : }
    :
    : foreach $key (keys %FORM)
    : {
    : if ($FORM{$key})
    : {
    : print MAIL "$key: $FORM{$key}
    ";
    : }
    : }
    :
    : if ($adduserdata)
    : {
    : print MAIL "
    ";
    : print MAIL "User Information
    ";
    : print MAIL "================

    ";
    : print MAIL "Host Address: $ENV{'REMOTE_ADDR'}
    ";
    : print MAIL "User Agent: $ENV{'HTTP_USER_AGENT'}
    ";
    : print MAIL "Referring Page: $ENV{'HTTP_REFERER'}

    ";
    : }
    :
    :
    : close MAIL;
    : }
    :
    :
    : #------------------------------[ SUBROUTINE HEADER ]---------------------------#
    : # NAME: SendRespionseEmail #
    : # DATA: [NONE] #
    : # DESC: Sends a coutresy email to the user who filled out the form. Email is #
    : # taken from a formatted text file. #
    : #------------------------------------------------------------------------------#
    : sub SendResponseEmail
    : {
    : # Attempt to open response file
    : if (open(RESPFILE, $ResponseFile))
    : {
    : # Read file into array and close
    : @ResponseText = RESPFILE>;
    [blue]I think there is a mistake on this line - it should be:-
    @ResponseText = ;[/blue]

    : close(RESPFILE);
    :
    : # Chomp newlines from first 3 lines
    : chomp($ResponseText[0]);
    : chomp($ResponseText[1]);
    : chomp($ResponseText[2]);
    :
    : # Formulate to, from and subject fields from relevant data
    : $ToAddress = $FORM{'name'} . " <" . $FORM{'email'} . ">";
    [blue]Insecure. This script can be hijacked by spammers - it allows them to enter many email addresses either in the name or email fields. Add the following before this line:-
    [code]if ($FORM{'name'} !~ /^[ws'-]*$/
    || $FORM{'email'} !~ /^[^@]+@[^@]+$/) {
    &Fail;
    }[/code]
    [/blue]

    : $FromAddress = $ResponseText[0] . " <" . $ResponseText[1] . ">";
    : $Subject = $ResponseText[2];
    :
    : # Initialise line count and email body
    : $Count = 3;
    : $Body = "";
    :
    : # For ther remaining lines in the array
    : while($Count <= $#ResponseText)
    : {
    : # Fetch line
    : $Line = $ResponseText[$Count];
    :
    : # If line is not a comment
    : if (substr($Line, 0, 2) ne "//")
    : {
    : # Translate incline variables for values
    : $Line =~ s/%NAME%/$FORM{'name'}/g;
    : $Line =~ s/%EMAIL%/$FORM{'email'}/g;
    : $Line =~ s/%RECIPIENT%/$recipient/g;
    :
    : # Add line to email body
    : $Body = $Body . $Line;
    : }
    :
    : # Next line
    : $Count++;
    : }
    :
    : # Attempt to open mailprog
    : if (open(MAIL, "|$mailprog"))
    : {
    : # Send email and close
    : print MAIL "To: " . $ToAddress . "
    ";
    : print MAIL "From: " . $FromAddress . "
    ";
    : print MAIL "Subject: " . $Subject . "

    ";
    : print MAIL $Body . "

    ";
    : print MAIL "datum: " .$daterequester . "

    ";
    : print MAIL "naam: " .$namerequester . "

    ";
    : print MAIL "adres: " .$adresrequester . "

    ";
    : print MAIL "plaats: " .$placerequester . "

    ";
    : print MAIL "naam contact persoon/opdrachtgever: " .$namecontact . "

    ";
    : print MAIL "telefoon nummer: " .$telephonerequester . "

    ";
    : print MAIL "fax: " .$faxrequester . "

    ";
    : print MAIL "email: " .$emailrequester . "

    ";
    : print MAIL "straat: " .$pickupadres . "

    ";
    : print MAIL "plaats: " .$pickupplace . "

    ";
    : print MAIL "postcode: " .$pickuppostcode . "

    ";
    : print MAIL "date: " .$pickuppostdate . "

    ";
    : print MAIL "tijd:" .$pickupposttime . "

    ";
    : print MAIL "naam klant: " .$nameclient . "

    ";
    : print MAIL "spreektaal: " .$languageclient . "

    ";
    : print MAIL "telefoon nummer: " .$telephoneclient . "

    ";
    : print MAIL "naam luchthaven: " .$airport . "

    ";
    : print MAIL "maatschappij: " .$carrier . "

    ";
    : print MAIL "inchecktijd: " .$checkintime . "

    ";
    : print MAIL "vlucht nr: " .$flightnr . "

    ";
    : print MAIL "datum: " .$flightdate . "

    ";
    : print MAIL "straat: " .$deststreet . "

    ";
    : print MAIL "plaats: " .$destplace . "

    ";
    : print MAIL "naam locatie: " .$destlocation . "

    ";
    : print MAIL "postcode: " .$destpostcode . "

    ";
    : print MAIL "land: " .$destcountry. "

    ";
    : print MAIL "Gewenste aankomstdatum bestemming: " .$destarrivaldate . "

    ";
    : print MAIL "Gewenste aankomsttijd bestemming: " .$destarrivaltime . "

    ";
    : print MAIL "Andere bestemming nl: " .$tel . "

    ";
    : print MAIL "tijd:" .$end . "

    ";
    : print MAIL "gewenste krant: ".$krant . "

    ";
    : print MAIL "persoonsbeveiliging: " .$persoonsbeveiliging . "

    ";
    : print MAIL "Hotelboeking: " .$hotelbooking . "

    ";
    : print MAIL "Welkomst attentie: " .$welcome . "

    ";
    : print MAIL "Muziek keuze: " .$muziek . "

    ";
    : print MAIL "Oerige wensen: " .$overige . "

    ";
    : close(MAIL);
    : }
    : }
    : }
    :
    : #------------------------------[ SUBROUTINE HEADER ]---------------------------#
    : # NAME: Success #
    : # DATA: [NONE] #
    : # DESC: Sends the appropriate success message back to Flash. #
    : #------------------------------------------------------------------------------#
    : sub Success
    : {
    : print "Content-type: text/plain

    ";
    : print "&result=okay";
    : print "&junk=1";
    : }
    :
    : #------------------------------[ SUBROUTINE HEADER ]---------------------------#
    : # NAME: Fail #
    : # DATA: $errormsg: Error message to send to Flash movie. #
    : # DESC: Sends the fail message along with an error message back to the Flash #
    : # movie. Performs URL encoding of errormessage before sending! #
    : #------------------------------------------------------------------------------#
    : sub Fail
    : {
    : my $errormsg = pop(@_);
    :
    : # URL ENCODE ERROR MESSAGE STRING
    : print "Content-type: text/plain

    ";
    : print "&result=fail";
    : print "&errormessage=$errormsg";
    : print "&junk=1";
    : exit;
    : }
    :

    [blue]There we go. Hope this helps - please take the security issues I mention seriously.[/blue]

    Jonathan


    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • james9james9 Posts: 3Member
    thankx it worked. pretty simple solution and also liked your security concerns. It never hurts to be safe rather than sorry.

    j
    ps if you need some help ever with Flash drop me a line
Sign In or Register to comment.