PDA

View Full Version : Emails


misslilbit02
07-29-2005, 02:29 AM
Hi,

I'm tying to send two emails from one form. I have my code below but if there is an easier way please assist. Someone please help.


#!/usr/bin/perl

# 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 = 'nikki_barnard@bellsouth.net';
$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: nikki_barnard@bellsouth.net";
# 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: mailinglist@fearfullymadedesigns.com";

# print a subject line so you know it's from your form cgi.


print MAIL "Reply-to: mailinglist@fearfullymadedesigns.com";
# 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 Fearfully and Wonderfully Made, Inc. 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;

}

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

mlseim
07-29-2005, 03:38 AM
Are you saying that the script you listed does not work?
or, it works but you want a smaller script?

I don't understand.

If it works, that's fine. To send two separate emails, you need to
send two separate emails. There's nothing wrong with that.

misslilbit02
08-01-2005, 02:38 AM
No I've tried this script but it doesn't work. If I only send one email then it works but when I try to send both then I get no emails. Do you have any idea why this is?

FishMonger
08-01-2005, 05:06 AM
Yes, there is an easier and much better way to do this. First of all, change the method you're using for retrieving the form submission, it's been depreciated for about the last 10 years; use the CGI module. Here's a brief example:

#!/usr/bin/perl -w

use strict;
use CGI;

my $q = new CGI;
my %FORM = $q->Vars;
my $recipient = 'me@mydomain.com';

if ($FORM{'email'} =~ /^$/ ){
&dienice("Please enter a valid email address. Please press your back button to correct.");
}

my $msg = MIME::Lite->new(
From => $FORM{'email'},
To => $recipient,
Subject =>'Mailing List',
Data =>"Hi,\n\nPlease add me to your mailing list.\n\n"
);
$msg->send;


I'll post a more complete example when I have more time but for now read the documentaion on these modules.

http://search.cpan.org/~lds/CGI.pm-3.10/CGI.pm
http://search.cpan.org/~yves/MIME-Lite-3.01/lib/MIME/Lite.pm

mlseim
08-01-2005, 05:27 AM
OK,

The problem with your code is this line here:
$recipient = 'nikki_barnard@bellsouth.net';

Use quotes and maybe try a slash in front of @:
$recipient = "nikki_barnard\@bellsouth.net";

Below is a more condensed version of the script using CGI instead of
parsing your own form variables. (it's a little more secure that way).

FishMonger (a much more experienced programmer), has the better
method ... but I think mine is easier to understand for the "novice".

Try both methods :)

#!/usr/bin/perl

use CGI ':standard';

$from = param('email');

# where is the mail program?
$mailprog = '/usr/sbin/sendmail';

#note: some servers want the @ symbol escaped ( \@ ).
$recipient = "nikki_barnard\@bellsouth.net";

open (MAIL, "|$mailprog -t") or die "Can't send mail.\n";
print MAIL "To: $recipient\n";
print MAIL "From: $from\n";
print MAIL "Subject: Add to Mailing List\n";
print MAIL "\n";
print MAIL "=====================================================\n";
print MAIL "
Hi,

Please add me to your mailing list.

Thank You
";
print MAIL "\n";
close(MAIL);

open (MAIL, "|$mailprog -t") or die "Can't send mail.\n";
print MAIL "To: $from\n";
print MAIL "From: $recipient\n";
print MAIL "Subject: Thanks for Joining Our Mailing List!\n";
print MAIL "\n";
print MAIL "=====================================================\n";
print MAIL "
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 Fearfully and Wonderfully Made, Inc. will be doing.

Sincerely,
Janna C. Chinnery
CEO

";
print MAIL "\n";
close(MAIL);

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

FishMonger
08-01-2005, 10:17 PM
The problem with your code is this line here:
$recipient = 'nikki_barnard@bellsouth.net';

Use quotes and maybe try a slash in front of @:
$recipient = "nikki_barnard\@bellsouth.net";Either of those will work but the second one requires you to escape the @ symbol so that it doesn't get interpreted as an array. A good rule to follow when quoting is use double quotes when you need variable interpolation otherwise use single quotes. The second email wasn't being sent due to this variable interpolation.

print MAIL "To: nikki_barnard@bellsouth.net";

should be

print MAIL 'To: nikki_barnard@bellsouth.net';
or
print MAIL "To: nikki_barnard\@bellsouth.net";

The other print MAIL statements would need the same correction.

Opening a pipe to sendmail and using multiple print MAIL statements is very common but to me it adds syntactic noise and it reduces platform independence. Normally, I prefer to use either Net::SMTP or MIME::lite so I can maintatin portability.

misslilbit02
08-01-2005, 10:39 PM
Okay thanks guys you have been a GREAT GREAT help. Thanks a bunch for you time with this matter. I really appreciate it.

Misslilbit02

mlseim
08-02-2005, 01:38 AM
FishMonger ...

That's really interesting.

I experimented with this quote thing.

With my webhost (Ipowerweb), and tried these three lines, one at a time:

#line 1
$recipient = "myname@comcast.com";

#line 2
$recipient = 'myname@comcast.com';

#line 3
$recipient = 'myname\@comcast.com';

Line 1 works OK.
Line 2 does not send email.
Line 3 works OK.

So, there must be something about the single quotes and the "escaped" @.

sometimes it's just frustrating ... :mad:

FishMonger
08-02-2005, 02:04 AM
FishMonger ...

That's really interesting.

I experimented with this quote thing.

With my webhost (Ipowerweb), and tried these three lines, one at a time:

#line 1
$recipient = "myname@comcast.com";

#line 2
$recipient = 'myname@comcast.com';

#line 3
$recipient = 'myname\@comcast.com';

Line 1 works OK.
Line 2 does not send email.
Line 3 works OK.

So, there must be something about the single quotes and the "escaped" @.

sometimes it's just frustrating ... :mad:
Are you sure about that? I'd expect it to be the exact opposite.

Edited: However, it's being interpolated twice, once in the assignment and once in the print statement.

mlseim
08-02-2005, 04:34 AM
FishMonger ...

I copied the same exact script to another webhost (Netfirms.com).

With that host, all three lines work OK.

go figure.

Anyhow, no big deal. I just do things until they work ... not necessarily
understanding them (although I learn something new all the time).