I was wondering, what can I do to send multiple emails after a form is submitted? I have a form and it inturns gathers info from the form and sends an email saying this person signed up for you mailiinglist, but what if I want to also send another email that tells the person hey we thank you for signing up and will be keeping you abreast of new designs. How can I code this in CGI. I've included my code just in case it's necessary.
#!/usr/bin/perl
print "Location: http://www.example.com/index2.htm#thanks\n\n";
# parse the form data.
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
#error messages
if ($FORM{'email'} =~ /^$/ ){
&dienice("Please enter a valid email address. Please press your back button to correct.");
}
# where is the mail program?
$mailprog = '/usr/sbin/sendmail';
$recipient = 'mailinglist@example.com';
$from = $FORM{'email'};
open (MAIL, "¦$mailprog -t") or &dienice("Can't access $mailprog!\n");
print MAIL "To: $recipient\n";
# Reply-to can be set to the email address of the sender,
# assuming you have actually defined a field in your form
# called 'email'.
print MAIL "From: $from\n";
# print a subject line so you know it's from your form cgi.
print MAIL "Reply-to: $FORM{'email'}\n";
# print out a subject line so you know it's from your form cgi.
# The two \n\n's end the header section of the message.
# anything you print after this point will be part of the
# body of the mail.
print MAIL "Subject: Mailing List\n\n";
# here you're just printing out all the variables and values,
# just like before in the previous script, only the output
# is to the mail message rather than the followup HTML page.
print MAIL <<End1;
Hi,
Please add me to your mailing list.
Thank you
End1
[edited by: jatar_k at 5:21 pm (utc) on July 27, 2005]
[edit reason] examplified url [/edit]
open (MAIL, "¦$mailprog -t") or &dienice("Can't access $mailprog!\n");
print MAIL "To: $recipient\n";
# Reply-to can be set to the email address of the sender,
# assuming you have actually defined a field in your form
# called 'email'.
print MAIL "From: $from\n";
# print a subject line so you know it's from your form cgi.
print MAIL "Reply-to: $FORM{'email'}\n";
# print out a subject line so you know it's from your form cgi.
# The two \n\n's end the header section of the message.
# anything you print after this point will be part of the
# body of the mail.
print MAIL "Subject: Mailing List\n\n";
# here you're just printing out all the variables and values,
# just like before in the previous script, only the output
# is to the mail message rather than the followup HTML page.
print MAIL <<End1;
Hi,
Please add me to your mailing list.
Thank you
End1
I tried what you suggested but I got an internal server error. My code is below...
#!/usr/bin/perl
print "Location: http://www.example.com/index2.htm#thanks\n\n";
# parse the form data.
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
#error messages
if ($FORM{'email'} =~ /^$/ ){
&dienice("Please enter a valid email address. Please press your back button to correct.");
}
# where is the mail program?
$mailprog = '/usr/sbin/sendmail';
$recipient = 'mailinglist@example.com';
$from = $FORM{'email'};
open (MAIL, "¦$mailprog -t") or &dienice("Can't access $mailprog!\n");
print MAIL "To: $recipient\n";
# Reply-to can be set to the email address of the sender,
# assuming you have actually defined a field in your form
# called 'email'.
print MAIL "From: $from\n";
# print a subject line so you know it's from your form cgi.
print MAIL "Reply-to: $FORM{'email'}\n";
# print out a subject line so you know it's from your form cgi.
# The two \n\n's end the header section of the message.
# anything you print after this point will be part of the
# body of the mail.
print MAIL "Subject: Mailing List\n\n";
# here you're just printing out all the variables and values,
# just like before in the previous script, only the output
# is to the mail message rather than the followup HTML page.
print MAIL <<End1;
Hi,
Please add me to your mailing list.
Thank you
End1
close(MAIL);
open (MAIL, "¦$mailprog -t") or &dienice("Can't access $mailprog!\n");
print MAIL "To: $from";
# Reply-to can be set to the email address of the sender,
# assuming you have actually defined a field in your form
# called 'email'.
print MAIL "From: $recipient\n";
# print a subject line so you know it's from your form cgi.
print MAIL "Reply-to: $recipient\n";
# print out a subject line so you know it's from your form cgi.
# The two \n\n's end the header section of the message.
# anything you print after this point will be part of the
# body of the mail.
print MAIL "Subject: Thanks for joining\n\n";
# here you're just printing out all the variables and values,
# just like before in the previous script, only the output
# is to the mail message rather than the followup HTML page.
print MAIL <<End2;
Dear Viewer,
Thank you for joining our mailing list. We look forward to sending you all of our new designs as we release them.
While we promise not to bombard you with junk email you can look forward to being updated on all of the spectacular things that we will be doing.
Sincerely,
Janna C. Chinnery
CEO
End2
close(MAIL);
sub dienice {
($errmsg) = @_;
print "<h2>Error</h2>\n";
print "$errmsg<p>\n";
print "</body></html>\n";
exit;
}
[edited by: jatar_k at 2:12 am (utc) on July 29, 2005]
It's not sending the second email. Code is below...
#!/usr/bin/perl
print "Location: http://www.example.com/index2.htm#thanks\n\n";
# parse the form data.
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
#error messages
if ($FORM{'email'} =~ /^$/ ){
&dienice("Please enter a valid email address. Please press your back button to correct.");
}
# where is the mail program?
$mailprog = '/usr/sbin/sendmail';
$recipient = 'mailinglist@example.com';
$from = $FORM{'email'};
open (MAIL, "¦$mailprog -t") or &dienice("Can't access $mailprog!\n");
print MAIL "To: $recipient\n";
# Reply-to can be set to the email address of the sender,
# assuming you have actually defined a field in your form
# called 'email'.
print MAIL "From: $from\n";
# print a subject line so you know it's from your form cgi.
print MAIL "Reply-to: $FORM{'email'}\n";
# print out a subject line so you know it's from your form cgi.
# The two \n\n's end the header section of the message.
# anything you print after this point will be part of the
# body of the mail.
print MAIL "Subject: Mailing List\n\n";
# here you're just printing out all the variables and values,
# just like before in the previous script, only the output
# is to the mail message rather than the followup HTML page.
print MAIL <<End1;
Hi,
Please add me to your mailing list.
Thank you
End1
close(MAIL);
$recipient = 'mailinglist@example.com';
$from = $FORM{'email'};
open (MAIL, "¦$mailprog -t") or &dienice("Can't access $mailprog!\n");
print MAIL "To: $from";
# Reply-to can be set to the email address of the sender,
# assuming you have actually defined a field in your form
# called 'email'.
print MAIL "From: $recipient\n";
# print a subject line so you know it's from your form cgi.
print MAIL "Reply-to: $recipient\n";
# print out a subject line so you know it's from your form cgi.
# The two \n\n's end the header section of the message.
# anything you print after this point will be part of the
# body of the mail.
print MAIL "Subject: Thanks for joining\n\n";
# here you're just printing out all the variables and values,
# just like before in the previous script, only the output
# is to the mail message rather than the followup HTML page.
print MAIL <<End1;
Dear Viewer,
Thank you for joining our mailing list. We look forward to sending you all of our new designs as we release them.
While we promise not to bombard you with junk email you can look forward to being updated on all of the spectacular things
that we will be doing.
Sincerely,
Janna C. Chinnery
CEO
End1
close(MAIL);
sub dienice {
($errmsg) = @_;
print "<h2>Error</h2>\n";
print "$errmsg<p>\n";
print "</body></html>\n";
exit;
}
[edited by: jatar_k at 2:14 am (utc) on July 29, 2005]
[edit reason] no urls thanks [/edit]
Then, try this skeleton version and if it works OK, add the form parsing routine and replace the hardcoded email addresses with the appropriate form variables.
#!/usr/bin/perl
# PARSE FORM
# SEND MAIL
open (MAIL, "¦/usr/sbin/sendmail -t");
print MAIL "To: mailinglist@example.com\n";
print MAIL "From: mailinglist@example.com\n";
print MAIL "Subject: Test\n\n";
print MAIL "Test\n\n";
close (MAIL);
# DISPLAY THANK YOU
print "Location: http://www.example.com/index2.htm#thanks\n\n";
exit;
[edited by: jatar_k at 2:14 am (utc) on July 29, 2005]