Hi,

I need help,I created contact form and I wrote email sending coding also,but the coding is not working.This is the coding that I have wrote.Please clear me what's the mistake....

  <?php
include "functions/libmail.php";

if (isset($_POST['Submit'])){
    extract($_POST);
    $ErrNo   = 0;
    $Name    = $_POST['name'];
    $Email   = $_POST['email'];
    $Company = $_POST['company'];

    if ($Name == ''){
        $ErrNo  = 1;
        $Err[] = 'Please enter name.';
    }
    if ($Email == ''){
        $ErrNo  = 1;
        $Err[] = 'Please enter email id.';
    }
    if ($Company == ''){
        $ErrNo  = 1;
        $Err[] = 'Please enter company name.';
    }
    if ($ErrNo  == 0){
        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$Email))
        {
           $ErrNo = 1;
           $Err[] = 'Please enter valid email address.';
        }
    }
    if ($ErrNo  == 0){

        $Phone   = trim($_POST['phoneno']);
        $Content = trim($_POST['comment']);
        $Web     = trim($_POST['website']);
        $Address = trim($_POST['address']);

        $file = "contact.html";
        $fh = fopen($file, 'r+');
        $contents = fread($fh, filesize($file));
        $contents = str_replace("XXXNAMEXXX", $Name, $contents);
        $contents = str_replace("XXXCOMPANYXXX", $Company, $contents);
        $contents = str_replace("XXXADDRESSXXX", $Address, $contents);
        $contents = str_replace("XXXEMAILXXX", $Email, $contents);
        $contents = str_replace("XXXPHONEXXX", $Phone, $contents);
        $contents = str_replace("XXXWEBXXX", $Web, $contents);
        $contents = str_replace("XXXCONTENTXXX", $Content, $contents);      


        $m= new Mail(); // create the mail
    $m->From($Email);
    $m->To("setvaz102@gmail.com");
    $m->Subject("Setvaz Enquiry Mail");

    $m->Body($contents);    // set the body
    //$m->Cc( "someone@somewhere.fr");
    //$m->Bcc( "someoneelse@somewhere.fr");
    //$m->Priority(4) ; // set the priority to Low
    //$m->Attach( "/home/leo/toto.gif", "image/gif" ) ; // attach a file of type image/gif
    if($m->Send()){
           $ErrNo = 1;
           $Err[] = 'Mail Sent Successfully';
        }
        else{
            $ErrNo = 1;
            $Err[] = 'Mail Sent Failed. Please resend again';
        }
        fclose($fh);
    }
 }
?>

Recommended Answers

All 9 Replies

in libmail.php there is function Send().
First of all echo all member of class Mail folowing exit.

If all members are Ok, then something is missing with your mail() function.trace mail function.
check this below testing email code.

<?php
$to      = 'youremailaddress@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
Member Avatar for rajarajan2017

Any error you receieved? Post the error message?

Hi,


After fields are filled when we click sumbit button ,the mail is not send.

error message that i received "mail not sent ,please try it again""

you have to debug php code by echo - exit.

Hi,

I am new to php,so I need help from you,I create html contact form and I wrote php coding ,but it have problem some.....,please clear my problem in this code or give me easy code for me to send mail.......

PHP Syntax (Toggle Plain Text)

1.
<?php
2.
include "functions/libmail.php";
3.

4.
if (isset($_POST)){
5.
extract($_POST);
6.
$ErrNo = 0;
7.
$Name = $_POST;
8.
$Email = $_POST;
9.
$Company = $_POST;
10.

11.
if ($Name == ''){
12.
$ErrNo = 1;
13.
$Err[] = 'Please enter name.';
14.
}
15.
if ($Email == ''){
16.
$ErrNo = 1;
17.
$Err[] = 'Please enter email id.';
18.
}
19.
if ($Company == ''){
20.
$ErrNo = 1;
21.
$Err[] = 'Please enter company name.';
22.
}
23.
if ($ErrNo == 0){
24.
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$Email))
25.
{
26.
$ErrNo = 1;
27.
$Err[] = 'Please enter valid email address.';
28.
}
29.
}
30.
if ($ErrNo == 0){
31.

32.
$Phone = trim($_POST);
33.
$Content = trim($_POST);
34.
$Web = trim($_POST);
35.
$Address = trim($_POST);
36.

37.
$file = "contact.html";
38.
$fh = fopen($file, 'r+');
39.
$contents = fread($fh, filesize($file));
40.
$contents = str_replace("XXXNAMEXXX", $Name, $contents);
41.
$contents = str_replace("XXXCOMPANYXXX", $Company, $contents);
42.
$contents = str_replace("XXXADDRESSXXX", $Address, $contents);
43.
$contents = str_replace("XXXEMAILXXX", $Email, $contents);
44.
$contents = str_replace("XXXPHONEXXX", $Phone, $contents);
45.
$contents = str_replace("XXXWEBXXX", $Web, $contents);
46.
$contents = str_replace("XXXCONTENTXXX", $Content, $contents);
47.
$m= new Mail(); // create the mail
48.
$m->From($Email);
49.
$m->To("setvaz102@gmail.com");
50.
$m->Subject("Setvaz Enquiry Mail");
51.

52.
$m->Body($contents); // set the body
53.
//$m->Cc( "someone@somewhere.fr");
54.
//$m->Bcc( "someoneelse@somewhere.fr");
55.
//$m->Priority(4) ; // set the priority to Low
56.
//$m->Attach( "/home/leo/toto.gif", "image/gif" ) ; // attach a file of type image/gif
57.
if($m->Send()){
58.
$ErrNo = 1;
59.
$Err[] = 'Mail Sent Successfully';
60.
}
61.
else{
62.
$ErrNo = 1;
63.
$Err[] = 'Mail Sent Failed. Please resend again';
64.
}
65.
fclose($fh);
66.
}
67.
}
68.
?>

<?php include "functions/libmail.php"; if (isset($_POST)){ extract($_POST); $ErrNo = 0; $Name = $_POST; $Email = $_POST; $Company = $_POST; if ($Name == ''){ $ErrNo = 1; $Err[] = 'Please enter name.'; } if ($Email == ''){ $ErrNo = 1; $Err[] = 'Please enter email id.'; } if ($Company == ''){ $ErrNo = 1; $Err[] = 'Please enter company name.'; } if ($ErrNo == 0){ if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$Email)) { $ErrNo = 1; $Err[] = 'Please enter valid email address.'; } } if ($ErrNo == 0){ $Phone = trim($_POST); $Content = trim($_POST); $Web = trim($_POST); $Address = trim($_POST); $file = "contact.html"; $fh = fopen($file, 'r+'); $contents = fread($fh, filesize($file)); $contents = str_replace("XXXNAMEXXX", $Name, $contents); $contents = str_replace("XXXCOMPANYXXX", $Company, $contents); $contents = str_replace("XXXADDRESSXXX", $Address, $contents); $contents = str_replace("XXXEMAILXXX", $Email, $contents); $contents = str_replace("XXXPHONEXXX", $Phone, $contents); $contents = str_replace("XXXWEBXXX", $Web, $contents); $contents = str_replace("XXXCONTENTXXX", $Content, $contents); $m= new Mail(); // create the mail $m->From($Email); $m->To("setvaz102@gmail.com"); $m->Subject("Setvaz Enquiry Mail"); $m->Body($contents); // set the body //$m->Cc( "someone@somewhere.fr"); //$m->Bcc( "someoneelse@somewhere.fr"); //$m->Priority(4) ; // set the priority to Low //$m->Attach( "/home/leo/toto.gif", "image/gif" ) ; // attach a file of type image/gif if($m->Send()){ $ErrNo = 1; $Err[] = 'Mail Sent Successfully'; } else{ $ErrNo = 1; $Err[] = 'Mail Sent Failed. Please resend again'; } fclose($fh); } } ?>


PHP Syntax (Toggle Plain Text)

1.
libmail.php

libmail.php

<?php

/*


this class encapsulates the PHP mail() function.
implements CC, Bcc, Priority headers


@version 1.3

- added ReplyTo( $address ) method
- added Receipt() method - to add a mail receipt
- added optionnal charset parameter to Body() method. this should fix charset problem on some mail clients

@example

include "libmail.php";

$m= new Mail; // create the mail
$m->From( "leo@isp.com" );
$m->To( "destination@somewhere.fr" );
$m->Subject( "the subject of the mail" );

$message= "Hello world!\nthis is a test of the Mail class\nplease ignore\nThanks.";
$m->Body( $message); // set the body
$m->Cc( "someone@somewhere.fr");
$m->Bcc( "someoneelse@somewhere.fr");
$m->Priority(4) ; // set the priority to Low
$m->Attach( "/home/leo/toto.gif", "image/gif" ) ; // attach a file of type image/gif
$m->Send(); // send the mail
echo "the mail below has been sent:<br><pre>", $m->Get(), "</pre>";


LASTMOD
Fri Oct 6 15:46:12 UTC 2000

@author Leo West - lwest@free.fr

*/


class Mail
{
/*
list of To addresses
@var array
*/
var $sendto = array();
/*
@var array
*/
var $acc = array();
/*
@var array
*/
var $abcc = array();
/*
paths of attached files
@var array
*/
var $aattach = array();
/*
list of message headers
@var array
*/
var $xheaders = array();
/*
message priorities referential
@var array
*/
var $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
/*
character set of message
@var string
*/
var $charset = "us-ascii";
var $ctencoding = "7bit";
var $receipt = 0;


/*

Mail contructor

*/

function Mail()
{
$this->autoCheck( true );
$this->boundary= "--" . md5( uniqid("myboundary") );
}


/*

activate or desactivate the email addresses validator
ex: autoCheck( true ) turn the validator on
by default autoCheck feature is on

@param boolean $bool set to true to turn on the auto validation
@access public
*/
function autoCheck( $bool )
{
if( $bool )
$this->checkAddress = true;
else
$this->checkAddress = false;
}


/*

Define the subject line of the email
@param string $subject any monoline string

*/
function Subject( $subject )
{
$this->xheaders = strtr( $subject, "\r\n" , " " );
}


/*

set the sender of the mail
@param string $from should be an email address

*/

function From( $from )
{

if( ! is_string($from) ) {
echo "Class Mail: error, From is not a string";
exit;
}
$this->xheaders = $from;
}

/*
set the Reply-to header
@param string $email should be an email address

*/
function ReplyTo( $address )
{

if( ! is_string($address) )
return false;

$this->xheaders["Reply-To"] = $address;

}


/*
add a receipt to the mail ie. a confirmation is returned to the "From" address (or "ReplyTo" if defined)
when the receiver opens the message.

@warning this functionality is *not* a standard, thus only some mail clients are compliants.

*/

function Receipt()
{
$this->receipt = 1;
}


/*
set the mail recipient
@param string $to email address, accept both a single address or an array of addresses

*/

function To( $to )
{

// TODO : test validité sur to
if( is_array( $to ) )
$this->sendto= $to;
else
$this->sendto[] = $to;

if( $this->checkAddress == true )
$this->CheckAdresses( $this->sendto );

}


/* Cc()
* set the CC headers ( carbon copy )
* $cc : email address(es), accept both array and string
*/

function Cc( $cc )
{
if( is_array($cc) )
$this->acc= $cc;
else
$this->acc[]= $cc;

if( $this->checkAddress == true )
$this->CheckAdresses( $this->acc );

}

/* Bcc()
* set the Bcc headers ( blank carbon copy ).
* $bcc : email address(es), accept both array and string
*/

function Bcc( $bcc )
{
if( is_array($bcc) ) {
$this->abcc = $bcc;
} else {
$this->abcc[]= $bcc;
}

if( $this->checkAddress == true )
$this->CheckAdresses( $this->abcc );
}


/* Body( text [, charset] )
* set the body (message) of the mail
* define the charset if the message contains extended characters (accents)
* default to us-ascii
* $mail->Body( "mél en français avec des accents", "iso-8859-1" );
*/
function Body( $body, $charset="" )
{
$this->body = $body;

if( $charset != "" ) {
$this->charset = strtolower($charset);
if( $this->charset != "us-ascii" )
$this->ctencoding = "8bit";
}
}


/* Organization( $org )
* set the Organization header
*/

function Organization( $org )
{
if( trim( $org != "" ) )
$this->xheaders = $org;
}


/* Priority( $priority )
* set the mail priority
* $priority : integer taken between 1 (highest) and 5 ( lowest )
* ex: $mail->Priority(1) ; => Highest
*/

function Priority( $priority )
{
if( ! intval( $priority ) )
return false;

if( ! isset( $this->priorities[$priority-1]) )
return false;

$this->xheaders["X-Priority"] = $this->priorities[$priority-1];

return true;

}


/*
Attach a file to the mail

@param string $filename : path of the file to attach
@param string $filetype : MIME-type of the file. default to 'application/x-unknown-content-type'
@param string $disposition : instruct the Mailclient to display the file if possible ("inline") or always as a link ("attachment") possible values are "inline", "attachment"
*/

function Attach( $filename, $filetype = "", $disposition = "inline" )
{
// TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
if( $filetype == "" )
$filetype = "application/x-unknown-content-type";

$this->aattach[] = $filename;
$this->actype[] = $filetype;
$this->adispo[] = $disposition;
}

/*

Build the email message

@access protected

*/
function BuildMail()
{

// build the headers
$this->headers = "";
// $this->xheaders = implode( ", ", $this->sendto );

if( count($this->acc) > 0 )
$this->xheaders = implode( ", ", $this->acc );

if( count($this->abcc) > 0 )
$this->xheaders = implode( ", ", $this->abcc );


if( $this->receipt ) {
if( isset($this->xheaders["Reply-To"] ) )
$this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
else
$this->xheaders["Disposition-Notification-To"] = $this->xheaders;
}

if( $this->charset != "" ) {
$this->xheaders["Mime-Version"] = "1.0";
$this->xheaders["Content-Type"] = "text/plain; charset=$this->charset";
$this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
}

$this->xheaders["X-Mailer"] = "Php/libMailv1.3";

// include attached files
if( count( $this->aattach ) > 0 ) {
$this->_build_attachement();
} else {
$this->fullBody = $this->body;
}

reset($this->xheaders);
while( list( $hdr,$value ) = each( $this->xheaders ) ) {
if( $hdr != "Subject" )
$this->headers .= "$hdr: $value\n";
}


}

/*
fornat and send the mail
@access public

*/
function Send()
{
$this->BuildMail();

$this->strTo = implode( ", ", $this->sendto );

// envoie du mail
$res = @mail( $this->strTo, $this->xheaders, $this->fullBody, $this->headers );

}

/*
* return the whole e-mail , headers + message
* can be used for displaying the message in plain text or logging it
*/

function Get()
{
$this->BuildMail();
$mail = "To: " . $this->strTo . "\n";
$mail .= $this->headers . "\n";
$mail .= $this->fullBody;
return $mail;
}


/*
check an email address validity
@access public
@param string $address : email address to check
@return true if email adress is ok
*/

function ValidEmail($address)
{
if( ereg( ".*<(.+)>", $address, $regs ) ) {
$address = $regs[1];
}
if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
return true;
else
return false;
}


/*

check validity of email addresses
@param array $aad -
@return if unvalid, output an error message and exit, this may -should- be customized

*/

function CheckAdresses( $aad )
{
for($i=0;$i< count( $aad); $i++ ) {
if( ! $this->ValidEmail( $aad[$i]) ) {
echo "Class Mail, method Mail : invalid address $aad[$i]";
exit;
}
}
}


/*
check and encode attach file(s) . internal use only
@access private
*/

function _build_attachement()
{

$this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";

$this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
$this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n" . $this->body ."\n";

$sep= chr(13) . chr(10);

$ata= array();
$k=0;

// for each attached file, do...
for( $i=0; $i < count( $this->aattach); $i++ ) {

$filename = $this->aattach[$i];
$basename = basename($filename);
$ctype = $this->actype[$i]; // content-type
$disposition = $this->adispo[$i];

if( ! file_exists( $filename) ) {
echo "Class Mail, method attach : file $filename can't be found"; exit;
}
$subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding: base64\nContent-Disposition: $disposition;\n filename=\"$basename\"\n";
$ata[$k++] = $subhdr;
// non encoded line length
$linesz= filesize( $filename)+1;
$fp= fopen( $filename, 'r' );
$ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
fclose($fp);
}
$this->fullBody .= implode($sep, $ata);
}


} // class Mail


?>

Member Avatar for rajarajan2017

Is this a error message from php? or your user defined message, but I can't see this message in your code. Plese copy and paste the whole error message with error codes displayed in your localhost?

Hi,

I am very new to php.I need your help very urgent.I created contact form design coding in html and my page extension is .php,I wrote php code in same html page,
This is my code,that is used....,I uploaded the page in online,please you check it this link www.setvaz.com/contact.php


PHP Syntax (Toggle Plain Text)

1.
<?php
2.
include "functions/libmail.php";
3.

4.
if (isset($_POST)){
5.
extract($_POST);
6.
$ErrNo = 0;
7.
$Name = $_POST;
8.
$Email = $_POST;
9.
$Company = $_POST;
10.

11.
if ($Name == ''){
12.
$ErrNo = 1;
13.
$Err[] = 'Please enter name.';
14.
}
15.
if ($Email == ''){
16.
$ErrNo = 1;
17.
$Err[] = 'Please enter email id.';
18.
}
19.
if ($Company == ''){
20.
$ErrNo = 1;
21.
$Err[] = 'Please enter company name.';
22.
}
23.
if ($ErrNo == 0){
24.
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$Email))
25.
{
26.
$ErrNo = 1;
27.
$Err[] = 'Please enter valid email address.';
28.
}
29.
}
30.
if ($ErrNo == 0){
31.

32.
$Phone = trim($_POST);
33.
$Content = trim($_POST);
34.
$Web = trim($_POST);
35.
$Address = trim($_POST);
36.

37.
$file = "contact.html";
38.
$fh = fopen($file, 'r+');
39.
$contents = fread($fh, filesize($file));
40.
$contents = str_replace("XXXNAMEXXX", $Name, $contents);
41.
$contents = str_replace("XXXCOMPANYXXX", $Company, $contents);
42.
$contents = str_replace("XXXADDRESSXXX", $Address, $contents);
43.
$contents = str_replace("XXXEMAILXXX", $Email, $contents);
44.
$contents = str_replace("XXXPHONEXXX", $Phone, $contents);
45.
$contents = str_replace("XXXWEBXXX", $Web, $contents);
46.
$contents = str_replace("XXXCONTENTXXX", $Content, $contents);
47.
$m= new Mail(); // create the mail
48.
$m->From($Email);
49.
$m->To("setvaz102@gmail.com");
50.
$m->Subject("Setvaz Enquiry Mail");
51.

52.
$m->Body($contents); // set the body
53.
//$m->Cc( "someone@somewhere.fr");
54.
//$m->Bcc( "someoneelse@somewhere.fr");
55.
//$m->Priority(4) ; // set the priority to Low
56.
//$m->Attach( "/home/leo/toto.gif", "image/gif" ) ; // attach a file of type image/gif
57.
if($m->Send()){
58.
$ErrNo = 1;
59.
$Err[] = 'Mail Sent Successfully';
60.
}
61.
else{
62.
$ErrNo = 1;
63.
$Err[] = 'Mail Sent Failed. Please resend again';
64.
}
65.
fclose($fh);
66.
}
67.
}
68.
?>

<?php include "functions/libmail.php"; if (isset($_POST)){ extract($_POST); $ErrNo = 0; $Name = $_POST; $Email = $_POST; $Company = $_POST; if ($Name == ''){ $ErrNo = 1; $Err[] = 'Please enter name.'; } if ($Email == ''){ $ErrNo = 1; $Err[] = 'Please enter email id.'; } if ($Company == ''){ $ErrNo = 1; $Err[] = 'Please enter company name.'; } if ($ErrNo == 0){ if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$Email)) { $ErrNo = 1; $Err[] = 'Please enter valid email address.'; } } if ($ErrNo == 0){ $Phone = trim($_POST); $Content = trim($_POST); $Web = trim($_POST); $Address = trim($_POST); $file = "contact.html"; $fh = fopen($file, 'r+'); $contents = fread($fh, filesize($file)); $contents = str_replace("XXXNAMEXXX", $Name, $contents); $contents = str_replace("XXXCOMPANYXXX", $Company, $contents); $contents = str_replace("XXXADDRESSXXX", $Address, $contents); $contents = str_replace("XXXEMAILXXX", $Email, $contents); $contents = str_replace("XXXPHONEXXX", $Phone, $contents); $contents = str_replace("XXXWEBXXX", $Web, $contents); $contents = str_replace("XXXCONTENTXXX", $Content, $contents); $m= new Mail(); // create the mail $m->From($Email); $m->To("setvaz102@gmail.com"); $m->Subject("Setvaz Enquiry Mail"); $m->Body($contents); // set the body //$m->Cc( "someone@somewhere.fr"); //$m->Bcc( "someoneelse@somewhere.fr"); //$m->Priority(4) ; // set the priority to Low //$m->Attach( "/home/leo/toto.gif", "image/gif" ) ; // attach a file of type image/gif if($m->Send()){ $ErrNo = 1; $Err[] = 'Mail Sent Successfully'; } else{ $ErrNo = 1; $Err[] = 'Mail Sent Failed. Please resend again'; } fclose($fh); } } ?>


PHP Syntax (Toggle Plain Text)

1.
libmail.php

libmail.php

<?php

/*


this class encapsulates the PHP mail() function.
implements CC, Bcc, Priority headers


@version 1.3

- added ReplyTo( $address ) method
- added Receipt() method - to add a mail receipt
- added optionnal charset parameter to Body() method. this should fix charset problem on some mail clients

@example

include "libmail.php";

$m= new Mail; // create the mail
$m->From( "leo@isp.com" );
$m->To( "destination@somewhere.fr" );
$m->Subject( "the subject of the mail" );

$message= "Hello world!\nthis is a test of the Mail class\nplease ignore\nThanks.";
$m->Body( $message); // set the body
$m->Cc( "someone@somewhere.fr");
$m->Bcc( "someoneelse@somewhere.fr");
$m->Priority(4) ; // set the priority to Low
$m->Attach( "/home/leo/toto.gif", "image/gif" ) ; // attach a file of type image/gif
$m->Send(); // send the mail
echo "the mail below has been sent:<br><pre>", $m->Get(), "</pre>";


LASTMOD
Fri Oct 6 15:46:12 UTC 2000

@author Leo West - lwest@free.fr

*/


class Mail
{
/*
list of To addresses
@var array
*/
var $sendto = array();
/*
@var array
*/
var $acc = array();
/*
@var array
*/
var $abcc = array();
/*
paths of attached files
@var array
*/
var $aattach = array();
/*
list of message headers
@var array
*/
var $xheaders = array();
/*
message priorities referential
@var array
*/
var $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
/*
character set of message
@var string
*/
var $charset = "us-ascii";
var $ctencoding = "7bit";
var $receipt = 0;


/*

Mail contructor

*/

function Mail()
{
$this->autoCheck( true );
$this->boundary= "--" . md5( uniqid("myboundary") );
}


/*

activate or desactivate the email addresses validator
ex: autoCheck( true ) turn the validator on
by default autoCheck feature is on

@param boolean $bool set to true to turn on the auto validation
@access public
*/
function autoCheck( $bool )
{
if( $bool )
$this->checkAddress = true;
else
$this->checkAddress = false;
}


/*

Define the subject line of the email
@param string $subject any monoline string

*/
function Subject( $subject )
{
$this->xheaders = strtr( $subject, "\r\n" , " " );
}


/*

set the sender of the mail
@param string $from should be an email address

*/

function From( $from )
{

if( ! is_string($from) ) {
echo "Class Mail: error, From is not a string";
exit;
}
$this->xheaders = $from;
}

/*
set the Reply-to header
@param string $email should be an email address

*/
function ReplyTo( $address )
{

if( ! is_string($address) )
return false;

$this->xheaders["Reply-To"] = $address;

}


/*
add a receipt to the mail ie. a confirmation is returned to the "From" address (or "ReplyTo" if defined)
when the receiver opens the message.

@warning this functionality is *not* a standard, thus only some mail clients are compliants.

*/

function Receipt()
{
$this->receipt = 1;
}


/*
set the mail recipient
@param string $to email address, accept both a single address or an array of addresses

*/

function To( $to )
{

// TODO : test validité sur to
if( is_array( $to ) )
$this->sendto= $to;
else
$this->sendto[] = $to;

if( $this->checkAddress == true )
$this->CheckAdresses( $this->sendto );

}


/* Cc()
* set the CC headers ( carbon copy )
* $cc : email address(es), accept both array and string
*/

function Cc( $cc )
{
if( is_array($cc) )
$this->acc= $cc;
else
$this->acc[]= $cc;

if( $this->checkAddress == true )
$this->CheckAdresses( $this->acc );

}

/* Bcc()
* set the Bcc headers ( blank carbon copy ).
* $bcc : email address(es), accept both array and string
*/

function Bcc( $bcc )
{
if( is_array($bcc) ) {
$this->abcc = $bcc;
} else {
$this->abcc[]= $bcc;
}

if( $this->checkAddress == true )
$this->CheckAdresses( $this->abcc );
}


/* Body( text [, charset] )
* set the body (message) of the mail
* define the charset if the message contains extended characters (accents)
* default to us-ascii
* $mail->Body( "mél en français avec des accents", "iso-8859-1" );
*/
function Body( $body, $charset="" )
{
$this->body = $body;

if( $charset != "" ) {
$this->charset = strtolower($charset);
if( $this->charset != "us-ascii" )
$this->ctencoding = "8bit";
}
}


/* Organization( $org )
* set the Organization header
*/

function Organization( $org )
{
if( trim( $org != "" ) )
$this->xheaders = $org;
}


/* Priority( $priority )
* set the mail priority
* $priority : integer taken between 1 (highest) and 5 ( lowest )
* ex: $mail->Priority(1) ; => Highest
*/

function Priority( $priority )
{
if( ! intval( $priority ) )
return false;

if( ! isset( $this->priorities[$priority-1]) )
return false;

$this->xheaders["X-Priority"] = $this->priorities[$priority-1];

return true;

}


/*
Attach a file to the mail

@param string $filename : path of the file to attach
@param string $filetype : MIME-type of the file. default to 'application/x-unknown-content-type'
@param string $disposition : instruct the Mailclient to display the file if possible ("inline") or always as a link ("attachment") possible values are "inline", "attachment"
*/

function Attach( $filename, $filetype = "", $disposition = "inline" )
{
// TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
if( $filetype == "" )
$filetype = "application/x-unknown-content-type";

$this->aattach[] = $filename;
$this->actype[] = $filetype;
$this->adispo[] = $disposition;
}

/*

Build the email message

@access protected

*/
function BuildMail()
{

// build the headers
$this->headers = "";
// $this->xheaders = implode( ", ", $this->sendto );

if( count($this->acc) > 0 )
$this->xheaders = implode( ", ", $this->acc );

if( count($this->abcc) > 0 )
$this->xheaders = implode( ", ", $this->abcc );


if( $this->receipt ) {
if( isset($this->xheaders["Reply-To"] ) )
$this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
else
$this->xheaders["Disposition-Notification-To"] = $this->xheaders;
}

if( $this->charset != "" ) {
$this->xheaders["Mime-Version"] = "1.0";
$this->xheaders["Content-Type"] = "text/plain; charset=$this->charset";
$this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
}

$this->xheaders["X-Mailer"] = "Php/libMailv1.3";

// include attached files
if( count( $this->aattach ) > 0 ) {
$this->_build_attachement();
} else {
$this->fullBody = $this->body;
}

reset($this->xheaders);
while( list( $hdr,$value ) = each( $this->xheaders ) ) {
if( $hdr != "Subject" )
$this->headers .= "$hdr: $value\n";
}


}

/*
fornat and send the mail
@access public

*/
function Send()
{
$this->BuildMail();

$this->strTo = implode( ", ", $this->sendto );

// envoie du mail
$res = @mail( $this->strTo, $this->xheaders, $this->fullBody, $this->headers );

}

/*
* return the whole e-mail , headers + message
* can be used for displaying the message in plain text or logging it
*/

function Get()
{
$this->BuildMail();
$mail = "To: " . $this->strTo . "\n";
$mail .= $this->headers . "\n";
$mail .= $this->fullBody;
return $mail;
}


/*
check an email address validity
@access public
@param string $address : email address to check
@return true if email adress is ok
*/

function ValidEmail($address)
{
if( ereg( ".*<(.+)>", $address, $regs ) ) {
$address = $regs[1];
}
if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
return true;
else
return false;
}


/*

check validity of email addresses
@param array $aad -
@return if unvalid, output an error message and exit, this may -should- be customized

*/

function CheckAdresses( $aad )
{
for($i=0;$i< count( $aad); $i++ ) {
if( ! $this->ValidEmail( $aad[$i]) ) {
echo "Class Mail, method Mail : invalid address $aad[$i]";
exit;
}
}
}


/*
check and encode attach file(s) . internal use only
@access private
*/

function _build_attachement()
{

$this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";

$this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
$this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n" . $this->body ."\n";

$sep= chr(13) . chr(10);

$ata= array();
$k=0;

// for each attached file, do...
for( $i=0; $i < count( $this->aattach); $i++ ) {

$filename = $this->aattach[$i];
$basename = basename($filename);
$ctype = $this->actype[$i]; // content-type
$disposition = $this->adispo[$i];

if( ! file_exists( $filename) ) {
echo "Class Mail, method attach : file $filename can't be found"; exit;
}
$subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding: base64\nContent-Disposition: $disposition;\n filename=\"$basename\"\n";
$ata[$k++] = $subhdr;
// non encoded line length
$linesz= filesize( $filename)+1;
$fp= fopen( $filename, 'r' );
$ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
fclose($fp);
}
$this->fullBody .= implode($sep, $ata);
}


} // class Mail


?>

Member Avatar for rajarajan2017

Your solution is there in my given article. Only you have to spend 30 mintues to make your work done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.