Solved

Form mail results in HTML format

Posted on 2002-06-11
2
233 Views
Last Modified: 2013-12-25
I have a very simple form mail script that I use and would like to continue using, as it is easy for me to understand. What I would like to do is to send the email that is generated in html format so that I can include tables etc, allowing me to control the email layout more.

Can anybody help by showing what modification need to be made to my script? (See below)

I am new to cgi so please keep it simple.



#!/usr/bin/perl

# Location of sendmail
$mailprog = '/usr/lib/sendmail';

# Email address that emails are sent from
$to_email = 'me@mydomain.com';

# Full server path to the location of the mailfail & thankyou html files are (with trailing slash).
$pathtohtml = '/home/sites/www.mydomain.com/web/';

# The name of the html mail fail file.
$failfilename = 'mailfail.html';

# The name of the html mail ok file.
$thankyoufilename = 'mailok.html';

##########################################################################

&readparse;

#####################################
# Required Fileds
#####################################

&blank unless $form{name};
&blank unless $form{email};

#####################################
# End of required fields
#####################################

&sendemails;

#######################################################
# print the thank you page
#######################################################

open(HTMLFILE,"$pathtohtml" . "$thankyoufilename") or ("Cannot open $thankyoufilename");
{ local $/; $htmlContent = <HTMLFILE> }
close(HTMLFILE);
$htmlContent = "Content-Type: text/html\n\n" . $htmlContent;
print "$htmlContent";

################################################################################
# Send to email address set above.
#################################################################################
sub sendemails
{
open (MAIL, "|$mailprog $to_email") || die "Can't open $mailprog!\n";
print MAIL "Reply-to: $form{email}\n";
print MAIL "Subject: HTML Test\n";

print MAIL qq!
$form{email} ($form{name})
Address:
$form{address}
Tel No: $form{tel}
Fax No: $form{fax}
------------------------------------------------------------
!;
close (MAIL);
}

#######################################################
# Sub for blank fields that are required
#######################################################
sub blank
{
open(HTMLFILE,"$pathtohtml" . "$failfilename") or ("Cannot open $failfilename");
{ local $/; $htmlContent = <HTMLFILE> }
close(HTMLFILE);
$htmlContent = "Content-Type: text/html\n\n" . $htmlContent;
print "$htmlContent";
exit;
}


##############################################################
#  Parse from the web form
##############################################################
sub readparse
{
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@cgiPairs = split(/&/,$buffer);
foreach $cgiPair (@cgiPairs)
{
  ($name,$value) = split(/=/,$cgiPair);
  $value =~ s/\+/ /g;
  $value =~ s/%(..)/pack("c",hex($1))/ge;
  $form{$name} .= "\0" if (defined($form{$name}));
  $form{$name} .= "$value";
}}

0
Comment
Question by:meto
2 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7070904
which part should be HTML?
Do you mean something like this (in your sub sendmails):

<table>
<tr><th>$form{email}</th><td>($form{name})</td></tr>
<tr><th>Address:</th><td>$form{address}</td></tr>
<tr><th>Tel No:</th><td>$form{tel}</td></tr>
<tr><th>Fax No:</th><td>$form{fax}</td></tr>
</table><hr>
0
 
LVL 6

Accepted Solution

by:
andreif earned 100 total points
ID: 7072079
add Content-Type header to the message:

print MAIL "Reply-to: $form{email}\n";
print MAIL "Subject: HTML Test\n";
print MAIL "Content-Type: text/html\n";

and, of course, format your message using HTML tags :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now