Print Add to Favorites   Back
Scripting JMail with Perl

Article Information
Article ID: 94
Author: Support @ CCS
Created: 11/16/2007
Modified: 11/16/2007
Views: 6,191

Scripting JMail with Perl


The scripting information provided here is aimed at CCS Leeds customers. Much of the code is applicable to any Windows based Web Server. However some parameters, such as the methods used to derive the names of the SMTP servers, may not be valid.

Perl users should now use the JMail object in place of BLAT for sending mail from their scripts. If you currently use Blat, please change it.

The code uses JMail version 4.4. This is the current version installed on the servers. Detailed component information can be obtained from the Dimac site -

Before we go into detail on how to get your form working, let's look at some ground rules that will ensure your mail isn't blocked:

  • CCS Leeds redirects all web based mail to an SMTP Filter System. Mails must have either a valid "from" or "to" address which is a domain hosted with Any mail that doesn't fulfil these criteria is dropped. If you are sending mail to a customer who has given you his email address, you need to use the domain name of the site (note that it does not need to be a valid mailbox on the account) noreply@domain.tld is a fairly common one to use. Remember to use a valid account if you want the customer to reply to the email.
  • CCS Leeds SMTP Filter System rate limits outgoing mail from any domain. This prevents bulk emailing. Our limits are set to allow normal form based email activity to pass unhindered, but stop any persistent attempt to send bulk mail.

Do not use your site for mass emailing (spam). The SMTP Filter System will prevent this, and the information gets logged. Attempting to bulk email will lead to your site being closed by our abuse department.            


Using JMail in Perl to Provide Email on your site                                                                                            

First make sure your form in the submitting page is correct. In its simplest form it should look like:

				         <form action="cgi-bin/" method="post" name="mailform">
				           <input name="email" type="text" size="40">
				           <input name="email_submit" type="submit" value="send mail">

Now to the real scripting.

Using the example submit form above, this is the code in the file. Note that this script is placed in the cg-bin directory because it has the necessary permissions to execute Perl scripts.

First, initialize the necessary units and set up the JMail object,

				               use OLE;
				               use CGI;
				               $jmail = CreateObject OLE "JMail.SMTPMail";

output the HTML headers,

				               print "Content-type: text/html\n\n";

and get the recipient and header information from the post page. Note that we get the referrer page here, and extract the domain name.

				               $form = new CGI;
				               $domain = $ENV {'SERVER_NAME'};
				               $referer = $ENV {'HTTP_REFERER'};
				               $url = $referer;
				               $url =~ s/^http:\/\///i;
				               $url =~ s/^www\.//i;
				               $domain =~ s/^www\.//i;

Next we set up the mail object,

				               $Sender = "noreply\@$domain";
				               $SMTPServer = "smtp.$domain:25";
				               $Subject = "JMail Example";
				               $Body = "This test mail sent from: $ENV{'LOCAL_ADDR'} using the JMail component on the server via Perl.";
				               $Header = "Originating-IP", $ENV{'REMOTE_ADDR'};
				               $jmail->{ServerAddress} = $SMTPServer;
				               $jmail->{Sender} = $Sender;
				               $jmail->{Subject} = $Subject;
				               $jmail->AddRecipient ($Recipient);
				               $jmail->{Body} = $Body;
				               $jmail->{Priority} = $Priority;
				               $jmail->AddHeader ($Header);

and then check the referrer to ensure that the posting page is on the same site as the script. This prevents others using your script for bulk emailing activities - see the note at the bottom of this page.

				               if ($url =~ m/^$domain/)
				                               $mailmessage = "mail sent";
				                               $mailmessage = "mail was not sent.  Incorrect Referer";

Finally we output the results.

				               print "Result: $mailmessage  Recipient: $Recipient";
				               print "Sender: $Sender  SMTP Server: $SMTPServer";
				               print "Subject: $Subject  Referer: $referer";
				               print "Domain: $domain  url: $url ";



For details of the options available for  Cheap Webhosting click here

This URL: