jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm having a problem sending an email with sendmail. (CGI)

what I need to do is take the parameters from input form, use those to extract an email address and send an email to that address

I have tested the script using a straight typed email address, and it works, as does an email picked up using $blah= param(blah). However when I try with an address extracted from my DB the email isn't sent

I have used the straight email and also replacement: s/@/\@/; I really can't understand what is happening and wondered if someone would mind taking a look and letting me know what I'm doing wrong.

#!/usr/bin/perl -w use strict; use CGI; my $query = new CGI; my $thisurl = "http://www.my_site.com/cgi-bin/email.pl"; my $id_in=$query->param('id_in'); my $Guest_Email=$query->param('Guest_Email'); my $check=$query->param('check'); my ($Password,$U_Timestamp,$three,$four,$skipthisfield); my @emailer = (); my $data="/home/users/t/my_site.com-99078/data/edit/data.txt"; open (FILE, "$data"); while (my $line =<FILE>) { ($Password,$U_Timestamp,$three,$four,$skipthisfield) = split "\t", +$line; if ($id_in eq $three) { push (@emailer, $four); last; } } my $Email_address = join (" ", @emailer); $Email_address =~ s/@/\@/; print "Content-type: text/html\n\n"; print "Thank you:<br>\n <br>Detailss $id_in<br>\n"; print "three is: $three"; print "Email is: $Email_address<br>"; print "Check is: $check"; &sending(); #SEND EMAIL================================= sub sending { my $check; my $sendmailer; my $emailto; my $Email_address; my $emailfrom; my $message_no; my $message; $Email_address =~ s/@/\@/; if ($check == 0) { $sendmailer="/usr/sbin/sendmail"; $emailto="$Email_address"; $emailfrom="enquiry\@my_site.com"; open (MAIL, "| $sendmailer -t") || die "sendmailer: $!"; print MAIL "To: $emailto\n"; print MAIL "From: $emailfrom\n"; print MAIL "Subject: Request for information\n"; print MAIL "Content-type: text/html\n\n"; print MAIL "<html><body><p>\n"; print MAIL "<font size=\"2\" color=\"#CC0000\">\n"; print MAIL "<u>REQUEST FOR INFORMATION</u><BR><BR>\n"; print MAIL "I am interested in receiving information on : $id_in<BR>\n +"; print MAIL "blah\n"; print MAIL "Any other comments:<BR>\n"; print MAIL "$message<br>\n"; print MAIL "</font></body></html>\n"; last; close(MAIL); close(MAIL); } elsif ($check == 1) { $sendmailer="/usr/sbin/sendmail"; $emailto="$Guest_Email"; $emailfrom="enquiry\@my_site.com"; open (MAIL, "| $sendmailer -t") || die "sendmailer: $!"; print MAIL "To: $emailto\n"; print MAIL "From: $emailfrom\n"; print MAIL "Subject: Response from owner\n"; print MAIL "Content-type: text/html\n\n"; print MAIL "<html><body><p>\n"; print MAIL "<font size=\"2\" color=\"#FFCC00\">\n"; print MAIL "You have a response from the owner of property: $id_in.<br +>\n"; print MAIL "<br>\n"; print MAIL "Message follows:<br>\n"; print MAIL "$message<br><br>\n"; print MAIL "\n"; print MAIL "</font></body></html>\n"; last; close(MAIL); close(MAIL); } elsif ($check == 2) { $sendmailer="/usr/sbin/sendmail"; $emailto="$Email_address"; $emailfrom="enquiry\@my_site.com"; open (MAIL, "| $sendmailer -t") || die "sendmailer: $!"; print MAIL "To: $emailto\n"; print MAIL "From: $emailfrom\n"; print MAIL "Content-type: text/html\n\n"; print MAIL "<html><body><p>\n"; print MAIL "<font size=\"2\" color=\"#CC0000\">\n"; print MAIL "<br>\n"; print MAIL "Message follows:<br>\n"; print MAIL "$message<br><br>\n"; print MAIL "\n"; print MAIL "</font></body></html>\n"; last; close(MAIL); close(MAIL); #end of patch } elsif ($check == 3) { $sendmailer="/usr/sbin/sendmail"; $emailto="$Guest_Email"; $emailfrom="enquiry\@my_site.com"; open (MAIL, "| $sendmailer -t") || die "sendmailer: $!"; print MAIL "To: $emailto\n"; print MAIL "From: $emailfrom\n"; print MAIL "Subject: Response from owner\n"; print MAIL "\n"; print MAIL "You have a response: $id_in.<br>\n"; print MAIL "Please click the following link:<br>\n"; print MAIL "<br>\n"; print MAIL "$message<br><br>\n"; print MAIL "\n"; last; close(MAIL); close(MAIL); } }

Replies are listed 'Best First'.
Re: emailing with an address taken from a database
by steves (Curate) on Feb 11, 2003 at 12:52 UTC

    You should show us some output, particularly some that shows what the email address is. Your while loop is strange to say the least: You find the address you want, push it onto an array, break out of the loop, then form your email address by joining that array of one with spaces. Why not just set a "found email address" variable and break out of the loop? It seems like you're trying to do something else there that's not obvious. I don't think '@' is the issue because you're storing the email address in a scalar -- not specifying it with double quotes where @ would be interpolated. Your @ substitutions are not doing what you think though. If you want a backslash, you need to backslash it since it's the escape character. I think you want this, although, as I said, I don't think it will make any difference:

    $Email_address =~ s/@/\\\@/;

Re: emailing with an address taken from a database
by fuzzyping (Chaplain) on Feb 11, 2003 at 12:27 UTC
    You haven't given us enough information to understand the format of the data you're munging. I expect that you haven't escaped the @ properly. What are $four and @emailer supposed to look like? Why are you join()ing @emailer with whitespace?

    If you're not enclosing the email address in single quotes, or escaping the @, you're allowing for variable substitution, and Perl is likely treating the "user@domain" as "user" . @domain, where @domain is a nonexistent array.

    You also haven't provided any errors from your maillog or the strict pragma. Please help us help you. :)

    -fp
Re: emailing with an address taken from a database
by jonnyfolk (Vicar) on Feb 11, 2003 at 14:00 UTC
    Many thanks for your responses, steves and fuzzyping.
    The while statement was plain wrong - no hidden agenda there! (in fact there's no hidden agenda about any of this(:). I've now got:
    my $Email_address; my ($Password,$U_Timestamp,$three,$four,$skipthisfield); my $data="/path/data/edit/data.txt"; open (FILE, "$data"); while (my $line =<FILE>) { ($Password,$U_Timestamp,$three,$four,$skipthisfield) = split "\t", +$line; if ($id_in eq $three) { $Email_address eq $four; last; } }
    which seems to me to be much more direct.

    $four holds the email address in the file which is to be extracted: the format of the email address me@my.com

    I'm afraid that on this rented server space I don't have access to error logs so I'm still somewhat baffled as to why the email isn't getting transmitted.

      This line:

      $Email_address eq $four;
      should read:
      $Email_address = $four;
      eq is for string comparison. = is used for all scalar assignments.

      I'd also add error checking to the open call:

      open (FILE, "$data") or die "Failed to open $data: $!\n";

      You need to find some way to see debugging output. The original code had debug statements going to the browser. Do those work? If so, I'd start adding some debugging so you can get an idea where things are going wrong. It will be hard to pinpoint without the ability to debug.

        Darn it! I had the $Email_address = $four; and then changed it just before posting - thanks for pointing it out.

        I tried adding debugging and just inserted a print "Email_address"; further down the script and decided I would get rid of the subroutine element and the (check=) statements just to try to simplify it.
        Immediately I received an email generated from the script! Progress, but it comes with befuddlement!!. what I have now is:
        #!/usr/bin/perl -w use strict; use CGI; my $query = new CGI; my $thisurl = "http://www.my_site.com/cgi-bin/email.pl"; my $id_in=$query->param('id_in'); my $Guest_Email=$query->param('Guest_Email'); my $check=$query->param('check'); my $Email_address; my ($Password,$U_Timestamp,$three,$four,$skipthisfield); my @emailer = (); my $data="/path/data/edit/data.txt"; open (FILE, "$data"); while (my $line =<FILE>) { ($Password,$U_Timestamp,$three,$four,$skipthisfield) = split "\t", +$line; if ($id_in eq $three) { $Email_address = $four; last; } } print "Content-type: text/html\n\n"; my $sendmailer; my $emailto; my $emailfrom; my $message_no; my $message; $sendmailer="/usr/sbin/sendmail"; $emailto="$Email_address"; $emailfrom="enquiry\@my_site.com"; print "Email is: $Email_address"; open (MAIL, "| $sendmailer -t") || die "sendmailer: $!"; print MAIL "To: $emailto\n"; print MAIL "From: $emailfrom\n"; print MAIL "Subject: Request for information\n"; print MAIL "Content-type: text/html\n\n"; print MAIL "<html><body><p>\n"; print MAIL "<font size=\"2\" color=\"#CC0000\">\n"; print MAIL "<u>REQUEST FOR INFORMATION</u><BR><BR>\n"; print MAIL "I am interested in receiving information on : $id_in<BR>\n +"; print MAIL "blah\n"; print MAIL "Any other comments:<BR>\n"; print MAIL "$message<br>\n"; print MAIL "</font></body></html>\n"; last; close(MAIL); close(MAIL);
        Any ideas why it should work like that but not in the previous version? (At least this gives me a basis to start looking but there might be something obvious which I'm not getting).