Solved

Form mail results in HTML format

Posted on 2002-06-11
2
236 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

820 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