Writing a Simple Form Mailer

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

11-Mar-03 00:00
This is a forum topic for discussing the article "Writing a Simple Form Mailer":

http://www.elated.com/articles/writing-a-simple-form-mailer/

This tutorial will show you how to build a simple form mailer script, so that visitors can send you emails from your site. Along the way we explain some new Perl concepts, including the CGI library module.
08-Oct-09 19:32
The script works fine and I receive an e-mail but the problem is when I open it it's blank (absolutely no text). Any suggestions??
09-Oct-09 01:12
Hi tjstab, and welcome!

Maybe your 'comments' form field name is different to the parameter name in your script?

If you can post your script and HTML form here then I can take a look for you.

Cheers,
Matt

--
Matt Doyle, Elated
20-Dec-10 17:40
Finally, I set up a contact form that works! Thank you for your article!

Instead of the "end html" message, can I have the submitter directed to a different html document? Is there a way to insert a link instead of a message?

Thank you
03-Jan-11 03:09
@Will: Sorry for the delay in replying (Xmas and all that!).

Yes, you could include a link in the output HTML, eg:


<body>Thanks for filling in our form! <a href="page.html">Click here to continue</a></body>


Or you could issue a redirect to take the user to the new page automatically:

http://search.cpan.org/dist/CGI.pm/lib/CGI.pm#GENERATING_A_REDIRECTION_HEADER

Hope that helps
Matt

--
Matt Doyle, Elated
03-Jan-11 14:25
Thanks! I think I'll try that redirect issue.

I appreciate you help.

Sincerely,
Will
06-Jan-11 14:17
Matt,

I tried adding a second contact form, and made a second cgi file, but named it differently. And then of course I specified the second file in my form action=" " . But it doesn't work.

Do I have to name the cgi file, "form_mailer.cgi" in order for this to work?

Thanks,
Will
06-Jan-11 23:46
@Will: Sounds like you did the right things. What error are you getting?

--
Matt Doyle, Elated
07-Jan-11 11:09
I get Internal Server Error - "The server encountered an internal error or misconfiguration and was unable to complete your request."

Here's the cgi file:




#!/usr/bin/perl

use CGI;

my $query = new CGI;

print $query->header ( );

my $name = $query->param("name");
my $phone_number = $query->param("phone_number");
my $email_address = $query->param("email_address");
my $message = $query->param("message");
my $referral = $query->param("referral");
my $email = $query->param("email");
my $phone = $query->param("phone");



$email_address = filter_header_field ( $email_address );
$comments = filter_field ( $comments );

open ( MAIL, "| /usr/sbin/sendmail -t" );
print MAIL "From: $email_address\n";
print MAIL "To: contact\@willjackson.com\n";
print MAIL "Subject: Album Design Form Submission\n\n";
print MAIL "$name\n";
print MAIL "$phone_number\n";
print MAIL "$email_address\n";
print MAIL "$message\n";
print MAIL "$referral\n";
print MAIL "$email\n";
print MAIL "$phone\n";
print MAIL "\n.\n";
close ( MAIL );

print <<END_HTML;

<html>
<head>
<link href="../style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<br /><br />
<h1>Thank you!</h1><br /><br />
<p>Someone should be contacting you shortly. If you don't hear from us within 48 hours, please call, 636-464-9475. Thank you.</p><br />
<br /><br />
<br /><br />

<div id="footer">
<p>Copyright
&copy; Will Jackson Photography, St. Louis MO. All&nbsp;rights&nbsp;reserved.</p>
</div>

</body>
</html>




END_HTML

sub filter_field
{
my $field = shift;
$field =~ s/From://gi;
$field =~ s/To://gi;
$field =~ s/BCC://gi;
$field =~ s/CC://gi;
$field =~ s/Subject://gi;
$field =~ s/Content-Type://gi;
return $field;
}

sub filter_header_field
{
my $field = shift;
$field =~ s/From://gi;
$field =~ s/To://gi;
$field =~ s/BCC://gi;
$field =~ s/CC://gi;
$field =~ s/Subject://gi;
$field =~ s/Content-Type://gi;
$field =~ s/[\0\n\r\|\!\/\<\>\^\$\%\*\&]+/ /g;
return $field;
}

07-Jan-11 15:13
Matt,

I have it figured out. I had forgot to set file permissions on the second cgi file I uploaded.

Thank you
09-Jan-11 19:29
@Will: Glad it's working now.

--
Matt Doyle, Elated
07-Jun-11 17:52
First of all I want to thank you for this tutorial

I have used it for numerous form, applications etc, and it works just fine until I needed to send a copy of the application back to the user

So I want to take the email address of the user from the form and use it for a CC in the script...I thought it would be a simple task but I was wrong again

I start with getting the form data:



my $recipientCC = $query->param("email_address");



Then I do a little check (and I also try to add the necessary \@ and \n at the end) :



if ($apptype eq "Staff")
{
$recipient = "jazztheblower\@gmail.com\n";
$recipientCC =~ s/(.*)(@.*)/$1\\$2\n/;
}



and finally I print it to the mail:



print MAIL "CC: $recipientCC";



So here's what happens:

I get the mail fine for my main recipient, but the CC should be webform2010@gmail.com but ends up as webform2010\@gmail.com@myservermailerdemon.com

I am slightly slipping into madness here I have tried changing a lot of things, like switching from a line feed \n to the actual characters, etc but nothing works

Please o'wise one, do you have any inputs on this problem?

Thanks in advance

[Edited by kingjazz on 07-Jun-11 17:15]
20-Jun-11 06:23
@kingjazz: Hmm, it could be an anti-spam mechanism inside your mail server. Is it escaping the '@' symbol like that? (\@)

Maybe try just sending 2 copies of the email, one to the main recipient and another to the CC recipient (using TO: instead of CC:)?

Also your regex might be messing things up (not sure). Try commenting it out and see if it works maybe?

Matt

--
Matt Doyle, Elated
08-Aug-11 04:34
Hi, thanks for that fantastic and very simple explanation.

I think I've done everything right, and I'm hoping what I'm about to ask is reasonably simple.

When I press 'send' I get this error:

"Not Found

The requested document was not found on this server.
Web Server at massageyou.co.nz"

the URL points to www.massageyou.co.nz/cgi-bin/formmailer.cgi as per the HTML. My CGI script is in the cgi-bin of my hosting account. However, my site is in the HTTPDocs folder of my hosting account. So I think the problem is one of two things:

1) My path is wrong. In which case, is it safe for me to move the cgi-bin folder into my HTTPDocs folder? Or is that likely to destroy things in the background that I can't fix? Or, do you know what the path might be? (Sorry, I know the path could be anything, but I'm just assuming that my hosting account is set up in a fairly standard way.

2) I'm doing something else wrong - in which case - any ideas?

Thanks.
Tom

[Edited by tomnorman on 08-Aug-11 03:35]
09-Aug-11 06:10
@tomnorman: Check with your hosting company to see what setup they recommend for running Perl CGI scripts. It may be that you need to set certain permissions, use a different file extension such as .pl, or, as you say, move your cgi-bin folder into your document root.

--
Matt Doyle, Elated
18-Nov-11 07:47
Hi,
Thanks for this tutorial.
I tried running this form mailer from my local system and i got this error.

The server encountered an internal error or misconfiguration and was unable to complete your request.

I don't know what to do.
20-Nov-11 17:56
I can feel it, I am insanely close! Unfortunately i am having the same issue as tjstab and am getting a blank email...

here's the form code:



<form action="/form-mailer.cgi" method="post" enctype="text/plain">
<input type="text" name="mail" value="Your Email" />
<input type="submit" value="Join">
</form>


and here's the cgi:



#!/usr/local/bin/perl

use CGI;

# Create the CGI object
my $query = new CGI;

# Output the HTTP header
print $query->header ( );

# Capture the form results
my $email_address = $query->param("email_address");

# Filter the form results
$email_address = filter_header_field ( $email_address );

# Email the form results
open ( MAIL, "| /usr/sbin/sendmail -t" );
print MAIL "From: $email_address\n";
print MAIL "To: subscribe\@disstory.com\n";
print MAIL "Subject: Form Submission\n\n";
print MAIL "$comments\n";
print MAIL "\n.\n";
close ( MAIL );

# Thank the user
print <<END_HTML;
<html>
<head></head>
<body>Thanks for filling in our form!</body>
</html>
END_HTML

# Functions for filtering user input

sub filter_field
{
my $field = shift;
$field =~ s/From://gi;
$field =~ s/To://gi;
$field =~ s/BCC://gi;
$field =~ s/CC://gi;
$field =~ s/Subject://gi;
$field =~ s/Content-Type://gi;
return $field;
}

sub filter_header_field
{
my $field = shift;
$field =~ s/From://gi;
$field =~ s/To://gi;
$field =~ s/BCC://gi;
$field =~ s/CC://gi;
$field =~ s/Subject://gi;
$field =~ s/Content-Type://gi;
$field =~ s/[\0\n\r\|\!\/\<\>\^\$\%\*\&]+/ /g;
return $field;
}


any help would be GREATLY appreciated!
Thank you!
21-Nov-11 23:59
@imokhaijoseph: Use CGI::Carp to view the exact error message:

http://perldoc.perl.org/CGI/Carp.html


use CGI::Carp qw(fatalsToBrowser);


@cordovad88: You seem to have missed out the lines of code relating to the comments field. This is why your message is blank.

eg:


my $comments = $query->param("comments");


is missing.

--
Matt Doyle, Elated
22-Nov-11 17:28
Thanks for the quick reply!

i added the lines to the code but.. no luck... any other ideas?

Thanks so much for your help.
23-Nov-11 21:19
@cordovad88: By "no luck" you mean you're still getting a blank email?

Your form doesn't seem to have a "comments" field in it either. You've also renamed the "email_address" field to "mail" in your form, which will break the script.

--
Matt Doyle, Elated
04-Feb-13 12:34
Hi, Can you explain how the filters work? (breakdown of the code) Thank you!
11-Feb-13 02:22
@brookln: filter_field should be self-explanatory. The last regular expression in filter_header_field filters out various unwanted characters that shouldn't be in email headers: NULLs (\0), line feeds (\n), carriage returns (\r) and so on.

--
Matt Doyle, Elated

 
New posts
Old posts

Follow Elated