Miva, Miva Script, Miva Empresa, Miva Mia amd Miva Merchant are registered trademarks of the Miva Corporation
 
Ivo Truxa - truXoft control systems: advanced programming and custom IT solutions home / about / webdesign / Miva / automation / contact

http://mivo.truxoft.com
MIVO!
miva beyond limits

 

MIVA®  SNIPPETS:  Validating e-mail addresses

by Ivo Truxa, 09/14/2000

  1. Syntax verification
  2. Domain verification - shell script
  3. Domain verification - Miva call
  4. Putting it all together
  5. Test it!
  6. Using Mailer Software For Validating
  7. Links
  8. User Comments

Syntax Verification

To avoid accepting mistyped, invalid or bogus e-mail addresses you should first check for validity of the syntax. Check for appearance of a single '@' character, at least one dot, last two domain tokens must be long at least two characters and the address may contain only valid characters: aphanumeric or a hyphen '-' see RFC 1034 - 3.5. Preferred name syntax. The minimal length of an e-mail address should be 7 characters.


 

top


Domain Verification - Shell Script

If you have your own cgi-bin directory and a shell enabled or Perl installed, you can use nslookup or ping - just MvCALL a very simple shell script and parse the callvalue to find out if the domain exists. However, it will not tell you if the mailbox exists or not.

The shell script (e.g. ping.sh) would look something like this:

#! /bin/sh
echo Content-Type: text/plain\n\n
ping -c1 $1

'-c1' stands for a single ping, if you prefer you can try to ping more times. You could write the script in Perl, of course too.

Be sure to upload the file in text mode and set permission to 0755. Test it before trying with Miva - try first in Telnet: ping.sh miva.com. If it works, call it from a browser: http://yourDomain.com/cgi-bin/ping.sh?miva.com. You should either see the output of the ping command on the screen or download it as a file. If there are problems, try changing the extension to '.cgi' or write the program in Perl instead:

#!/usr/bin/perl
print "Content-Type: text/plain\n\n";
system ('ping -c1 '.$ENV{'QUERY_STRING'});

 

top


Domain Verification - Miva Call

You would call it with the following Miva code:

<MvCALL ACTION="{'http://domain/cgi-bin/ping.sh?' $ gettoken(g.email,'@',2)}" METHOD="get">
  <MvIF EXPR="{'transmitted' CIN s.callvalue}">
    <MvASSIGN NAME="l.ok" VALUE="1">
    <MvCALLSTOP>
  </MvIF>
</MvCALL>

According to your OS and the ping version you may need to change some details like url, paths, arguments or the return string. Instead of checking the callvalue you can also work with the callreturnheaders - the ping.sh script returns 500 server error if nothing found.

You should be aware that the method is not bulletproof:

  • some servers (e.g. behind a firewall) do not reply any ping requests
  • you may possibly get problems with some 3, 4 or 5 level domains
  • no ping during network problems
  • the domain may exists, but there is no mailserver

You can modify the script to use 'nslookup' instead or checking the ports 110 (POP) and/or 25 (SMTP). The last might be the best solution - as soon as I find some spare time I will try to post a sample script too.


 

top


Putting It All Together

This is a simplified code. It checks for valid characters (alphanumeric + hyphen), the minimal lenght (7 characters), for presence of '@' and a dot and pings the domain to see if some echo comes.

<MvFUNCTION NAME="verifEMail" PARAMETERS="adr">
 <MvASSIGN NAME="tmp" VALUE="{glosub(glosub(glosub(adr,'@',''),'.',''),'-','')}">
 <MvIF EXPR="{isalnum(tmp) AND '.' IN adr AND '@' IN adr AND len(adr) GE 7}">
   <MvCALL ACTION="{'http://domain/cgi-bin/ping.sh?' $ gettoken(adr,'@',2)}" METHOD="get">
     <MvIF EXPR="{'transmitted' CIN callvalue}"><MvFUNCTIONRETURN VALUE="1"></MvIF>
   </MvCALL>
   <H3>Invalid domain address or a network error.</H3> 
 <MvELSE><H3>Invalid e-mail address syntax</H3></MvIF>
 <H2>Please try again!</H2>
 <MvFUNCTIONRETURN VALUE="0">
</MvFUNCTION>

After posting this code, Drew Skinner mentioned on the Miva User List that there are addresses containing non-alphanueric characters in the first part of the address. For example !@domain.com or !/bin/sh@domain.com. They are not really conform to the recomendation of RFC, but they may exist. If you want to accept such addresses just slightly modify the code as follows:

<MvFUNCTION NAME="verifEMail2" PARAMETERS="adr">
 <MvASSIGN NAME="dom" VALUE="{gettoken(adr,'@',2)}">
 <MvASSIGN NAME="tmp" VALUE="{glosub(glosub(dom,'.',''),'-','')}">
 <MvIF EXPR="{isalnum(tmp) AND '.' IN dom AND '@' IN adr AND len(adr) GE 7}">
   <MvCALL ACTION="{'http://domain/cgi-bin/ping.sh?' $ dom}" METHOD="get">
     <MvIF EXPR="{'transmitted' CIN callvalue}"><MvFUNCTIONRETURN VALUE="1"></MvIF>
   </MvCALL>
   <H3>Invalid domain address or a network error.</H3> 
 <MvELSE><H3>Invalid e-mail address syntax</H3></MvIF>
 <H2>Please try again!</H2>
 <MvFUNCTIONRETURN VALUE="0">
</MvFUNCTION>

Here goes a sample code with a submission form and both alternatives


Test It!

  

more tolerant (non-alphanumeric characters allowed in the first part)

 

top


Using Mailer Software For Validating

Drew suggested also using the -bv option of a mailer to check if a mail to the desired address is deliverable. Unfortunately, the most common mailer, Sendmail, is absolutely useless - it confirms any non-existing domain (in the default configuration, at least). According to Drew, Exim (another mailer) is much better. You can experiment with your own mailer and modify the scripts respectively. For example, with Exim you could use the following shell script:
#! /bin/sh
echo Content-Type: text/plain\n\n
exim -bv $1

and modify the Miva script respectively:
<MvCALL ACTION="{'http://domain/cgi-bin/exim.sh?' $ adr}" METHOD="get">
  <MvIF EXPR="{'deliverable' CIN callvalue}">
    <MvFUNCTIONRETURN VALUE="1">
  </MvIF>
</MvCALL>


 

top


Some Useful Links

List of RFC's
RFC 821 - SMTP
RFC 821 (SMTP) Verifying and expanding
RFC 822 Format of ARPA Internet text messages
RFC 1035 - 8. MAIL SUPPORT
RFC 974 Mail routing and the domain system

 

top

   

Miva and some other terms used on this page are registerd trademarks of the Miva Corporation
copyright  truXoft  © 1997-2012