Web Analytics Made Easy -
StatCounter Emails - CodingForum

Announcement

Collapse
No announcement yet.

Emails

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Emails

    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 = '[email protected]';
    $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: [email protected]";
    # 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: [email protected]";

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


    print MAIL "Reply-to: [email protected]";
    # 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";

  • #2
    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.

    Comment


    • #3
      no it doesn't work

      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?

      Comment


      • #4
        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:

        Code:
        #!/usr/bin/perl [COLOR=Red]-w[/COLOR]
        
        [COLOR=Red]use strict;
        use CGI;[/COLOR]
        
        my $q = new CGI;
        my %FORM = $q->Vars;
        my $recipient = '[email protected]';
        
        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-Li...b/MIME/Lite.pm

        Comment


        • #5
          OK,

          The problem with your code is this line here:
          $recipient = '[email protected]';

          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

          Code:
          #!/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";
          Last edited by mlseim; Jul 31, 2005, 11:48 PM.

          Comment


          • #6
            Code:
            The problem with your code is this line here:
            $recipient = '[email protected]';
            
            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: [email protected]";

            should be

            print MAIL 'To: [email protected]';
            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.

            Comment


            • #7
              Thanks

              Okay thanks guys you have been a GREAT GREAT help. Thanks a bunch for you time with this matter. I really appreciate it.

              Misslilbit02

              Comment


              • #8
                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 = "[email protected]";

                #line 2
                $recipient = '[email protected]';

                #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 ...

                Comment


                • #9
                  Originally posted by mlseim
                  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 = "[email protected]";

                  #line 2
                  $recipient = '[email protected]';

                  #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 ...
                  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.
                  Last edited by FishMonger; Aug 1, 2005, 08:08 PM.

                  Comment


                  • #10
                    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).

                    Comment

                    Working...
                    X