Solved

Form mail results in HTML format

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article will show, step by step, how to integrate R code into a R Sweave document
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

11 Experts available now in Live!

Get 1:1 Help Now