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:

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:

 $body = "message body";
 $toName = 'satya prakash';
 $toMail = '';
 $fromName = 'Satya Prakash';
 $fromMail = '';
 $domain   = '';
 $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
  • 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:
    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:

 // Instantiate your new class
 $mail = new MyMailer;
 // Now you only need to add the necessary stuff
 $mail->AddAddress("", "Josh Adams");
 $mail->Subject = "Here is the subject";
 $mail->Body    = "This is the message body";
 $mail->AddAttachment("c:/temp/", "");  // optional name
  echo "There was an error sending the message";
 echo "Message was sent successfully";

Whole Tutorial for PHP Mailing.

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