Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

interacting with sendmail

by synapse0 (Pilgrim)
on Aug 17, 2001 at 23:59 UTC ( #105797=perlquestion: print w/replies, xml ) Need Help??

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

Hello fellow monksters...
It has been requested for one of my cgi scripts to add the ability to email a link out from the script. One of the things I want for this script is to keep away from extraneous modules. So I figured I could talk with sendmail directly, but I'm wary of doing such things. I'd much rather use a module, but not for this project. So I ask your wisdom and vigilant eyes, see any potential Bad Things [TM] lurking around in there? Have hints on better ways?
-Syn0 (This is part of my EIGHT - Easy Image Gallery software)
(also, I know the email regex I use is not compliant, but it suffices for what I need (I think) and I would run into the module issue if I tried for full compliance)

The code is:
# MAIL-A-LINK info # if set to TRUE, mail-a-link will be displayed during img display my $mail_a_link = 'TRUE'; # location of sendmail binary my $sendmail = '/usr/bin/sendmail'; # message to send as body of mail # placeholders for use in body: # <: sender_name :> - name of the person who requested the email # <: image_link :> - link being emailed # <: image_name :> - name of the image my $mail_body = <<EOD; This message is being sent to you by $default_title galleries on behal +f of <:sender_name:>, who thinks you would enjoy this image:<BR> <:image_name:> at <:image_link:><BR> <BR> Feel free to stop by and have a look around.<BR> Thank you,<BR> <A href="$base_url">$default_title</A> EOD # /* ----------------------------------------------------------------- +-- */ # linkmail # /* ----------------------------------------------------------------- +-- */ sub linkmail { my $img_name = $img_lst[$curr_img_num] || &idx(); my($status, $to_name, $to_email, $from_name, $from_email) = ('')x5; if (($to_name = $cgi->param('to_name')) && ($to_email = $cgi->param('to_email')) && ($from_email = $cgi->param('from_email')) && ($from_name = $cgi->param('from_name'))) { $status = "\nERR: Invalid character in Recipient's name : $to_name +<BR>" unless $to_name =~ /^[\w\.\'\s]+$/; $status .= "\nERR: Malformed email address for Recipient : $to_ema +il<BR>" unless $to_email =~ /^[\w\-\.]+\@[\w\-\.]+\.\w+$/; $status .= "\nERR: Invalid character in Your name : $from_name<BR> +" unless $from_name =~ /^[\w\.\'\s]+$/; $status .= "\nERR: Malformed email address for You : $from_email<B +R>" unless $from_email =~ /^[\w\-\.]+?\@[\w\-\.]+?\.\w+$/; if ($status eq '') { # no errors, try and send email my $img_lnk = qq(<A href="@{[$cgi->url]}?opt=img&gal=$gal_id&id= +$img_name">@{[$cgi->url]}?opt=img&gal=$gal_id&id=$img_name</A>); $mail_body =~ s/<:\s*sender_name\s*:>/$from_name/g; $mail_body =~ s/<:\s*image_link\s*:>/$img_lnk/g; $mail_body =~ s/<:\s*image_name\s*:>/$img_name.jpg/g; my $mailmsg = <<EOD; From: $from_name <$from_email> To: $to_name <$to_email> Subject: Image link MIME-Version: 1.0 Content-Type: TEXT/HTML; charset=US-ASCII $mail_body EOD $ENV{PATH} = ''; if (open(MAIL, "| $sendmail -oi -t")) { print MAIL $mailmsg; if (close(MAIL)) { # all's well $status = qq( The link was mailed successfully.<BR> You can [ <A href="@{[$cgi->url]}?opt=img&gal=$gal_id&id=$img_na +me">return to gallery</A> ] or send the link to someone else: <HR>); $to_email = ''; $to_name = ''; } else { # close failed $status = qq(\n ERR: There was a problem processing the mess +age. Your link was not sent.<BR>); log_err("ERR: Mail-a-link failure during mail process/send : $!" +); } } else { # can't connect to server, log and err out $status .= qq(\n ERR: There was a problem connecting to the m +ail server. Your link was not sent.); log_err("ERR: Could not connect to sendmail : $!"); log_err(" Set the config var \$mail_a_link to 'FALSE' if thi +s error persists"); } } } &PrintHead(); print qq(\n<TABLE border=0 bgcolor="$colors{'img_cap'}" width="100%" +><TR><TD>&nbsp;</TD></TR></TABLE>); print qq( <!-- Start Mail-A-Link Table --> <TABLE border=0 width="100%"> <TR> <TD> $status <FORM action="@{[$cgi->url]}" method=POST> <INPUT type=hidden name=opt value="mal"> <INPUT type=hidden name=gal value="$gal_id"> <INPUT type=hidden name=id value="$img_name"> Sending an email link to a friend. <I>(All fields are require +d)</I><BR> <BR> Recipient's Name: (up to 30 characters long)<BR> <INPUT type=text name=to_name value="$to_name" size=15 maxlen +gth=30><BR> Recipient's Email address: (up to 80 characters long)<BR> <INPUT type=text name=to_email value="$to_email" size=30 maxl +ength=80><BR> <BR> Your Name: (up to 30 characters long)<BR> <INPUT type=text name=from_name value="$from_name" size=15 ma +xlength=30><BR> Your Email Address: (up to 80 characters long)<BR> <INPUT type=text name=from_email value="$from_email" size=30 +maxlength=80><BR> <INPUT type=submit value="Send this link!"> </FORM> <HR> Sending a link to this image:<BR> <IMG src="$img_url/$img_name.jpg" border=0> </TD> </TR> </TABLE> <!-- End Mail-A-Link Table --> ); &PrintTail(); }

Replies are listed 'Best First'.
Re: interacting with sendmail
by Trimbach (Curate) on Aug 18, 2001 at 02:57 UTC
    If you're bound and determined not to Build on the Shoulders of Giants and use modules then you should consider cutting and pasting the good parts of good modules directly into your code. A good example of this is your admittedly nasty email regex: why not just cut and paste the patented regex from Email::Valid into your code? It works, it's no effort on your part, and it means you don't have to formally import the module. You might also be able to do the same thing with one of the simple SMTP modules to eliminate your reliance on SendMail. Of course, this means your code is going to get long, but then, that's the whole reason why all this stuff was modularized in the first place.

    Gary Blackburn
    Trained Killer

      I know about modules, I use and prefer them.. read the above reply as to why I'm not using one in this case. As for cut'n'paste, well often modules rely on other modules to run.. which obviously wouldn't work and would easily render a script obnoxiously large. Yes I know that's why modules are written.
      Thanks for the hint on the regex in Email::Valid, I'll take a look at it. If I remember, it's quite a monstrous piece, and I don't really know if something like that is necessitated by this project, but I'll look into it.
      Hrmm.. and reading your post, I think a better answer to my question may have been, "You know, you can send mail w/out using sendmail. Talk to the SMTP server rather than sendmail", which may indeed be a better way to do it. Good idea, I say. I should have thought of that, but that's why I come here, in case I overlook something or my train of thought is off..
      Anyway, thanks..
Re: interacting with sendmail
by perrin (Chancellor) on Aug 18, 2001 at 01:55 UTC
    If your purpose in not requiring modules is to make this program more portable, requiring sendmail is a step in the wrong direction. A simple SMTP module from CPAN would be more universal.
      I don't believe I ever mentioned portability. The purpose for not using CPAN modules is that they can be bothersome at best to install, particularily if one a) doesn't have root access, b) doesn't particularily care much either way about the programming language the app was created in. Like I said, I'd personally much rather use a mod and save myself the trouble, but I do not believe it is appropriate for this script. It's an install, set config vars, and run kinda deal. The link mail func is just a perk, if they happen to have sendmail useable, then hey, more power to em. If not, well it doesn't really matter.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://105797]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2023-02-01 11:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found