Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: sendmail working for me but not another

by tobyink (Canon)
on May 25, 2020 at 14:51 UTC ( [id://11117235]=note: print w/replies, xml ) Need Help??


in reply to sendmail working for me but not another

Firstly, your $email_message looks weird. There should be a blank line after the final header line. So after the "From" line, but before the "First name" line, leave a totally blank line. Not even any whitespace characters on it. That certainly could be causing issues; fix that first.

If that doesn't help, you state two different issues in your question which might seem the same, but very much are not:

  • People do not receive emails
  • Emails are never sent out

If people are not receiving emails, it doesn't mean that they were never sent out. They could have been sent but lost somewhere along the way to the user's inbox, perhaps dropped by an overzealous spam filter.

You want to check your outgoing mail server log files to find out if the emails are being sent or not. That log may also have some useful information about the status codes the recipient's mail server has responded with.

If my first suggestion didn't fix things, and there's no clues in the mail log about what went wrong, then I would suggest the most common explanation is that your mail is being filtered as spam, so fixing that would be a good thing to attempt. Look into configuring your mail server to add DKIM headers to outgoing mail, and put DKIM and SPF entries into your DNS.

Oh, and by the way, your check to see if an email address is valid is pretty bad. It rejects bob-smith@bobs-furniture.com.

Replies are listed 'Best First'.
Re^2: sendmail working for me but not another
by kickingchicken (Initiate) on May 25, 2020 at 17:19 UTC
    ## Create the email message body $email_message = qq{To: $recipient Subject: Adoption Application From: $fstname $lstname<$email> First name: $fstname Last name: $lstname

    So you are saying it should look like this? Thank you toby, i'm trying to clarify it, its just hard to explain. I do receive emails of the form data, i have received about 20 so far. However a few people have complained they have submitted but I haven't received the data, neither have the other 2 email boxes. One person in particular, submits the form and makes it to the confirmation page, but no one gets the data in an email. I know the code is horrible but i'm learning. I will just remove the email validity code. I don't send an email to that, its just a means to collect their address for this application.

    My host is telling me i'm not allowed to look at the email logs, I've asked them what I can access and i'm waiting to hear. I'm not sure they know what i'm talking about, but they did present to me a list of emails that were sent successfully with my perl but that doesn't help me.. cause I know they were sent, I have them. I can't ssh in right now, having trouble with it and i'm trying to correct that through them. I will try this new empty line you asked me to add, and also what bliako mentioned. Thanks you two.

      So you are saying it should look like this?

      Yes, what you showed is correct. Note it's generally better to use heredocs instead of a multiline qq, so e.g.

      my $email_message = <<"ENDMAIL"; To: $recipient Subject: Adoption Application ... ENDMAIL
      My host is telling me i'm not allowed to look at the email logs, I've asked them what I can access and i'm waiting to hear.

      You could write out your data to a logfile on your account, in case something with the emails doesn't work out.

      I know the code is horrible but i'm learning.

      If this were a legacy script, ok, but if this a new script, then you'd be much better off learning something more modern. I've been doing a lot with Mojolicious recently, and I think it's pretty great. See Mojolicious::Guides::Tutorial for getting started. Otherwise, at the very least what this script needs is to Use strict and warnings! See also UP-TO-DATE Comparison of CGI Alternatives.

      And with that many variables, it'd be much better to store them in a data structure like a hash. Then you could do things like loop over it so you don't have to repeat the regex a bunch of times. (Also, note you don't seem to be doing anything with the yardtypeother field.)

      Also, you should check if the host has Email::Sender::Simple installed, that should be much better and easier to use than shelling out to sendmail.

        See Mojolicious::Guides::Tutorial for getting started.
        Thanks, I will definitely check mojo out. My script is legacy, at least this is a good opportunity to learn perl, should have long time ago. I will up date my script with heredocs. I was using strict and warnings but I keep getting blank page or 404. I've been working on finding out what is causing it, but havn't been able to. I'm using use CGI::Carp qw(fatalsToBrowser warningsToBrowser); When I get rid of strict it will process with warnings, 3 variables were not initialized properly. but with strict it is just a blank screen. There is a lot of information here, i'm going to go through what everyone has given me and change my code. If I could get some kind of log, it would help but i'm still waiting on my host..

      Also, here's what I mean about eliminating repetition. It's untested though.

      #!/usr/bin/perl use strict; use warnings; use CGI qw/:standard/; $CGI::POST_MAX = 1024 * 1; my %fields = ( fstname => [ first => 40 ], lstname => [ last => 40 ], adda => [ 1ad1a => 60 ], addb => [ 1ad1b => 60 ], city => [ 1ad2 => 60 ], state => [ 1ad3 => 20 ], zip => [ 1ad4 => 5 ], email => [ eadd => 70 ], phone => [ pnum => 16 ], residence => [ residence => 9 ], yardtype => [ yardtype => 16 ], yardtypeother => [ yardtypeother => 40 ], landlord => [ landlord => 40 ], preadda => [ 2ad1 => 60 ], precity => [ 2ad2 => 60 ], prestate => [ 2ad3 => 20 ], prezip => [ 2ad4 => 5 ], alone => [ alone => 14 ], household => [ household => 100 ], vet => [ vet => 45 ], pet => [ pet => 3 ], petname => [ petnme => 30 ], currentpets => [ currentpets => 60 ], previouspets => [ previouspets => 60 ], references => [ references => 140 ], ok => [ ok => 7 ], ); my %values; for my $key ( keys %fields ) { my ( $post_param, $max_length ) = @{ $fields{$key} } my $value = substr( param($post_param), 0, $max_length ); $value =~ s/[\|\/\\}{\[\]\(\)\*&\^%\$\#<>;:]/ /g; $values{$key} = $value; } $values{email} =~ tr/[A-Z]/[a-z]/; ## Check email address if ( $values{email} ne "" and $values{email} !~ /\@/ ) { print "Content-type: text/plain\n\n"; print "error (Your email is invalid, please check and resubmit you +r application)"; exit; } ## Check data my $total_form = $values{fstname} . $values{lstname} . $values{email}; if ( length($total_form) < 25 ) { print "Content-type: text/plain\n\n"; print "error (The form was incomplete, please check that you fille +d in your full first and last name, and email address, then resubmit +your application.)"; exit; } my $recipient = "email1\@emailsrv1, email2\@emailsrv2, email3\@emailsr +v3"; ## Create the email message body my $email_message = <<"MESSAGE" To: $values{recipient} Subject: Adoption Application From: $values{fstname} $values{lstname} <$values{email}> First name: $values{fstname} Last name: $values{lstname} Current address: $values{adda} $values{addb} $values{city} $values{state} $values{zip} _________________________________________________ Email address: $values{email} Phone number: $values{phone} Type of residence: $values{residence} Yard: $values{yardtype} Landlord contact info: $values{landlord} Previous address: $values{preadda} $values{precity} $values{prestate} $values{prezip} How many hours will pet be alone: $values{alone} Name and age of people in household: $values{household} Veterinarian: $values{vet} Dog or Cat: $values{pet} Name of pet requested: $values{petname} Current pets: $values{currentpets} Previous pets: $values{previouspets} References: $values{references} Aggree to consent form: $values{ok} MESSAGE ## Send email my $mailprog = '/usr/sbin/sendmail'; open my $mailhandle, '|-' "$mailprog -t" or die "Could not open mailhandle"; print $mailhandle $email_message; close $mailhandle; print "Location: application-submitted\n\n"; exit;

        Wow, thanks for this. I see what you mean. I will go through it after I figure out where my error is. In the send mail part

        my $mailprog = '/usr/sbin/sendmail'; open my $mailhandle, '|-' "$mailprog -t" or die "Could not open mailhandle"; print $mailhandle $email_message; close $mailhandle;
        What are you doing with mailhandle?

      Yep, that's where the empty line should be.

      Sucks not having access to logs. I'm generally admin of most machines I work on these days, but I have had to deal with limited hosting accounts at times and not having direct access to logs can be one of the most frustrating things, especially with how prone I am to typos and other silly little mistakes.

      And don't worry about your code. It's not that bad; standard beginner stuff. Could be a lot worse. It's pretty repetitive though. As you start learning more, you'll learn to hate repetitive code, and start to use loops and write things in a more abstract way. Everybody has to start somewhere though. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11117235]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-03-29 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found