PHP Email – Script and Validation

Validating email with regular expression is the bast you can do with the email entered by users. There are other way to validating email about real or not but with the email provider but that is not perfect way and not possible to do.
Validate user Email ID:

<?php
function checkMail($mail) {
    if (empty ($mail))
    {
         return false;
    }
    if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $mail))
    {
        return false;
    }
    else {
        return true;
    }
}
?>

Use of simple php Email - mail() function:

<?php
 $body = "message body";
 $toName = 'satya prakash';
 $toMail = 'satyaprakash@flora2000.com';
 $fromName = 'Satya Prakash';
 $fromMail = 'satyaprakasha1@yahoo.co.in';
 $domain   = 'flora2000.com';
 $subject = 'Mail subject';
 $to = ""$toName" <$toMail>";
 $headers = "From: "$fromName" <$fromMail>" . "rn" .
 "Reply-To: "$fromName" <$fromMail>" . "rn" .
 "Return-Path: "$fromName" <$fromMail>". "rn" .
 "Message-Id: <".time().".xyz@$domain>" ."rn".
 'MIME-Version: 1.0' . "rn" .
 'Content-type: text/html; charset=iso-8859-1'. "rn".
 'X-Mailer: PHP-'. PHP_VERSION;
mail($to, $subject,  $body, $headers);
?>

I will suggest you downloading phpmailer package. Once you download this package, you will become free from any email problems. This is very good free mailer package.

PHP Mailer Function

It will provide all the functionality you are looking from email function.

  • Can send emails with multiple TOs, CCs,
    BCCs and REPLY-TOs
  • Redundant SMTP servers
  • Multipart/alternative emails for email clients that do not
    read HTML email
  • Support for 8bit, base64, binary, and quoted-printable
    encoding
  • Uses the same methods as the very popular AspEmail active server (COM) component
  • SMTP authentication
  • Word wrap
  • Address reset functions
  • HTML email
  • Tested on multiple SMTP servers:
    Sendmail,
    qmail,
    Postfix,
    Imail, Exchange, etc
  • Works on any platform
  • Flexible debugging
  • Custom mail headers
  • Multiple fs, string, and binary attachments (those
    from database, string, etc)
  • Embedded image support

Very simple example from there:

<?php
 //http://phpmailer.sourceforge.net/extending.html
 require("mail.inc.php");
 // Instantiate your new class
 $mail = new MyMailer;
 // Now you only need to add the necessary stuff
 $mail->AddAddress("josh@site.com", "Josh Adams");
 $mail->Subject = "Here is the subject";
 $mail->Body    = "This is the message body";
 $mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip");  // optional name
 if(!$mail->Send())
 {
  echo "There was an error sending the message";
  exit;
 }
 echo "Message was sent successfully";
?>

Whole Tutorial for PHP Mailing.
http://phpmailer.sourceforge.net/tutorial.html

Read various Comments on PHP email function - mail() here on official php site.