Forum Moderators: coopster & phranque

Message Too Old, No Replies

actions

sending two emails in CGI

         

misslilbit02

3:39 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



Hi,

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]

WWMike

4:31 am on Jul 28, 2005 (gmt 0)

10+ Year Member



Just copy and paste an additional section like the one below and change the message content that's between the <<End1 and End1 tags:

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

misslilbit02

5:17 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



Hey,

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]

misslilbit02

5:19 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



Never mind got it to work. Sorry to bother you. Thanks for your help it is GREATLY APPRECIATED!

misslilbit02

5:35 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



Okay maybe I do need help.

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]

WWMike

6:54 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



One thing you probably need to do is change the second set of tags to <<End2 and End2. Try that and see if that's it.

misslilbit02

9:23 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



When I do that that's when I get the internal server error.

misslilbit02

9:58 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



okay I kept playing with it and I finally got it to not give me an enternal server error but now I don't get any emails.

Oh goodness help.

WWMike

10:51 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



This should be the LAST thing your script does:

print "Location: http://www.example.com/index2.htm#thanks\n\n";

Move that to the bottom.

misslilbit02

12:11 am on Jul 29, 2005 (gmt 0)

10+ Year Member



nope still not working.

WWMike

1:40 am on Jul 29, 2005 (gmt 0)

10+ Year Member



First, manually send yourself a test email to mailinglist@example.com to verify that you can receive mail at that alias.

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]